Scaling and changing databases · senior
Scaling a busy database step by step
Before a big traffic event, measure first, then use the safest fixes first: queries, cache, pooling, read replicas, a bigger machine. Partitioning and sharding come last.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Measure first: pg_stat_statements sorted by total time. A few queries are usually most of the load.
- Order of work, from low risk to high: fix top queries (indexes, N+1, fewer columns); cache hot reads (Redis, CDN); pool connections; move reports off the primary; add read replicas; scale the instance up; then partition or shard.
- Replicas help reads that can be a little old. They hurt read-after-write paths and add lag under heavy writes.
- Scaling up is fast but has a ceiling and a price, and failover takes longer on huge instances.
- Writes: batch them, queue non-urgent ones, check WAL volume, checkpoints and disk IOPS, and look for hot rows such as inventory counters.
- Sale-day switches: feature flags to turn off recommendations, live counts and heavy filters.
- Readiness: a load test with production-sized data and the traffic shape of the sale, a game day and a runbook.
The red pen
Common traps: mistake → fix
Mistake: Shard now
Fix: it is the most costly step; try the cheaper steps first.
Mistake: Add replicas without fixing the top query
Fix: the same slow query runs on more machines.
Mistake: No load test is needed
Fix: without one you only guess.
Easy to hard
Questions interviewers ask, easy to hard
- Your primary is at 85% CPU and traffic will be 4 times higher in three weeks. What is your plan?
- How do you find the heaviest queries?
- When do replicas help and when do they hurt?
- What do you do last, and why?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.