Skip to content

Commit c3cd0da

Browse files
committed
feat(backend): add production connection pooling limit for database
1 parent 6687b7f commit c3cd0da

File tree

3 files changed

+93
-23453
lines changed

3 files changed

+93
-23453
lines changed

backend/src/app.module.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import { UsersModule } from './users/users.module';
2828
useFactory: (configService: ConfigService) => {
2929
const databaseUrl = configService.get<string>('DATABASE_URL');
3030

31+
const isProduction =
32+
configService.get<string>('NODE_ENV') === 'production';
33+
3134
// Use DATABASE_URL if provided (production/Aiven), otherwise use individual vars (local dev)
3235
if (databaseUrl) {
3336
// Aiven SSL configuration with CA certificate
@@ -47,10 +50,21 @@ import { UsersModule } from './users/users.module';
4750
CourseSection,
4851
Lesson,
4952
],
50-
synchronize: configService.get<string>('NODE_ENV') !== 'production',
53+
synchronize: !isProduction,
5154
ssl: caCert
5255
? { rejectUnauthorized: true, ca: caCert }
5356
: { rejectUnauthorized: false },
57+
// Connection pool settings for serverless environment (Aiven limit: 20)
58+
extra: {
59+
// Max 2 connections per Lambda to stay under Aiven's 20 limit
60+
max: isProduction ? 2 : 10,
61+
min: 0,
62+
// Fail fast if no connection available
63+
connectionTimeoutMillis: 5000,
64+
// Release idle connections quickly in serverless
65+
idleTimeoutMillis: 5000,
66+
allowExitOnIdle: true,
67+
},
5468
};
5569
}
5670

@@ -72,11 +86,18 @@ import { UsersModule } from './users/users.module';
7286
CourseSection,
7387
Lesson,
7488
],
75-
synchronize: configService.get<string>('NODE_ENV') !== 'production',
89+
synchronize: !isProduction,
7690
ssl:
7791
configService.get<string>('DB_SSL') === 'true'
7892
? { rejectUnauthorized: false }
7993
: false,
94+
// Connection pool settings
95+
extra: {
96+
max: 10,
97+
min: 0,
98+
connectionTimeoutMillis: 10000,
99+
idleTimeoutMillis: 30000,
100+
},
80101
};
81102
},
82103
inject: [ConfigService],

0 commit comments

Comments
 (0)