Senior LevelREST APIs & Web Services
How do you configure resilience parameters for database connection pools in high-throughput APIs?
❓ Interview Prompt: Explain database connection pooling parameters (`max`, `idleTimeoutMillis`, `connectionTimeoutMillis`), pool exhaustion, and PgBouncer connection queuing.
Un-tuned database connection pools under heavy API traffic spikes cause thread starvation, query timeouts, and API crashes:
Key Pool Tuning Parameters:
•
'max': Max active DB connections (typically 20-50 per Node.js process; setting to 500 crashes PostgreSQL!).
•
'connectionTimeoutMillis': Max time an API request waits for an available pool connection before throwing a '503' error (set to ~2000ms).
•
PgBouncer: Use external connection poolers (PgBouncer) in transaction pooling mode to support 10,000 concurrent API requests over 100 physical DB connections!
Code Implementation Example
typescript
import { Pool } from 'pg';
// Tuned PostgreSQL Connection Pool for High-Throughput API
export const dbPool = new Pool({
max: 20, // Keep max connections low to prevent DB CPU thrashing
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000, // Fail fast after 2s if pool exhausted
});💡Tuned PostgreSQL connection pool configuration in TypeScript.
🎯 Interview Key Takeaway
Keep database connection pool sizes low and fail fast with short connection timeouts to prevent database CPU thrashing under API load spikes.
#Connection Pooling#PostgreSQL#PgBouncer#Senior
Back to all REST APIs & Web Services Questions→