Quick revision
The short answer
I would introduce a connection budget for every shared database, allocated to services and recorded next to each service's configuration, and require a separate database user per service with a connection limit so no service can starve another. The platform starter would set standard pool settings, sensible timeouts, leak detection and open-in-view off by default; alerts would fire on pending pool threads and database connection usage well before the limit; and autoscaling limits would be derived from downstream capacity, not only CPU.
What to remember
In this answer, we saw that repeated connection exhaustion comes from pools sized per instance against a limit shared by the fleet. The policy gives every database a recorded connection budget allocated to services, enforces it with per-service users and connection limits, sets safe pool defaults in the platform starter including leak detection and open-in-view off, alerts on pending threads and usage before limits are reached, and derives autoscaling limits from downstream capacity.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would introduce a connection budget for every shared database, allocated to services and recorded next to each service's configuration, and require a separate database user per service with a connection limit so no service can starve another. The platform starter would set standard pool settings, sensible timeouts, leak detection and open-in-view off by default; alerts would fire on pending pool threads and database connection usage well before the limit; and autoscaling limits would be derived from downstream capacity, not only CPU.
Why this question is asked
Three outages with the same cause in one year, in different teams, is a signal that the problem is not any one team's code. It is a missing rule at the level of the platform: every team chooses its pool size and autoscaling settings locally, while the database connection limit is shared. Interviewers ask this to see whether we can turn incident learnings into policy and tooling that teams adopt without friction. In this answer, we will see why exhaustion keeps happening, define connection budgets, enforce isolation with per-service users, standardise pool settings in the starter, add early alerts, and connect autoscaling to downstream capacity.
1Why Connection Exhaustion Keeps Happening
Each service has a connection pool per instance, and each team picks a pool size, usually by copying a number that worked elsewhere. The database's max_connections, however, is shared by every service that uses it. Nobody owns the total. When one service scales out, has a slow dependency that makes it hold connections longer, or leaks connections, it consumes the shared limit, and the database refuses connections to everyone.
The incidents look different on the surface, a deploy, a traffic spike, a bug, but the mechanism is the same. That is why a fleet-wide policy is the right answer, not three separate fixes.
2A Connection Budget for Every Database
For each shared database, the platform records a budget: the total connections the database can serve well, a reserve for administration, migrations and incident access, and a share for each service that uses it. A service's share is then divided by its maximum instance count to get the pool size per instance. The budget lives in version control next to the service's deployment configuration, so a change to pool size or maximum replicas that breaks the budget is visible in review, and a simple check in the pipeline can reject it.
- Pool size x max replicas known?YesCompare with the service's shareNoReject: both must be declared
- Within the share?YesDeployNoFail the pipeline, ask for a budget change
- Budget change approved?YesUpdate shares and deployNoReduce pool or replicas
3 steps in order. Play them, or step through them one at a time.
Notice that the budget is also a conversation tool. When a team needs more, the discussion is explicit: who gives up connections, or does the database need to grow, or does the team need a pooler or its own database?
3Enforcing Isolation With Per-Service Users
A budget on paper does not stop a buggy service at 3 a.m. So every service connects with its own database user, and that user has a connection limit equal to its share, for example ALTER ROLE orders_api CONNECTION LIMIT 240 in PostgreSQL. If orders-api exhausts its share, only its own new connections are refused; billing and reporting keep working. The incident stays inside one service, which is exactly what the three past outages lacked.
Per-service users have other benefits too. pg_stat_activity shows which service holds which connections, query statistics can be grouped by service, and permissions can be narrowed to the tables each service needs.
4Standard Pool Settings in the Platform Starter
Most teams should not have to think about pool tuning. The platform's Spring Boot starter sets defaults that reflect the lessons from the incidents: a maximum pool size taken from the budget, a short connectionTimeout of a few seconds so threads fail fast instead of piling up, leakDetectionThreshold on so long-held connections are logged with a stack trace, spring.application.name passed to the database as the application name, and spring.jpa.open-in-view=false so a web request does not hold a connection for its whole life.
Teams can override these values, but an override is visible in configuration and in review. Defaults that are right for most cases do more for reliability than a long wiki page.
5Alerting Before the Limit
The past outages were noticed when the database refused connections, which is too late. We alert earlier, on two levels. In each service, Hikari's Micrometer metrics show pending threads, threads waiting for a connection; a sustained non-zero value means the pool is saturated. On the database, connection usage per user against its limit, and total connections against max_connections, show trouble building up. An alert at 80 percent of a service's share, routed to that service's team, gives time to act before anyone is affected.
- Connection budget per databaseshares and reserve, in version control
- Deployment pipeline checkpool size x max replicas within the share
- Platform starter defaultspool size, timeouts, leak detection, open-in-view off
- Per-service database usersCONNECTION LIMIT equals the share
- Alertspending threads, usage over 80 percent of share
5 layers, from the top down. Step through them one at a time.
6Autoscaling That Respects Downstream Capacity
Autoscalers usually react to CPU or request rate, which ignores the database. A service that scales from 10 to 60 instances during a spike multiplies its connections by six, and every extra query competes for the same database cores. So the maximum replica count is part of the budget, and the autoscaler's maxReplicas is generated from it. Where a service truly needs elastic scale beyond what the database can hold, the answer is a pooler such as PgBouncer, caching, or a design change, not a larger pool.
I would roll the policy out database by database, starting with the one behind the past incidents, and review its effect after a quarter: number of connection-related incidents, how often alerts fired early, and how many teams needed budget changes.
7Follow-up questions
How do you get teams to accept limits on their pools?
By showing that the limits protect them as much as everyone else, using the three incidents as examples, and by making the default setup easier than a custom one. The budget is also negotiable through a clear process, which matters for acceptance.
What about services that need large pools, like batch jobs?
They get a share that fits their workload and a lower maximum instance count, or they run against a replica or a separate database. Batch jobs can also be scheduled at quiet times when the budget can lend them more.
Is max_connections itself worth raising?
Only modestly. Each PostgreSQL connection is a separate process with its own memory, and many active connections compete for the same cores. Raising the limit without more capacity mostly lets the database get slower before it refuses work.
8Conclusion
In this answer, we saw that repeated connection exhaustion comes from pools sized per instance against a limit shared by the fleet. The policy gives every database a recorded connection budget allocated to services, enforces it with per-service users and connection limits, sets safe pool defaults in the platform starter including leak detection and open-in-view off, alerts on pending threads and usage before limits are reached, and derives autoscaling limits from downstream capacity.
Open with
The three outages share one cause: each team sizes its pool per instance, but max_connections is shared and nobody owns the total. So I would make the total explicit.
Then, if they want more
Every shared database gets a connection budget with a reserve for administration and a share per service, kept in version control next to the service's configuration, and the deployment pipeline checks that pool size times maximum replicas fits the share.
To enforce it at runtime, every service connects with its own database user with a CONNECTION LIMIT equal to its share, so a misbehaving service only exhausts its own connections and the rest keep working; it also lets us see who holds what.
The platform starter sets the defaults: pool size from the budget, a short connection timeout, leak detection on, the application name passed to the database, and open-in-view off. For alerting, I would watch Hikari pending threads in each service and connection usage per database user, alerting the owning team at around 80 percent of its share, before anything is refused.
Finally, autoscaling limits come from the budget, not only CPU, and services that need more elasticity get a pooler, caching or their own database. I would roll it out starting with the database behind the incidents and measure the effect after a quarter.
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.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Database outages caused by connection exhaustion have happened three times this year...": https://scrivio-interview-library.netlify.app/databases/database-scaling/56b9e57f6a/