Database Concurrency & Scaling interview question · Most asked
Your service throws 'connection pool exhausted' errors but database CPU is only 20%. What's going on?
Summary
Connections are being held while not doing database work — usually an HTTP call, slow rendering or open-session-in-view inside a transaction. Pool throughput is pool size ÷ hold time, so long holds drain the pool while the database idles.
Longer Answer
Check pg_stat_activity for many idle in transaction sessions and look at traces for external calls nested inside database spans. Fixes:
- Release connections before external calls; never call APIs inside a transaction.
- Disable open-session-in-view (
spring.jpa.open-in-view=false). - Fail fast with a short pool timeout and shed load instead of queueing for 30 s.
- Size pools from database capacity, not traffic, and use a pooler (PgBouncer) when instances multiply.
Raising the pool size usually just moves the bottleneck to max_connections.
Key Points
- Throughput = pool size ÷ hold time
- No external calls while holding a connection
- Short pool timeouts
- Don't fix it by raising pool sizes
Deep dive: Connection Pools and Transactions
More most-asked Database Concurrency & Scaling questions
- How do you prevent overselling in a flash sale with 50,000 buyers in the first minute?
- After a deploy, logs show hundreds of 'deadlock detected' errors per hour on checkout. Walk through how you'd investigate.
- Two customers buy the last item at the same moment and both orders are confirmed. What happened, and how do you prevent it?
- Why shouldn't you call a payment API inside a database transaction?
- What's the difference between Read Committed and Repeatable Read in practice?
- Database CPU is at 85%. How do you decide what to optimise before buying a bigger server?
- A user saves their profile and the next page shows the old data. Reads go to replicas. How do you fix it?
- How do you choose a shard key for a multi-tenant SaaS?