Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Connection storms after deployments push a shared PostgreSQL cluster to max_connections and cause outages for other teams. Design connection management for 150 services sharing a few clusters.

I would put a central pooling layer, PgBouncer, a managed proxy such as RDS Proxy, or a sidecar, between all services and each cluster, with hard per-database and per-user limits, so no single deployment can reach max_connections. Each service gets a connection budget derived from the cluster's capacity, its HikariCP maximumPoolSize is computed from that budget instead of left at the default, and administrators keep reserved connections. Staggered rollouts and a small minimumIdle stop every pod from connecting at once, and per-role alerts with a review process keep the budgets honest.

Why this question is asked

A connection storm is a strange kind of outage: one team deploys a perfectly healthy service, and three other teams' services start failing with errors that there are too many clients already. On a shared cluster, connections are a shared resource with a hard limit, and without rules the first service to grab them wins. Interviewers ask this design question to see whether we can find the mechanism behind the storm and build limits at the right layers. In this answer, we will see why deployments cause storms, then design the pooling layer, connection budgets, reserved connections, startup behaviour, and the monitoring and review that keep it working.

1Why Deployments Cause Connection Storms

max_connections is a hard limit on the number of backend processes the server accepts. When it is reached, every new connection fails with the error FATAL: sorry, too many clients already, whichever team it belongs to.

Deployments push us there in a predictable way. HikariCP's minimumIdle defaults to the same value as maximumPoolSize, so each new pod opens its whole pool at startup. A rolling deployment starts new pods before old ones stop, so for a few minutes both generations hold connections. Take one service with 40 pods and a pool of 10: normally 400 connections, during the rollout up to 800, plus retries from pods whose first attempts failed. On a cluster with max_connections = 1000 shared by many services, that one deployment can fill it.

Step throughA rolling deployment fills max_connections
New podsOld podsPostgreSQLOther team
hold 400 connections
40 pods x 10 at startup: 400 more
1,000 of 1,000 used
connect
FATAL: too many clients already
failed pods retry immediately

6 messages in order. Play them, or step through them one at a time.

Both generations hold full pools at the same time, and retries make it worse.

2A Central Pooling Layer With Hard Limits

The first layer is a pooler between the services and each cluster. PgBouncer in transaction mode, a managed proxy such as RDS Proxy, or a pooler sidecar all do the same job: they accept many client connections cheaply and run a small, bounded number of server connections. Crucially, they enforce limits per database and per user, so one service's surge is held at the pooler instead of reaching the server.

With PgBouncer, max_db_connections caps server connections for a database and max_user_connections caps them per user, while default_pool_size sets the normal pool per database and user pair. Clients above the limit wait in PgBouncer's queue for a short time instead of being refused by PostgreSQL. We also set a per-role limit in PostgreSQL itself as a second line of defence.

Java
; pgbouncer.ini
[databases]
orders = host=pg-shared-1 dbname=orders max_db_connections=120
[pgbouncer]
pool_mode = transaction
default_pool_size = 20
max_user_connections = 40
-- in PostgreSQL, the same limit as a backstop
ALTER ROLE orders_svc CONNECTION LIMIT 40;

3Per-Service Connection Budgets

Next, each service gets a budget of server connections, derived from what the cluster can actually run. A cluster with 32 cores might run well with about 200 active server connections. We keep some for administrators and replication, and split the rest between services by their measured needs, for example 40 for orders, 20 for billing and 10 for each small service.

The HikariCP pool size per pod is then computed, not guessed. Without a pooler, it is the budget divided by the maximum number of pods, including the rollout surge, so 40 / (20 pods x 2) = 1 is clearly too small, which tells us that service needs the pooler. With a pooler in transaction mode, the HikariCP pool can be moderate, say 10 per pod, because client connections to PgBouncer are cheap, and the budget is enforced as max_user_connections at the pooler. Either way, the number comes from a formula in a shared library or config template, not from the default of 10 copied everywhere.

Step throughConnection limits at each layer
  1. HikariCP per podmaximumPoolSize from the formula, small minimumIdle
  2. Poolermax_user_connections = service budget, queueing above it
  3. PostgreSQL roleCONNECTION LIMIT as a backstop
  4. PostgreSQL servermax_connections with reserved slots for admins
  5. Monitoringuse per role against budget, alerts at 80 percent

5 layers, from the top down. Step through them one at a time.

4Reserved Connections for Administrators

When a cluster is full, the people who need a connection most are the ones fixing it. PostgreSQL keeps superuser_reserved_connections, 3 by default, for superusers only. Since PostgreSQL 16 there is also reserved_connections, which reserves slots for members of the pg_use_reserved_connections role, so operators and monitoring can connect without being superusers.

Java
superuser_reserved_connections = 3
reserved_connections = 5 # PostgreSQL 16 and later
GRANT pg_use_reserved_connections TO dba_oncall, monitoring;

With these, an on-call engineer can always log in, see which role holds the connections and terminate them, even in the middle of a storm.

5Calm Startups

The storm is caused by everyone connecting at once, so we change startup behaviour too. Setting HikariCP's minimumIdle to a small value, such as 2, means a new pod opens two connections and grows only when traffic needs more. Kubernetes rollouts with a small maxSurge, for example 10 percent, replace pods in waves instead of doubling the fleet. Readiness probes that do not open extra connections, and retries with exponential backoff and jitter, keep failed pods from hammering the server in lockstep.

Java
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=2
spring.datasource.hikari.initialization-fail-timeout=-1 # start, and keep retrying in the background
# deployment.yaml
strategy:
rollingUpdate: { maxSurge: 10%, maxUnavailable: 0 }

Notice that minimumIdle lower than maximumPoolSize makes Hikari a dynamic pool that also closes idle connections after idleTimeout, which brings the count back down after the deployment.

6Alerts and a Review Process

Limits only hold if we watch them. We export connection counts per role from pg_stat_activity and from the pooler's statistics, and alert when a role passes 80 percent of its budget or the cluster passes 80 percent of max_connections. Pooler wait times show when a service is short of connections before users notice.

Budgets change through a lightweight review. A team asking for more connections shows its measured active connection use and wait times, and the database team checks the cluster's headroom. Sometimes the answer is yes; often the real fix is a slow query that holds connections too long, or an idle-in-transaction bug, and the review finds it.

SQL
SELECT usename, count(*) AS used,
count(*) FILTER (WHERE state = 'active') AS active
FROM pg_stat_activity GROUP BY usename ORDER BY used DESC;
-- orders_svc | 38 | 11
-- billing | 19 | 4

7Follow-up questions

Why not just raise max_connections to 5,000?

Because each connection is a backend process with its own memory, and snapshot and lock overhead grow with the number of connections. Raising the limit lets the storm in instead of stopping it, and the cluster may become slow for everyone instead of refusing a few connections.

What should a service do when the pooler queues its requests?

Wait briefly and then fail fast with a clear error, so threads do not pile up. HikariCP's connectionTimeout, for example 3 seconds, sets how long a request waits for a connection, and the service should have timeouts and circuit breakers so one slow dependency does not exhaust its own threads.

Does a pooler solve idle-in-transaction connections?

No. In transaction mode, a client that opens a transaction and waits holds a server connection the whole time. idle_in_transaction_session_timeout in PostgreSQL ends such sessions, and fixing the code that holds transactions open is the real cure.

8Conclusion

In this answer, we saw that connection storms come from rolling deployments where each pod opens its full HikariCP pool and both generations hold connections at once. We designed limits at every layer: a central pooler with per-database and per-user limits, per-service budgets from cluster capacity with pool sizes computed from a formula, role connection limits as a backstop, reserved connections for administrators including reserved_connections in PostgreSQL 16 and later, calm startups with a small minimumIdle and staggered rollouts, and per-role alerts with a review process for more connections.

What to say in the interviewAbout 80 seconds aloud

Open with

The storm comes from how pools start. HikariCP's minimumIdle defaults to the pool size, so every new pod opens its full pool, and a rolling deployment runs old and new pods together, so one service can briefly double its connections and fill max_connections, and then every team gets too many clients.

Then, if they want more

I would design limits at several layers. First, a central pooler, PgBouncer in transaction mode or a managed proxy like RDS Proxy, with max_db_connections and max_user_connections, so a surge waits in the pooler's queue instead of hitting the server.

Second, each service gets a connection budget from the cluster's real capacity, and its HikariCP maximumPoolSize comes from a formula in a shared template, not the default, with ALTER ROLE CONNECTION LIMIT as a backstop. Third, reserved connections: superuser_reserved_connections, and on PostgreSQL 16 or later reserved_connections for the on-call and monitoring roles, so we can always get in.

Fourth, calm startups: minimumIdle around 2, small maxSurge in rollouts, and retries with backoff and jitter. Finally, I would alert on connections per role against budget and at 80 percent of max_connections, and have a short review when a team wants more, which often finds a slow query or an idle-in-transaction bug instead.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Connection storms after deployments push a shared PostgreSQL cluster to max_connections...": https://scrivio-interview-library.netlify.app/databases/postgresql/9cb8c39bfd/