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:
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
β¨ Itβs time for #AsliEngineering β¨