Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

Your service autoscales from 10 to 60 pods with a Hikari maximumPoolSize of 20, and PostgreSQL max_connections is 500. During a traffic spike the database refuses connections and other services sharing it fail too. How would you fix this?

At 60 pods with 20 connections each, the service can open 1,200 connections against a server that allows 500, so the database refuses new ones and every service sharing it fails. The fix is to size pools from a connection budget: divide what the database can serve among the services and the maximum pod count, use much smaller pools per pod, and cap autoscaling to fit. A server-side pooler such as PgBouncer in transaction mode can multiplex many client connections, at the cost of session state, and per-service database users with connection limits stop one service starving the rest.

Why this question is asked

Autoscaling is supposed to make a service safer under load, but here it turned a traffic spike in one service into an outage for several. The problem is that pool settings were chosen per pod, by habit, while the limit that matters is shared by the whole fleet. Interviewers ask this scenario to see whether we can do the arithmetic, understand why small pools are usually enough, and protect the shared database from any single service. In this answer, we will do the arithmetic, build a connection budget, size the pools, look at PgBouncer, and isolate services from each other.

1Doing the Arithmetic

Each pod runs its own HikariCP pool with maximumPoolSize 20. At 10 pods that is up to 200 connections, comfortably under PostgreSQL's max_connections of 500. During a spike, the autoscaler adds pods, and at 60 pods the service alone can open 60 x 20 = 1,200 connections. PostgreSQL refuses connections beyond its limit with the error sorry, too many clients already (FATAL 53300), and the refusal does not care which service asked. The other services sharing the database cannot open connections either, so they fail too.

Notice that the autoscaler made things worse at the exact moment the database was busiest. More pods meant more connections, more concurrent queries and more contention on the same CPU and disks, so each pod got slower, which triggered more scaling.

Connections at each scale
PodsPool per podPossible connectionsWithin 500?
1020200yes
2520500at the limit, other services starve
60201,200no: refusals for everyone
604240yes, within this service's budget
The limit is shared by every service, so pool size must come from a budget.

2Building a Connection Budget

A connection budget starts from the database. PostgreSQL keeps a few slots for superusers (superuser_reserved_connections, 3 by default), and we keep more for migrations, monitoring and an engineer who needs to log in during an incident, say 50. That leaves about 450 for applications. We then divide that among the services sharing the database according to their needs, for example 240 for this service, 120 for another and 90 for the rest.

This service's budget is then split across its maximum pod count. With 240 connections and up to 60 pods, each pod gets 4. If 4 is too few for one pod to work well, the honest answer is that the service cannot scale to 60 pods on this database, and the autoscaler's maximum must come down, for example to 30 pods with 8 connections each.

3Why Small Pools Are Usually Enough

It sounds alarming to run a pod with 4 connections, but the database can only do as much work at once as it has CPU cores and disk bandwidth. A server with 16 cores gains little from running 500 queries at the same moment; they queue inside the database and context-switch instead. A common starting point is a total of a few times the core count across all clients, and benchmarks usually show throughput flattening, then falling, as connections grow past that.

Within a pod, a connection held for 5 ms can serve 200 short transactions per second. Four connections give 800 per second per pod, which is plenty for most pods, as long as transactions stay short and do not hold connections during remote calls. If a pod needs many more, that is usually a sign of long-held connections that should be fixed first.

YAML
# per pod, derived from the budget, not habit
spring:
datasource:
hikari:
maximum-pool-size: 4
minimum-idle: 4
connection-timeout: 3000 # fail fast instead of piling up threads for 30 s
# autoscaler cap that matches the budget
# HorizontalPodAutoscaler: maxReplicas: 60 (60 x 4 = 240 = this service's budget)

4Adding a Server-Side Pooler

If the service really needs many pods, a server-side pooler such as PgBouncer sits between the pods and PostgreSQL. Each pod connects to PgBouncer, which keeps a smaller set of real connections to the database. In transaction mode, a client gets a real connection only for the duration of each transaction, so 1,200 client connections can share, say, 150 server connections.

But there is a catch. Because consecutive transactions from one client may run on different server connections, anything tied to a session breaks: SET commands that change session settings, session advisory locks, LISTEN and NOTIFY, temporary tables, and server-side prepared statements. The PostgreSQL JDBC driver prepares statements on the server after a few executions, so with PgBouncer in transaction mode we either set prepareThreshold=0 on the driver or use a PgBouncer version and setting that tracks prepared statements itself (max_prepared_statements in recent releases); check your versions.

Step throughPods, pooler and database
  1. 60 pods4 Hikari connections each, 240 client connections
  2. PgBouncer, transaction modemultiplexes clients onto fewer server connections
  3. PostgreSQL150 server connections, 16 cores

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

5Isolating Services From Each Other

The outage spread because every service connects as if the database were its own. We fix that with separate database users per service, each with a connection limit, for example ALTER ROLE orders_svc CONNECTION LIMIT 240. When the orders service hits its limit, its own new connections are refused, but the other services still have their share. PgBouncer can enforce similar per-user or per-database pool limits. Larger organisations go further and give busy services their own databases, so a spike in one cannot touch the others at all.

Finally, the autoscaler should know about the downstream limit. Scaling on CPU alone ignores the database; capping maxReplicas from the budget, and alerting when the pool's pending threads rise, keeps scaling within what the database can serve.

6Follow-up questions

What if 4 connections per pod make the service slower?

Then we look at how long each transaction holds its connection. Usually the slow part is a remote call or open-in-view inside the transaction, and fixing it frees far more capacity than extra connections would. If the work is genuinely database-bound, more connections will not help, because the database is the limit.

Is PgBouncer session mode useful here?

Not much. In session mode a client keeps its server connection for the whole session, so it saves connection setup cost but does not reduce the number of server connections that Hikari's long-lived pooled connections need.

How do you handle database migrations that need extra connections?

We keep a reserved part of the budget outside the application share, and run migrations as a separate user whose limit fits within that reserve.

7Conclusion

In this answer, we saw that 60 pods with 20 connections each can open 1,200 connections against a limit of 500, which made PostgreSQL refuse everyone. We built a connection budget, divided it across services and the maximum pod count, and used small pools with an autoscaling cap to fit. PgBouncer in transaction mode can multiplex many clients onto fewer server connections if we handle session state and prepared statements, and per-service users with connection limits keep one service from starving the others.

What to say in the interviewAbout 80 seconds aloud

Open with

The arithmetic is the problem: 60 pods times 20 connections is 1,200 potential connections, and PostgreSQL allows 500 for everyone, so during the spike it starts refusing connections and the other services on that database fail too.

Then, if they want more

I would stop choosing pool sizes per pod and work from a connection budget.

Out of 500, I keep some for superusers, migrations and incident access, give each service a share, say 240 for this one, and divide that by the maximum pod count, so 60 pods get 4 connections each, or I cap the autoscaler at 30 pods with 8.

Small pools are usually enough because throughput is limited by the database's cores and I/O, not connection count; with short transactions, 4 connections serve hundreds of transactions per second per pod. I would also shorten connectionTimeout so threads fail fast.

If we truly need many pods, I would put PgBouncer in transaction mode in front, which multiplexes many client connections onto fewer server connections, but it breaks session state like SET, advisory locks and LISTEN, and I need to handle prepared statements, for example prepareThreshold=0 in the driver.

Finally, each service gets its own database user with a CONNECTION LIMIT, so one service can never starve the others again.

About this answer

Last updated
12 November 2026
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.
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 "Your service autoscales from 10 to 60 pods with a Hikari maximumPoolSize of 20, and...": https://scrivio-interview-library.netlify.app/databases/database-scaling/1ade6f5b84/