We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Data Replication Distribution
Free preview: the first part of this 9-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Data Replication Distribution
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
A user on Twitter changes their display name from "John" to "Johnny." They refresh their profile page. It still says "John." They refresh again. Still "John." Three minutes later, it finally shows "Johnny."
What happened? The write went to Twitter's primary database. The profile page was served by a read replica. And that replica was three minutes behind the primary. The user's update existed in the system. It just hadn't propagated to the replica that served the read request yet.
This is lag, and it's one of the most common sources of user-visible bugs in distributed systems. Every company that uses read replicas (which is basically every company at scale) has fought this battle. GitHub, Slack, Shopify, LinkedIn, they've all had outages or bug reports traced back to replication lag.
The frustrating part? The system is working exactly as designed. Replication lag isn't a bug. It's a fundamental trade-off of distributed databases.

The Twitter story above is the extreme version. Here is the ordinary one, measured.
Look at the leftmost point on the red line. A user writes, then immediately reads, and lands on a follower. They miss their own write 100 percent of the time. Not occasionally. Every single time, because no replica has had a chance to hear about it yet.
The curve only starts to fall once the wait passes the replication lag, which is why the median lag line sits right where the steep part is. Before that line you are asking a replica about something it has not been told. After it, you have better than even odds.
That is what makes this so hard to catch in testing. Your test writes and reads back in the same millisecond, so it fails immediately and you notice. A real user clicks Save, the page navigates, and 200 milliseconds pass on their behalf, so they only see it sometimes. The bug you cannot reproduce is the bug that reaches production.
The blue line is the fix that costs you least. Pin a client to the primary for 300 milliseconds after it writes and let it go back to followers afterwards. Ten stale reads out of 2,400 instead of 1,407, and your read fleet still carries almost all the traffic. The green line, reading the primary for everything, has no stale reads at all and no read scaling either, which is the thing you bought replicas for.
One number here deserves suspicion in your own system. The 67.6 ms median came from a lab where one follower was made four times slower than the others. Yours will be different, it will vary by hour, and it will get much worse during exactly the incidents when you need it to behave. Measure it before choosing a sticky window, and alert on it.