Databases · Lesson 4.1
One like count, two numbers
So when Ada's dashboard asked the replica at 20 milliseconds, it got 120, and at 80 milliseconds, 130.
But that was one replica, and Simplified runs several.
So can Simplified count on every replica catching up?
As the primary writes the entry that sets Ada's XP to 130, it sends each read replica a copy.
Say replica 1 applies it at 40 milliseconds, and replica 2 at 60.
Each keeps applying every entry, so by 80 milliseconds, either replica returns 130.
So once the lag has passed, every replica has the latest write.
This is called eventual consistency.
Eventual consistency means reads from replicas may be stale for a moment, but eventually return the latest write.
And the primary always has the latest write, because every write goes to it first.
Now why might a like count show one number on your phone, and another on your laptop?
Say your Instagram post has 41 likes, and a friend likes it.
Instagram's primary writes 42, and sends the change on to its read replicas.
Each replica gets the change on its own, so one can get it first.
Your phone reads from one replica, which already has the change: 42.
Your laptop reads from another, still in its lag: 41.
A moment later, both replicas have the change, and both show 42.
A read from a replica can be a moment behind, but it always catches up.
Simplified's replication lag is 50 milliseconds. Ben has 40 XP, and the COMMIT for his lesson, worth 10 XP, reaches the primary at 0 milliseconds. His dashboard reads his XP from a read replica at 30 milliseconds, and gets 40. Then it reads again, from the same replica, at 120 milliseconds.
What XP does the second read return?
Explanation
First, the change reaches the replica 50 milliseconds after the primary has it.
Then, 120 milliseconds is after that, so the replica has applied it: 50 XP.
Therefore, the second read returns 50 XP: the replica has caught up.
Simplified's read replicas can return a stale read during the replication lag.
Why does a read replica eventually return the latest write?
Databases · Lesson 4.2
Seeing what you just did
So once the 50 milliseconds had passed, Ada's dashboard got her 130 from a replica.
But at 20 milliseconds, it got 120.
And Ada has just finished lesson 3, so she's looking for her 10 XP.
If her dashboard shows 120, it looks like her XP was lost.
And when the primary is very busy, the lag can be seconds.
So how can Ada see her own new XP at once, not 50 milliseconds later?
Simplified's server handled Ada's write, so it knows she has just written.
So for a short time after a learner writes, it sends their reads to the primary.
Say 1 second, far longer than the usual 50-millisecond lag.
So when her dashboard reads at 20 milliseconds, it goes to the primary.
The primary always has the latest write, so it sends back 130.
This is called read-your-writes.
Read-your-writes means a user always sees their own writes.
But doesn't that put the reads back on the primary?
Only Ada's reads go there, and only for that 1 second.
Everyone else keeps reading from replicas.
Ben opens Courses at 20 milliseconds.
He hasn't written in the last second, so his read goes to a replica.
Nothing of his has changed, so the replica's copy is all he needs.
So the primary still carries only a small part of the reads.
Just after a learner writes, their own reads go to the primary, so they see their writes.
Simplified sends a learner's reads to the primary for 1 second after they write, and its replication lag is 50 milliseconds. Ben has 40 XP, and the COMMIT for his lesson, worth 10 XP, reaches the primary at 0 milliseconds. At 30 milliseconds, his dashboard reads his XP.
What XP does Ben's dashboard show?
Explanation
First, Ben wrote at 0 milliseconds, so until 1 second, his reads go to the primary.
Then, the primary always has the latest write, so it has his 50.
Therefore, Ben's dashboard shows 50 XP, though a replica would still say 40.
Say Instagram sends a user's reads to its primary for 1 second after they write, and its replication lag is 50 milliseconds. You comment on your post at 0 milliseconds. At 30 milliseconds, your friend, who hasn't written anything in the last second, refreshes your post.
Does your friend see your comment?
Simplified sends a learner's reads to the primary for 1 second after they write, and its replication lag is 50 milliseconds. Ada's XP goes from 120 to 130 at 0 milliseconds. At 2 seconds, her dashboard reads her XP from a read replica.
What XP does it show?
Simplified sends a learner's reads to the primary for 1 second after they write. Fay finishes a lesson, and the write reaches the primary at 0 milliseconds. At 500 milliseconds, Fay's dashboard reads her XP.
Where does Simplified's server send that read?
Simplified sends a learner's reads to the primary only for 1 second after they write.
Why not send every learner's reads to the primary all the time?
Simplified uses read-your-writes, and its replication lag is 50 milliseconds. Ada finishes a lesson, and her XP goes from 120 to 130 on the primary.
Which of these is it sure to give, straight away?
For a short time after a learner writes, Simplified sends that learner's reads to the primary, so they always see their own writes.
What is this promise called?