Mhonn YbañezSRE, DevOps, AI Engineer
 Today I Learned
Read the full page

What is thread pool exhaustion?

Posted
Tags
reliability, sre, distributed-systems

A service can stop answering while its CPU sits idle, because every thread is stuck waiting. Usually the threads are waiting on a database connection, and that pool ran out first.

Yesterday, while writing about RED vs USE, I noted that in a ghost outage the thread pool can fill up even while CPU looks idle. I wanted to understand that properly.

The question I had

What is thread pool exhaustion?

What I found

Thread pool exhaustion is when every thread in a service's pool is busy, so new requests wait in line or get rejected, even though the machine itself may be nearly idle.

The setup

Many servers don't create a new thread for each request. They keep a fixed pool (Tomcat's default is 200 threads). Each request borrows a thread, does its work, and gives it back. That keeps memory in check and avoids the cost of creating threads all the time.

How it runs out

The pool runs dry when threads are borrowed faster than they come back. The usual cause isn't heavy work. It's waiting.

  • A downstream service or database that normally answers in 50ms slows to 5 seconds.
  • Each thread calling it now sits blocked 100 times longer, doing nothing.
  • With the same traffic, all 200 threads are soon stuck waiting.
  • New requests queue up (saturation), then time out or get rejected.

Little's Law makes this easy to estimate: threads in use ≈ requests per second × how long each request holds a thread. At 100 requests per second and 50ms each, that's about 5 threads. If the dependency slows to 2 seconds, it's 200, and the whole pool is gone.

Why it's sneaky

  • CPU looks fine. Blocked threads use no CPU, so the physical resource metrics stay quiet.
  • It spreads. A slow dependency fills the caller's pool, the caller slows down, and that fills its own callers' pools. One slow service can freeze a chain of healthy ones (I touched on this in my p99 vs average latency post).
  • It hits unrelated endpoints. If /checkout and /health share the pool, a slow payment API can make health checks fail. The load balancer then pulls healthy instances out, which makes things worse.

What to look at

SignalWhat it tells me
Active threads compared with pool maxUtilization. Pinned at max means exhausted.
Queued requests, or time waiting for a threadSaturation. Should be about 0.
Rejected requestsErrors on the pool itself
A thread dumpWhat the threads are actually blocked on. Usually one slow call stands out.

How to prevent it

  • Timeouts on every outbound call, so a thread can't wait forever.
  • Bulkheads: a separate, smaller pool per dependency, so a slow payment API can only use up the payment threads.
  • Circuit breakers: stop calling a dependency that keeps timing out, and return a fallback right away.
  • Async or non-blocking I/O (event loops, virtual threads), so waiting doesn't tie up a thread.
  • A bigger pool is not the fix. It only buys time, and can push the overload downstream.

Connection pool exhaustion: the same thing, one layer down

Both are the same problem with the roles flipped:

  • Thread pool exhaustion: my app is the server. It runs out of threads to serve its clients.
  • Connection pool exhaustion: my app is the client. It runs out of connections to its server, the database.
  Clients (browsers, other services)
          │  requests
          ▼
┌─────────────────────────────────┐
│ My app (the server)             │
│   Thread pool: 200 threads      │ ← thread pool exhaustion:
│          │                      │   no free thread to serve a client
│          ▼                      │
│   Connection pool: 10 conns     │ ← connection pool exhaustion:
└──────────┬──────────────────────┘   no free connection to reach the db
           │  queries (my app is now the client)
           ▼
┌─────────────────────────────────┐
│ Database (the server)           │
│   max_connections: 100          │ ← shared by every app instance
└─────────────────────────────────┘

Read top to bottom, it also shows how one causes the other. A thread waiting for a connection is a blocked thread. When the connection pool is empty, threads pile up waiting (HikariCP waits up to 30 seconds by default), and the thread pool empties too. So the first error is often "no threads available" while the cause is one layer down.

Why it runs out

Apps pool connections because opening one is expensive (TCP, TLS, auth, and memory on the database side). Like threads, connections run out when they're held too long:

  • Slow queries: a missing index or lock wait turns 5ms into 2 seconds.
  • Long transactions: slow work, like an external API call, inside an open transaction (idle in transaction in Postgres).
  • Leaks: an error path never returns the connection.

Little's Law again: 200 queries per second at 10ms needs about 2 connections. At 200ms it needs 40, and a pool of 20 is gone.

The database's ceiling is shared

Each instance has its own pool, but they all share the database's limit. With Postgres at 100, 10 instances × HikariCP's default of 10 already hits it. Autoscaling makes it worse, because every new instance brings more connections.

Read replicas raise the ceiling only for reads. Each replica has its own max_connections, but writes still go to the primary, so its limit stays the same. The app's pool doesn't grow on its own either: I'd add a separate read pool pointed at the replicas, which means more connections per instance to fit under each database's limit. Replicas help when reads are what's running out and they can live with a little replication lag. They don't help when the primary is the bottleneck or a slow query is the real cause.

What to look at and how to prevent it

The same signals as the thread pool (active compared with max, wait time, acquire timeouts), plus total connections on the database compared with max_connections, and long or idle in transaction sessions in pg_stat_activity.

  • Fix the slow query, and keep transactions short.
  • Always return connections (try-with-resources, with, defer).
  • Timeouts on both sides: a short acquire timeout in the app, plus statement_timeout and idle_in_transaction_session_timeout in Postgres.
  • Size pools so instances × pool size fits under max_connections, and add a pooler (PgBouncer, RDS Proxy) once there are many instances.

Takeaway

Thread pool exhaustion is usually a symptom. A slow dependency or an empty connection pool is often the cause behind it. Making either pool bigger rarely fixes it. It just moves the pressure somewhere else.