

Monotonic Reads | Arpit Bhayani
source link: https://arpitbhayani.me/blogs/monotonic-reads
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Asynchronous replication leads to a fascinating situation where it feels like we are going through a wormhole traveling back and forth in time. In this essay, we understand why this happens and the consequences and devise a quick solution to address it.
Through the wormhole
As per Wikipedia, a wormhole can be visualized as a tunnel with two ends at different points in spacetime (i.e., different locations, different points in time, or both), allowing us to traverse back and forth in time again and again. So, where exactly is a wormhole in the context of a distributed datastore?
Say, we have a distributed KV store having one Master and 2 Replica nodes, and we make three updates on a key X
, the first update U1
sets X
as 1
, the second update U2
sets it to 2
, while the third update U3
sets X
to 3
. Like in a typical Master Replica setup, the writes go to the Master, and they are propagated to Replicas through an Asynchronous replication. The reads are typically sent to any one of the Replicas at random.
The writes are propagated to the Replicas asynchronously, which means both the Replicas will have slight replication lags and say this lag on Replica 1 is of 2 seconds
, and on Replica 2 is 1 second
. As of current time instant, all the three updates U1
, U2
, and U3
have happened on the Master, while only update U1
has reached Replica 1, and it is lagging behind Replica 2 that saw updates U1
and U2
.
Say, after making the update U3
at instant t
, the User initiates a read that hits Replica 2. Since the update U3
is yet to reach the Replica 2, it returned 2
, an old value of X
. This breaks Read your write consistency and make the user feel that the recent write is lost. Say the user makes another read after this one, which now reaches Replica 1, and since the Replica 1 has just seen the update U1
, it returns the value 1
, which is even older than the last returned value.
Here we see that after the latest write U3
, the two successive reads yielded historical values depending on which Replica it hit, giving a feel of traveling back in time. The situation becomes even more interesting when the Replica starts to catch up. Depending on which Replica the read request went to, the User would be oscilating between the old and new values of X
, giving it a feel of going through the wormhole.
Monotonic Reads
Monotonic read guarantees users to see value always moving forward in time, no matter how many or how quickly it tries to read the data. It is a weaker guarantee than strong consistency but a stronger one than eventual consistency.
Achieving Monotonic Reads
The root cause of this seemingly random fetch lies in allowing the read request to hit Replicas with different Replication Lags. For a particular Replica, the writes are always applied in order, moving forward in time. So, a niche solution for this problem is to make the read request of a user sticky to a replica.
Once it is ensured that a particular user's request only goes to a specific replica, that User will see updates always moving forward in time as the Replica continues to catch up with the Master.
To implement stickiness, the server can pick the Replica using the hash of the User ID instead of picking it randomly. This way, the stickiness between a user and a Replica helping us achieve Monotonic Reads.
Recommend
-
10
Thoughts, wonderings, and philosophies.There is no direct correlation between education and degree. There will always be a missed opportunity.
-
19
470+ Competitive Programming Solutions During winter vacation at IIIT Hyderabad, around December 2013, I was addicted to solving competitive programming questions on sites li...
-
8
Replication Strategies 3 min read 773 reads Published on 10th Aug 2021...
-
6
Image Steganography 10 min read 292 reads Published on 17th Jan 2020...
-
5
Consistent Hashing 14 min read 1215 reads Published on 24th May 2020...
-
10
Arpit BhayaniIn this essay, we explore a simple yet effective DoS attack called Fork Bomb, also called Rabbit Viru...
-
10
Changing Python 7 min read 1104 reads Published on 3rd Jan 2020 ...
-
8
Replication Formats 4 min read 321 reads Published on 15th Aug 2021
-
9
Conflict ResolutionEvery multi-master replication setup comes with a side-effect - Conflicts. Conflict happens when two or more database accepts conflicting updates on the same record. We say that the updates are conflicting when we cann...
-
7
Israeli QueuesA queue is a data structure that holds up elements for a brief period of time until a peripheral processing system is ready to process them. The most common implementation of a queue is a FIFO queue - First In First Out - t...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK