Quick revision
The short answer
I would start by measuring who uses the database and how, then act on three time horizons at once. In the short term, connection budgets, shorter transactions and a connection proxy such as PgBouncer buy headroom; in the medium term, reads move to replicas and caches and batch work moves off peak; in the long term, data is split by domain ownership, which is a multi-year programme. I present these options with cost, risk and time, and fund the long path while the quick fixes buy the time it needs.
What to remember
In this answer, we saw that a shared database near its connection and CPU limits needs measurement before decisions: connections per service, the queries that burn CPU, and the split between reads, writes and batch. Short-term budgets, shorter transactions and a connection proxy buy headroom; replicas, caches and off-peak batch relieve the primary over months; and splitting data by domain ownership is the multi-year fix. The leader's job is to present these with cost, risk and time and to make sure the long-term path is funded while the quick fixes buy time.
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 start by measuring who uses the database and how, then act on three time horizons at once. In the short term, connection budgets, shorter transactions and a connection proxy such as PgBouncer buy headroom; in the medium term, reads move to replicas and caches and batch work moves off peak; in the long term, data is split by domain ownership, which is a multi-year programme. I present these options with cost, risk and time, and fund the long path while the quick fixes buy the time it needs.
Background to this question
A shared database that is near both its connection limit and its CPU limit is one of the most dangerous situations a platform can be in, because one bad day takes down every service that depends on it. It is also a decision that tests senior leadership: the tempting answers, buy a bigger machine or rewrite everything into microservices, are both wrong on their own. In this answer, we will see how to measure the problem first, then walk through the short, medium and long-term options, what each costs and what it fixes, and finally how to present the decision so that it gets funded.
1Two Different Limits: Connections and CPU
It helps to separate the two symptoms, because they have different fixes. The connection limit is about how many clients are attached. In PostgreSQL every connection is a separate server process, so hundreds of service instances, each with a pool of 10 or 20 connections, can reach thousands of connections even when most of them sit idle.
The CPU limit is about the work those connections ask for. Slow queries, missing indexes, reports scanning large tables and long transactions all burn CPU. A connection proxy can fix the first problem completely and do almost nothing for the second. Splitting the database fixes both, but takes years. So the first job is to find out which problem is really pressing.
2Measure Who Uses the Database
Before deciding anything, we build a picture of the load by client. PostgreSQL can tell us most of it, provided each service sets application_name in its connection settings, which is itself a cheap first step.
Notice what the numbers usually reveal. Most connections are idle, held by pools that are larger than they need to be. A handful of queries dominate CPU, and a surprising share often comes from batch jobs and reports rather than user traffic. We classify the workloads into reads, writes and batch, because each class has a different home in the plan.
3Short Term: Budgets, Shorter Transactions and a Proxy
The short-term steps take weeks and buy headroom. Connection budgets per service cap how many connections each client may hold, and we usually find pools can shrink by half. Shortening transactions matters just as much: a transaction that calls another service while holding a connection keeps that connection busy for the whole remote call.
A connection proxy such as PgBouncer sits between the services and the database. In transaction pooling mode it hands a real server connection to a client only for the duration of a transaction, so 3,000 client connections can share 200 server connections. There is a catch: session features such as session-level prepared statements, advisory locks held across transactions and SET commands behave differently, so each service needs testing (recent PgBouncer versions support prepared statements in transaction mode; check your version).
- 300 service podspools of 10: up to 3,000 client connections
- PgBouncer in transaction modemultiplexes clients onto a small server pool
- PostgreSQL primaryabout 200 server connections, CPU unchanged
3 layers, from the top down. Step through them one at a time.
The proxy solves the connection count. It does not remove a single query's CPU cost.
For CPU, the short-term fixes are the slow-query list: add the missing indexes, rewrite the worst queries and stop reports from running on the primary at noon.
4Medium Term: Replicas, Caches and Off-Peak Batch
The medium-term steps take months. Read-heavy traffic moves to read replicas, copies of the database that apply the primary's changes a little later, or to caches. That works only for reads that tolerate slightly stale data, so we agree with each team which reads qualify. Batch work, such as nightly exports and reconciliation, moves to off-peak windows or to a replica dedicated to it.
Each of these removes a class of load from the primary without changing who owns the data. They also teach teams to think about their own share of the database, which prepares them for the long-term step.
5Long Term: Split the Data by Domain Ownership
The structural fix is to stop sharing. Each domain, such as orders, payments or catalogue, gets its own database owned by the team that owns the domain, and other services reach the data through APIs or events instead of joining tables directly. That fixes connections and CPU together and removes the shared blast radius.
It is a multi-year programme, because the hard part is not copying tables but untangling the joins, foreign keys and shared transactions between domains. So we do it one domain at a time, starting with the one that is both heavy and loosely coupled to the rest.
6Presenting the Options and Funding the Long Path
Leaders decide well when options are laid side by side with cost, risk and time. The table below is the shape of what we would present.
The recommendation is usually all of them, in sequence. The short-term fixes are not an alternative to the split; they are what keeps the business running while the split happens. The common failure is to do the quick fixes, feel relief and never fund the long path, so in two years the same meeting happens again with a bigger database. We ask for the long-term programme to be funded as a named line in the roadmap, with milestones, and report progress on the database's headroom every quarter.
| Option | Fixes | Time | Risk |
|---|---|---|---|
| Budgets and smaller pools | connections | weeks | low |
| Shorter transactions, query fixes | connections and CPU | weeks | low |
| Connection proxy (PgBouncer) | connections only | weeks | medium: session features |
| Bigger instance | CPU, for a while | days | low, but cost grows |
| Replicas, caches, off-peak batch | CPU for reads and batch | months | medium: stale reads |
| Split by domain ownership | both, for good | years | high, staged |
7Follow-up questions
When is a bigger instance the right call?
When it buys time cheaply and the plan behind it is real. Scaling up is fast and low risk, and it is often the right first move during a crisis. It becomes a mistake when it is the only move, because the largest instance is a hard ceiling and cost grows faster than capacity near the top of the range.
Why not use a distributed SQL database instead of splitting?
It can be an option for the write-scaling part, but it does not solve the ownership problem: hundreds of services still share one logical database and its schema. It also brings different latency, different failure behaviour and new skills to learn. I would evaluate it as a candidate home for one large domain, not as a way to avoid deciding who owns which data.
How do you choose which domain to split first?
I look for a domain that carries a large share of the load and has few cross-domain joins and transactions. Moving it relieves the most pressure for the least untangling, and it builds the tooling and experience that later moves reuse.
8Conclusion
In this answer, we saw that a shared database near its connection and CPU limits needs measurement before decisions: connections per service, the queries that burn CPU, and the split between reads, writes and batch. Short-term budgets, shorter transactions and a connection proxy buy headroom; replicas, caches and off-peak batch relieve the primary over months; and splitting data by domain ownership is the multi-year fix. The leader's job is to present these with cost, risk and time and to make sure the long-term path is funded while the quick fixes buy time.
Open with
First I would measure, because connections and CPU are different problems. With application_name set per service I can see connections per client in pg_stat_activity, and pg_stat_statements shows which queries burn CPU, and I would classify the load into reads, writes and batch.
Then, if they want more
Usually most connections are idle and a few queries dominate. Then I would work on three horizons. Short term, over weeks: connection budgets per service, smaller pools, shorter transactions, fixing the top queries, and a connection proxy like PgBouncer in transaction mode to cap server connections, accepting that we need to test session features.
Medium term, over months: move tolerant reads to replicas and caches and push batch work off peak or to its own replica. Long term: split the data by domain ownership so each team owns its database, one domain at a time, which is a multi-year programme.
I would present these as a table with what each fixes, cost, risk and time, and recommend them in sequence. The key point I would make to leadership is that the quick fixes buy time for the split, so the split must be funded explicitly, or we will be back in the same meeting in two years.
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 official documentation for the version you use. 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 largest shared database is near its connection and CPU limits because hundreds of...": https://scrivio-interview-library.netlify.app/spring-boot/service-performance/acefff4d3d/