Quick revision
The short answer
Doubling a load that already uses 70 percent CPU would need 140 percent, so the plan must either halve the cost per request or add capacity, and the first step is to profile where the CPU goes with pg_stat_statements, the read and write mix, cache hit rate, I/O wait and connections. Then we compare options from cheapest to most invasive: query and index tuning, caching, PgBouncer, moving reads and reports to replicas, a larger instance, and for writes, splitting workloads or sharding. A load test with production-like data shows the real limit, and a trigger such as sustained 60 percent starts the next step.
What to remember
In this answer, we saw that 70 percent CPU at peak leaves little headroom because queueing grows fast near saturation, so doubling traffic needs either halving the cost per request or adding capacity. We profiled with pg_stat_statements, the read and write mix, cache hit rate and connections, then compared options from tuning, caching and PgBouncer through read replicas and vertical scaling to splitting workloads or sharding for writes. A load test with production-like data finds the real limit, and triggers such as sustained 60 percent start each next step in 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.
Doubling a load that already uses 70 percent CPU would need 140 percent, so the plan must either halve the cost per request or add capacity, and the first step is to profile where the CPU goes with pg_stat_statements, the read and write mix, cache hit rate, I/O wait and connections. Then we compare options from cheapest to most invasive: query and index tuning, caching, PgBouncer, moving reads and reports to replicas, a larger instance, and for writes, splitting workloads or sharding. A load test with production-like data shows the real limit, and a trigger such as sustained 60 percent starts the next step.
Why this question is asked
Nine months sounds like plenty of time, but the slow options, such as sharding or splitting a database, can take most of it, and a primary database at its CPU limit does not degrade gently: latency climbs sharply and timeouts cascade into every service that depends on it. Interviewers ask this design question to see whether we measure before we buy, know what each scaling option really fixes, and can sequence them against a deadline. In this answer, we will recap why 70 percent is already close to the edge, profile the load, go through the options with their costs, and turn it into a plan with load tests and triggers.
1A Quick Recap: Why 70 Percent Is Not 30 Percent of Headroom
CPU utilisation is the share of time the processors are busy. As it rises, requests start waiting for a free core, and queueing makes waiting grow much faster than utilisation: at 50 percent a query rarely waits, at 85 to 90 percent waits can be several times the query's own time. So a database at 70 percent at peak has room for perhaps 15 to 20 percent more load before users notice, not for a doubling. Doubling the traffic at the same cost per request would need 140 percent of today's machine, which means we need to roughly halve the CPU cost per request, double the capacity, or combine both.
2Profile Where the CPU Goes
Before choosing any option we find out what the CPU is spent on, because the right fix depends entirely on that. The pg_stat_statements extension records, for each normalised query, how often it ran and how much time it used in total.
In this example, two queries use half of all execution time, which is very common. We also look at the read versus write ratio, from rows fetched against rows inserted and updated in pg_stat_database, because it decides whether replicas can help. The cache hit rate tells us whether the working set fits in memory; a falling rate means more disk reads and more I/O wait. And connection counts matter: hundreds of active connections cost CPU in context switching and lock contention by themselves.
3The Cheap Wins First
Tuning is usually the best return. The top query might be missing an index or running a plan that reads far more rows than it returns, and fixing that can remove a third of the load in a day. Caching helps for data that is read far more often than it changes, such as product catalogues and prices, through an application cache or a shared cache like Redis, with the cost of invalidation logic. Connection pooling with PgBouncer in transaction mode lets thousands of client connections share a few dozen database connections, which lowers CPU wasted on idle and competing backends. And moving reporting queries and other heavy reads to a read replica takes them off the primary entirely.
These steps are cheap in time and money, so we do them first, but we measure the gain after each one rather than assuming it.
| Option | Helps with | Cost and limits | Lead time |
|---|---|---|---|
| Query and index tuning | inefficient top queries | engineering time only | days |
| Caching | hot, rarely changing reads | invalidation, stale data | weeks |
| PgBouncer | many connections | transaction mode limits session features | days |
| Read replicas | read-heavy load, reports | replication lag, reads only | days to weeks |
| Larger instance | everything, for a while | cost steps, hard upper limit | hours to days |
| Split workloads or shard | write-heavy load | big application changes | months |
4Scaling Up and Out
Vertical scaling, moving to a bigger instance, is the fastest real capacity increase and needs no application change, but it has limits. Cloud instance sizes come in steps where the price roughly doubles each time, the largest size is a hard ceiling, and doubling cores does not double throughput if the bottleneck is lock contention or a single hot table. It buys time; it is rarely the whole plan.
Read replicas add capacity only for reads, and the application must accept that replica data can be slightly behind, by milliseconds normally and by seconds or more under load. Reads that must see the user's own just-written data, such as an order confirmation page, should still go to the primary. If the load is 80 percent reads, replicas can relieve the primary a lot; if it is mostly writes, they barely help.
5Scaling Writes
Writes all go to the single primary, so scaling them means reducing them or spreading them over more than one primary. Reducing them means batching, removing unneeded indexes, which make every write more expensive, and avoiding write amplification such as updating a row on every page view. Spreading them means splitting the database by workload, for example moving inventory or audit data to its own database, or sharding by tenant or another key, either in the application or with a distributed extension such as Citus. Note that table partitioning on one server helps maintenance and pruning but does not add CPU; only spreading data over several servers does. Sharding is the most powerful and the most invasive option, with months of work and cross-shard queries to redesign, which is why the decision must be made early in a nine-month window.
- Profile the loadtop queries, read/write mix, cache hit, connections
- Top queries inefficient?YesTune queries and indexes firstNoLook at the read share
- Mostly reads?YesCaching and read replicasNoLook at write reduction and splitting
- Load test with production-like datafind the real limit after each change
- Sustained peak over 60 percent?YesStart the next step nowNoKeep monitoring the forecast
5 steps in order. Play them, or step through them one at a time.
6Load Test and Set Triggers
Estimates of gains are guesses until tested. We load test against a copy of production data at full size, because plans and cache behaviour change with data volume, replaying a realistic mix of queries at increasing rates until latency targets break. That gives the real limit, for example 1.6 times today's peak after tuning, and shows which resource fails first.
Then we write the plan with dates and triggers: the tuning and PgBouncer work in month one, replicas for reports in month two, and a decision by month three on whether the write share requires splitting a workload, which must start then to finish in time. A trigger such as peak CPU sustained above 60 percent for a week, or a forecast crossing 70 percent within 90 days, starts the next step automatically instead of waiting for an incident.
7Follow-up questions
Why not just buy the largest instance and be done?
It may work for nine months, but it is the most expensive option, it does not fix inefficient queries that will grow with traffic, and it leaves no next step once we are at the top size. Tuning first often removes the need for the biggest jump.
How do you know replicas will actually help?
From the profile: if most CPU time is in reads that tolerate slight staleness, moving them to replicas relieves the primary. We confirm by routing a share of that traffic to a replica in a test or canary and measuring the primary's CPU drop.
What would make you choose sharding early?
A write-heavy profile where tuning and write reduction leave the primary above about 50 percent at today's load, combined with growth that continues beyond the nine months. Then the lead time of sharding is the deciding factor, and starting late is the real risk.
8Conclusion
In this answer, we saw that 70 percent CPU at peak leaves little headroom because queueing grows fast near saturation, so doubling traffic needs either halving the cost per request or adding capacity. We profiled with pg_stat_statements, the read and write mix, cache hit rate and connections, then compared options from tuning, caching and PgBouncer through read replicas and vertical scaling to splitting workloads or sharding for writes. A load test with production-like data finds the real limit, and triggers such as sustained 60 percent start each next step in time.
Open with
First I would point out that doubling a load that runs at 70 percent means 140 percent, and that latency already climbs sharply above about 80 percent, so I need to cut cost per request, add capacity, or both.
Then, if they want more
I would start by profiling: pg_stat_statements for the queries using the most total time, the read versus write ratio, the cache hit rate, I/O wait and connection counts.
Usually a few queries dominate, so the cheap wins come first: fix those queries and indexes, cache hot read-mostly data, put PgBouncer in front if there are many connections, and move reports and other heavy reads to replicas, accepting replication lag for those reads.
Vertical scaling is the fastest capacity boost but has price steps and a ceiling, so it buys time rather than solving growth. Replicas only help reads.
If the profile is write-heavy, the real options are reducing writes, splitting a workload into its own database, or sharding by tenant or key with something like Citus or in the application, and since that takes months, I would decide in the first quarter.
Throughout, I would load test with production-sized data to find the real limit after each change, and set a trigger like sustained 60 percent at peak or a forecast crossing 70 percent within 90 days to start the next step.
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
- PostgreSQL documentation, MySQL reference manual
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 "A PostgreSQL primary runs at 70 percent CPU at peak and traffic is expected to double in...": https://scrivio-interview-library.netlify.app/databases/database-operations/e49247a4e0/