Intermediate LevelSQL & MySQL
How does Database Connection Pooling reduce connection handshake overhead?
❓ Interview Prompt: Explain Database Connection Pooling, TCP and authentication handshake overhead, pool sizing formulas, and PgBouncer / HikariCP.
Creating a new database socket connection requires TCP 3-way handshakes, TLS negotiation, and database user authentication (~30-100ms overhead per request):
Connection Pool Benefits:
•
Maintains a warm pool of reusable open database connections.
•
Sizing Formula: $\text{Optimal Pool Size} = (\text{CPU Cores} \times 2) + \text{Spindle Count}$. Over-allocating thousands of database connections causes thread context switching thrashing!
Code Implementation Example
typescript
// Typical Connection Pool config in Node.js (pg / mysql2)
const pool = new Pool({
max: 20, // Max open connections in pool
idleTimeoutMillis: 30000, // Close idle connections after 30s
connectionTimeoutMillis: 2000
});💡Configuring a database connection pool in Node.js.
🎯 Interview Key Takeaway
Use connection pools to eliminate connection handshake latency, and size pools appropriately to avoid thread thrashing.
#SQL#Connection Pool#Performance#Intermediate
Back to all SQL & MySQL Questions→