Skip to the page

Common backend failure patterns · core

Connection pools: exhaustion, sizing and idle in transaction

A pool error such as "timed out waiting for a connection" means connections are held too long or there are too many requests. A bigger pool usually makes it worse. Find who holds the connections and why.

The notes

What you must know

  • The error shows requests waited the full timeout (for example 30 s) for a free connection, so the pool is empty. If database CPU is low (for example 30%), connections are held while doing little.
  • Check pool metrics per pod (active, idle, pending, usage time), then the database view (pg_stat_activity: state, xact_start, wait events, last query), then recent changes and dependency health.
  • The classic cause: a slow outside API call made inside a database transaction. The connection sits "idle in transaction", often holding row locks, until the call ends.
  • An HTTP client with no read timeout turns one slow partner into an outage for the whole service. Set timeouts on every outside call and use a circuit breaker.
  • More pods times a bigger pool can ask for more connections than the database allows (for example 10 pods x 100 = 1,000 against a limit of 500). Other services then get "too many clients". More connections also mean more lock waits.
  • A database runs about as many queries at once as it has cores. HikariCP guide starting point: cores x 2 plus disks. Many more active connections only adds waiting.
  • Do not restart everything at once. Fix the holder (timeout, move the call out of the transaction), then put pool sizes back.
  • Little law: connections in use = arrival rate x hold time. A slow query at 200 ms and 200 requests a second needs 40 connections per pod; fix the query, not the pool.

The red pen

Common traps: mistake → fix

  • Mistake: Pool exhausted means we need a bigger pool

    Fix: more connections can overload the database and hide the real cause.

  • Mistake: Only slow queries hold connections

    Fix: a transaction waiting on an outside call holds one for seconds while the database does nothing.

  • Mistake: Idle connections are free

    Fix: each Postgres connection is a process with its own memory.

Easy to hard

Questions interviewers ask, easy to hard

  1. Requests fail with "timeout waiting for a connection". What do you check?
  2. Database CPU is only 30%. What does that tell you?
  3. Why can raising the pool size make an outage worse?
  4. How do you stop one slow partner from taking down your service?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview