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

← All Database Concurrency & Scaling questions