Load-Balancing Database CPU: Diagnosing Hot Servers and Spreading Query Load
Company: Mercor
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
How would you balance CPU load across a database? The question was asked in a single line during a rapid-fire fundamentals call. Assume a relational database whose CPU is running hot, or is unevenly loaded across its servers, and explain how you would spread the load, starting from how you would find out where the CPU is going.
```hint Find what is burning the CPU
Before spreading load, decide how you would tell whether the CPU goes to a few expensive queries, to many cheap ones, or to handling connections; each points to a different fix.
```
```hint Reads and writes spread differently
Think about which work a copy of the data can serve and which work must go to the node that owns the data.
```
### Clarifying Questions
- Is the database a single server with its CPU saturated, or a cluster where some nodes run much hotter than others?
- Is the load mostly reads or mostly writes, and is it spread across many tables and users or concentrated on one table, key or tenant?
- Which reads can tolerate slightly stale data, and which cannot?
- Is it a managed cloud database or self-hosted, and can the application change its queries?
### What a Strong Answer Covers
- Diagnosing where the CPU goes before scaling anything
- Reducing the load itself: query and index fixes, caching, connection pooling
- Spreading reads across replicas and handling replication lag
- Spreading writes by partitioning or sharding, with hot keys and rebalancing addressed
- Routing queries to the right node, health checks, and the metrics that show the change worked
### Follow-up Questions
- A user updates their profile and immediately sees the old version because the read went to a lagging replica. How do you fix that without sending every read to the primary?
- One tenant generates most of the load on one shard. What do you do?
- When is a bigger machine the better answer than spreading load across several?
- Should read and write routing live in the application, in a proxy, or in the database driver? What are the trade-offs?
Overview: Explain how to balance CPU load on a relational database whose servers are running hot. It tests how you diagnose where the load comes from, how you weigh options for reducing it and for spreading reads and writes across servers, and how you handle replication lag and uneven load between nodes.