MultiHub Forum

Full Version: Database connection pooling issues - connections timing out
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm running into persistent database connection issues in our web application. We're using PostgreSQL with a connection pool, but we keep getting connection timeouts under load. Here's our current setup:

```javascript
// Database configuration
const pool = new Pool({
host: 'localhost',
database: 'myapp',
user: 'myuser',
password: 'mypassword',
port: 5432,
max: 20, // maximum number of clients in the pool
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
});
```

The symptoms:
- Under moderate load (~100 concurrent users), we start seeing "Connection terminated unexpectedly" errors
- Database CPU usage spikes to 100%
- New connection attempts timeout

I've tried the usual database troubleshooting steps:
1. Increased max_connections in PostgreSQL config
2. Adjusted connection pool settings
3. Added more database resources

But the problems keep coming back. I'm looking for more advanced debugging tips and solutions for these kinds of database issues.

Specific questions:
1. How do I properly size a connection pool for my application?
2. What tools can I use to monitor database connection health?
3. Are there common coding errors and fixes related to connection leaks?
4. How do I distinguish between database server issues and application code issues?

This feels like one of those web development support challenges that requires understanding both the database layer and the application layer. I'd appreciate any code review and feedback on our approach or suggestions for better monitoring and debugging strategies.