Connection Pooling
Basics
Visual Representation
What is it?
🚕 Think of it like a taxi stand:
Without a taxi stand, every time you need a ride, you call a new taxi (wait 10 minutes for it to arrive), take the ride, and the taxi goes away. Next time? Call again, wait again.
A taxi stand keeps 20 taxis WAITING and ready. Need a ride? Grab one immediately. When you're done, the taxi returns to the stand for the next person. No waiting, instant availability.
Connection pooling is exactly this for database connections. Instead of creating a new database connection for every request (slow — takes 50-100ms!) and destroying it after, you keep a POOL of pre-made connections ready. Need to query the DB? Grab a connection from the pool. Done? Return it to the pool. No setup overhead.
💡 Simple Summary: Keep a pool of ready-to-use database connections instead of creating/destroying them for every request. Saves time and resources.
How it works — Like you're watching it happen
❓ But wait — why is creating a new connection so expensive?
A database connection isn't just "hey, connect." It involves: (1) TCP handshake (3 packets), (2) TLS negotiation (if encrypted), (3) Authentication (send credentials, server verifies), (4) Session setup (allocate memory on DB server for this session). Total: 50-200ms per new connection. If your request takes 5ms to execute, spending 100ms just connecting is 20x overhead! Connection pooling eliminates this repeated cost.
Why should you care? (Interview perspective)
Key Things to Remember
Real Examples You Use Daily
🛒 Amazon — During checkout, your request needs to: check inventory, create order, charge payment. Each might be a DB query. Without pooling, each creates 3 new connections (slow). With pooling, it grabs 3 from the pool instantly, runs queries, returns them — all in <10ms connection overhead.
📱 Instagram — Every time you scroll, dozens of database queries fire (posts, comments, likes, user info). Instagram's app servers maintain connection pools to their database clusters. Without pooling, millions of users = millions of connection attempts = database dies.
🚗 Uber — Driver location updates hit the database 1.25M times/second. If each created a new connection, the database would need to handle 1.25M connection setups/second (impossible). Connection pooling means a fixed 1000 connections handle all 1.25M queries by being rapidly reused.
💬 Any web app with a database — Even your simple personal project benefits from connection pooling. Frameworks like Django, Rails, Spring Boot all use connection pools by default.
Common Mistakes in Interviews
❌ Not mentioning connection limits — "We'll just connect to the database" — but HOW MANY connections can it handle? PostgreSQL typically maxes at 100-500. Plan for this.
❌ Setting pool size too large — "I'll set pool to 1000 for safety." But the database can't handle 1000 connections (each consumes RAM). More connections ≠ better performance. After a point, database performance DEGRADES (context switching, memory pressure).
❌ Ignoring connection leaks — Not returning connections to the pool is a common bug that causes production outages. Mention proper resource management (try-finally blocks).
❌ Forgetting multi-server math — Pool size of 20 per server × 50 servers = 1000 DB connections total. If your DB supports 200 max, you need a connection proxy (PgBouncer) between servers and DB.
❌ Not discussing timeout/queue behavior — What happens when the pool is exhausted? Does the request wait forever? Fail fast? This matters for user experience and system stability.
🎯 Interview One-Liner
"Connection pooling maintains a set of pre-established database connections that requests borrow and return, eliminating per-request connection setup overhead of 50-200ms — and pool sizing must account for the database's maximum connection limit across all application server instances."
Interview Q&A
Q: How do you determine the right pool size?
Start with the formula: connections ≈ (CPU cores × 2) + disk spindles. For a 4-core server with SSD: ~10 connections. Then consider: DB max connections divided by number of app servers. If DB allows 200 and you have 10 servers, max 20 per server. Monitor and adjust: if requests frequently wait for connections, increase pool. If DB CPU is high, pool might be too large.
Q: What's a connection leak and how do you detect it?
A connection leak is when code borrows a connection from the pool but never returns it (missing close/release call, exception skipping finally block). The pool slowly shrinks until no connections are available — then ALL requests fail with "cannot acquire connection." Detection: monitor pool metrics (available count trending toward zero), set maximum connection age/lifetime so stale connections are forcibly closed and recreated.
Q: You have 20 app servers, each with pool size 30. Database allows 200 connections. Problem?
20 × 30 = 600 connections needed, but DB only allows 200. Solution: (1) Reduce pool size to 10 per server (10 × 20 = 200). Or (2) Use PgBouncer/ProxySQL — a connection proxy that multiplexes 600 application connections over 200 actual DB connections using transaction-level pooling (connections shared between requests at transaction boundaries).
Q: Connection pooling vs connection-per-request — what's the performance difference?
Creating a new DB connection: TCP handshake (15ms) + TLS (20ms) + auth (10ms) + session setup (5ms) ≈ 50ms overhead. With pooling: grab from pool = <1ms. If your query takes 5ms, without pooling total = 55ms (90% overhead!). With pooling = 6ms. At 10K requests/sec, that's saving 490 seconds of total wait time per second — impossible without pooling.
Q: How does PgBouncer help in a microservices architecture?
With 50 microservices each having 20-connection pools = 1000 potential connections. PostgreSQL dies beyond 500. PgBouncer sits between all services and the DB, maintaining only 200 actual DB connections. It queues and multiplexes the 1000 application requests over 200 real connections. This is critical in microservices where many services share one database (before they get their own).
Quick Quiz
1/5Creating a new database connection takes 50ms. Your query takes 5ms. Without connection pooling, what percentage of time is wasted on connection setup?