| Users / Traffic | Writes per second | Reads per second | Replication Lag | Number of Slaves | Notes |
|---|---|---|---|---|---|
| 100 users | 10 | 50 | Negligible | 1 | Single master, one slave for read scaling |
| 10,000 users | 1,000 | 5,000 | Small, under 1 second | 3-5 | More slaves added to handle read traffic |
| 1,000,000 users | 50,000 | 200,000 | Noticeable, seconds delay | 10-20 | Replication lag increases, master CPU high |
| 100,000,000 users | 1,000,000+ | 5,000,000+ | High, seconds to minutes | 50+ | Master bottleneck, replication lag critical |
Database replication (master-slave) in HLD - Scalability & System Analysis
The master database server is the first bottleneck because it handles all write operations. As traffic grows, the master CPU and disk I/O become overwhelmed. Replication lag to slaves increases, causing stale reads. Network bandwidth between master and slaves can also limit replication speed.
- Read Scaling: Add more slave replicas to distribute read queries.
- Write Scaling: Use sharding to split data across multiple masters.
- Connection Pooling: Reduce overhead by reusing database connections.
- Caching: Use in-memory caches (e.g., Redis) to reduce read load on slaves.
- Asynchronous Replication: Accept some lag to improve master throughput.
- Monitoring: Track replication lag and master load to trigger scaling actions.
Assuming 1 master and 5 slaves at 10,000 users:
- Writes: ~1,000 QPS on master (CPU and disk intensive)
- Reads: ~5,000 QPS distributed across slaves (~1,000 QPS each)
- Replication bandwidth: ~10 MB/s between master and slaves (depends on data size)
- Storage: Master and slaves store full dataset; storage grows with data size
- Network: Master needs high bandwidth to push changes to all slaves
Start by explaining the master-slave model and its purpose (write master, read slaves). Discuss bottlenecks focusing on the master for writes and replication lag. Then propose scaling strategies like adding slaves, sharding, and caching. Always mention trade-offs such as eventual consistency and lag. Use numbers to justify your points.
Your database master handles 1,000 QPS writes. Traffic grows 10x to 10,000 QPS writes. What do you do first and why?
Answer: The master is overwhelmed. First, consider sharding the database to split write load across multiple masters. This reduces load per master and improves write scalability. Adding more slaves won't help write bottleneck.
Practice
Solution
Step 1: Understand master responsibilities
The master database is responsible for handling all write operations to ensure data consistency.Step 2: Understand slave responsibilities
Slaves replicate data from the master and typically handle read requests to reduce load on the master.Final Answer:
Handle all write operations and propagate changes to slaves -> Option AQuick Check:
Master = writes + replication [OK]
- Thinking slaves handle writes
- Confusing backup role with master
- Assuming master only monitors
Solution
Step 1: Configure master first
The master must be configured first to accept writes and log changes for replication.Step 2: Configure slaves after master
Slaves are then configured to connect to the master and replicate its data.Final Answer:
Configure master, then configure slaves, then start replication -> Option DQuick Check:
Master setup before slaves [OK]
- Configuring slaves before master
- Starting replication before configuration
- Ignoring master configuration
Solution
Step 1: Understand replication lag effect
Replication lag means slaves are behind the master by 2 seconds, so recent writes are not yet applied.Step 2: Analyze read request timing
A read immediately after a write will hit the slave before replication applies the change, so stale data is returned.Final Answer:
The slave returns stale data missing the latest write -> Option CQuick Check:
Replication lag causes stale reads [OK]
- Assuming slaves always have latest data
- Thinking slaves reject reads during lag
- Believing slaves crash due to lag
Solution
Step 1: Identify cause of inconsistent reads
Inconsistent reads usually happen due to replication lag causing slaves to have outdated data.Step 2: Fix by reducing replication lag
Improving monitoring and optimizing replication reduces lag, making slave reads more consistent.Final Answer:
Increase monitoring and reduce replication lag -> Option BQuick Check:
Reduce lag to fix inconsistent reads [OK]
- Allowing writes on slaves breaks consistency
- Disabling replication loses read scaling
- Adding slaves without lag fix doesn't help
Solution
Step 1: Understand master failure impact
If the master fails, writes cannot proceed unless a new master is available.Step 2: Use automatic failover
Promoting a slave to master automatically and redirecting writes ensures availability and minimal data loss.Step 3: Evaluate other options
Stopping writes causes downtime; writing on slaves breaks consistency; single master lacks redundancy.Final Answer:
Automatically promote a slave to master and redirect writes -> Option AQuick Check:
Automatic failover ensures availability [OK]
- Stopping writes causes downtime
- Writing on slaves breaks data integrity
- Ignoring failover leads to outages
Ready to prove it?
Pass the test and this pattern moves unknown
