N
Neon4mo ago
generous-apricot

Db connections are not removed when established from aws lambda

Hi team, we are running in aws lambdas and setting up a connection like this:
const pool = new Pool({
connectionString: ...,
connectionTimeoutMillis: 10000, // connection timeout in milliseconds
idleTimeoutMillis: 10000, // idle timeout in milliseconds
});
const db = drizzle(pool, { schema: { ...schema, ...relations } });
const pool = new Pool({
connectionString: ...,
connectionTimeoutMillis: 10000, // connection timeout in milliseconds
idleTimeoutMillis: 10000, // idle timeout in milliseconds
});
const db = drizzle(pool, { schema: { ...schema, ...relations } });
This db is reused between invocations in lambda. Current executions never get above 25 in aws, and there's concurrency limits set on that, however in neon dashboard it's seen the db connection count approaches 100. how is that possible? Is there a way to counteract this?
No description
6 Replies
ugly-tan
ugly-tan4mo ago
This is using node-postgres driver, right? You might also want to set the max pool connections. It's possible that each pool in each is holding ~4 connections
generous-apricot
generous-apricotOP4mo ago
import { neonConfig, Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
import { neonConfig, Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
ugly-tan
ugly-tan4mo ago
Ah, ok
generous-apricot
generous-apricotOP4mo ago
right that makes sense and in lambda that's wasteful, looking into how to do this. Also just switched to -pooled is it possible that this Pool is holding 4 connections?
const pool = new Pool({
max: 1,
connectionString: Resource.NeonDbConnectionStringMobileAnalyticsDb.value,
connectionTimeoutMillis: 10000, // connection timeout in milliseconds
idleTimeoutMillis: 10000, // idle timeout in milliseconds
});
const pool = new Pool({
max: 1,
connectionString: Resource.NeonDbConnectionStringMobileAnalyticsDb.value,
connectionTimeoutMillis: 10000, // connection timeout in milliseconds
idleTimeoutMillis: 10000, // idle timeout in milliseconds
});
Do you mean adding this max here?
ugly-tan
ugly-tan4mo ago
That would work, although I would be a little bit careful. There might be some reason why your lambda creates more connections that would then break if it cannot create more than 1 So, make sure to test it
generous-apricot
generous-apricotOP4mo ago
thanks! I don't see a reason why aws lambda would attempt to use multiple db connections in a normal, non-exotic usage. As lambda is not able to handle multiple requests concurrently

Did you find this page helpful?