What if your website never slowed down or crashed, no matter how many visitors came?
Why Database replication (master-slave) in HLD? - Purpose & Use Cases
Imagine running a busy online store where all customer orders are saved in a single database. When many people shop at once, the database gets overwhelmed, slowing down the whole site and making customers wait.
Using just one database means every read and write request waits in line. If the database crashes, the whole store stops. Fixing this manually is slow and risky, causing lost sales and unhappy customers.
Database replication with master-slave setup copies data from one main database (master) to one or more copies (slaves). Reads can happen on slaves, spreading the load and keeping the system fast and reliable even if one database fails.
all_reads_and_writes_to_master()
write_to_master(); read_from_slave();
This lets your system handle many users smoothly and stay online even if one database has problems.
Popular social media platforms use master-slave replication to show posts quickly to millions while safely saving new posts without delay.
Single database struggles with heavy traffic and risks downtime.
Master-slave replication spreads read load and improves reliability.
It keeps applications fast and available for many users.
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
