How would you feel when you posted on a Social

Arpit Bhayani

Arpit Bhayani

Sep 22, 2021 β€’ 3 min read


How would you feel when you posted on a Social Platform, and upon refreshing it, it gave you a 404? This is a classical case of your datastore missing Read-your-write Consistency.

In essay #64, we skim this problem, understand Read-your-write consistency, and look at potential solutions to it. Here’s a short snippet

In a Master-Replica setup, the Writes happening on the Master take some time to reach the Replica. When the client issues the Read on a Replica that has yet to receive the write, it leads to an undesirable behavior wherein the client will see the old value (or null) and think that the write it made was lost.

A few more examples of why we need Read-Your-Write consistency

πŸ™ˆ Imagine getting a match on Tinder and disappearing upon refresh πŸ™ˆ Imagine buying an AAPL Stock and seeing no trace of it on the orders page πŸ™ˆ Imagine adding items to your Amazon cart and realizing it empty when placing the order

Let’s find out how to have a Read-your-write consistency in your system.

πŸ’₯ Synchronous Replication Replication Lag exists because the writes are propagated to Replica asynchronously. But if we switch to synchronous replication, all Replicas will always be incomplete sync with the Master. So we would always get RYW Consistency.

πŸ’₯ Pinning User to Master Instead of routing all the reads from all the users to the Master, what if we routed reads of the User who recently performed the Write to the Master? This sounds promising and addresses our concern, and this exactly is Pinning the User to the Master.

πŸ’₯ Fragmented Pinning Pinning a user to the Master would mean queries, both Read and Write, made by the user will hit the Master for a configured time window. But instead of pinning everything, what if we pick only a few critical reads to hit the Master; this is Fragmented Pinning.

πŸ’₯ Master Fallback There is no User Pinning in this approach, and all the Read operations go to the Replica while the Master node only handles Write. The Master and Replica are kept in sync using asynchronous replication. If the Read request that went to the Replica resulted in the 404, i.e., Key Not Found, the application forwards the same query on the Master node and then returns the response.

To gain a detailed understanding of the topic, I highly recommend you reading the long-form detailed essay with illustrations:

πŸ‘‰ https://lnkd.in/dfk-gESQ


If you want to learn how real systems are built and managed, enroll in my 5th cohort on System Design that starts November 6th. 15 seats are already gone, 40 more to go.

We discuss and build some of the most amazing systems and in a very detailed way, not just drawing random boxes just to add components and look smart. We build it the way it should have been built.

You can find the week-by-week curriculum and topics, benefits, testimonials, and other details at

https://lnkd.in/dtBk7eE.

✨ It’s time for #AsliEngineering ✨

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses