This document outlines the implementation of optimized database connection pooling for the Muse DApp backend. The implementation improves performance and handles concurrent requests efficiently through proper connection management.
The database connection pooling is implemented in src/config/database.ts with the following optimized settings:
{
maxPoolSize: Math.max(10, Number(process.env.DB_MAX_POOL_SIZE) || 20),
minPoolSize: Math.max(2, Number(process.env.DB_MIN_POOL_SIZE) || 5),
maxIdleTimeMS: Number(process.env.DB_MAX_IDLE_TIME_MS) || 30000,
serverSelectionTimeoutMS: Number(process.env.DB_SERVER_SELECTION_TIMEOUT_MS) || 5000,
socketTimeoutMS: Number(process.env.DB_SOCKET_TIMEOUT_MS) || 45000,
connectTimeoutMS: Number(process.env.DB_CONNECT_TIMEOUT_MS) || 10000,
heartbeatFrequencyMS: Number(process.env.DB_HEARTBEAT_FREQUENCY_MS) || 10000,
bufferCommands: false,
bufferMaxEntries: 0,
waitQueueTimeoutMS: Number(process.env.DB_WAIT_QUEUE_TIMEOUT_MS) || 10000,
retryWrites: true,
retryReads: true,
readPreference: (process.env.DB_READ_PREFERENCE as any) || 'primary',
writeConcern: {
w: Number(process.env.DB_WRITE_CONCERN_W) || 'majority',
j: process.env.DB_WRITE_CONCERN_J !== 'false',
wtimeout: Number(process.env.DB_WRITE_CONCERN_TIMEOUT_MS) || 5000
},
compressors: ['snappy', 'zlib'],
zlibCompressionLevel: Number(process.env.DB_ZLIB_COMPRESSION_LEVEL) || 6
}- All pool settings are configurable via environment variables
- Sensible defaults provided for development
- Production-ready values for optimal performance
- Real-time connection pool statistics
- Connection metrics tracking
- Error rate monitoring
- Response time tracking
- Automated health monitoring
- Performance metrics collection
- Connection status tracking
| Variable | Default | Description |
|---|---|---|
DB_MAX_POOL_SIZE |
20 | Maximum connections in pool |
DB_MIN_POOL_SIZE |
5 | Minimum connections to maintain |
DB_MAX_IDLE_TIME_MS |
30000 | Idle connection timeout |
DB_SERVER_SELECTION_TIMEOUT_MS |
5000 | Server selection timeout |
DB_SOCKET_TIMEOUT_MS |
45000 | Socket operation timeout |
DB_CONNECT_TIMEOUT_MS |
10000 | Initial connection timeout |
DB_HEARTBEAT_FREQUENCY_MS |
10000 | Heartbeat interval |
DB_WAIT_QUEUE_TIMEOUT_MS |
10000 | Connection wait timeout |
DB_READ_PREFERENCE |
primary | Read preference |
DB_WRITE_CONCERN_W |
majority | Write acknowledgment |
DB_WRITE_CONCERN_J |
true | Journal writes |
DB_WRITE_CONCERN_TIMEOUT_MS |
5000 | Write timeout |
DB_ZLIB_COMPRESSION_LEVEL |
6 | Compression level |
New monitoring endpoints have been added to track connection pool performance:
Returns connection pool statistics:
{
"success": true,
"data": {
"readyState": 1,
"host": "localhost",
"port": 27017,
"name": "muse",
"poolSize": 5,
"maxPoolSize": 20,
"minPoolSize": 5,
"timestamp": "2024-01-01T00:00:00.000Z"
}
}Returns detailed connection metrics:
{
"success": true,
"data": {
"totalConnections": 10,
"activeConnections": 5,
"failedConnections": 0,
"averageResponseTime": 25.5,
"errorRate": 0,
"connectionUptime": 3600000,
"recentErrors": [],
"timestamp": "2024-01-01T00:00:00.000Z"
}
}Performs health check and returns status:
{
"success": true,
"data": {
"status": "healthy",
"responseTime": 15,
"timestamp": "2024-01-01T00:00:00.000Z",
"connectionStatus": true
}
}Resets connection metrics (admin only):
{
"success": true,
"message": "Database connection metrics reset successfully"
}- Connection pool eliminates the overhead of creating new connections for each request
- Multiple concurrent requests can share available connections
- Reduces connection establishment time significantly
- Maintains optimal number of connections based on load
- Prevents connection leaks and resource exhaustion
- Automatic cleanup of idle connections
- Built-in retry mechanisms for failed operations
- Connection error tracking and monitoring
- Graceful handling of connection failures
- Real-time pool statistics and metrics
- Performance tracking and alerting capabilities
- Historical data for optimization
Comprehensive test suite included in src/tests/connectionPool.test.ts:
- Connection management tests
- Pool statistics validation
- Health check functionality
- Concurrent operation handling
- Error handling scenarios
- Metrics reset functionality
// Old approach in index.ts
await mongoose.connect(MONGODB_URI)
logger.info('Connected to MongoDB')// New approach in index.ts
await database.connect()
logger.info('Connected to MongoDB with connection pooling')- Small applications:
maxPoolSize: 10-15 - Medium applications:
maxPoolSize: 20-30 - Large applications:
maxPoolSize: 50+
- Set up alerts for high error rates (>5%)
- Monitor average response times
- Track pool utilization (>80% indicates need for larger pool)
- Adjust
maxIdleTimeMSbased on traffic patterns - Optimize
serverSelectionTimeoutMSfor network conditions - Configure compression based on data size
-
Connection Timeout Errors
- Increase
DB_CONNECT_TIMEOUT_MS - Check network connectivity
- Verify MongoDB server availability
- Increase
-
Pool Exhaustion
- Increase
DB_MAX_POOL_SIZE - Check for connection leaks in application code
- Monitor slow queries
- Increase
-
High Error Rates
- Review MongoDB server logs
- Check authentication credentials
- Verify network stability
- Use
/api/database/metricsendpoint for real-time monitoring - Check application logs for connection errors
- Monitor MongoDB server metrics
- Connection strings should use environment variables
- Enable SSL/TLS for production connections
- Implement proper authentication mechanisms
- Monitor for unusual connection patterns
This connection pooling implementation provides a robust, scalable, and monitorable database connection solution for the Muse DApp. It significantly improves performance under concurrent load while providing comprehensive monitoring and debugging capabilities.
The implementation follows MongoDB best practices and includes proper error handling, retry mechanisms, and performance optimization features suitable for production environments.