Databases · Lesson 3.1
Sharing the reads
So Simplified now runs a second database server, a replica.
Server A and server B send every write to the first one, the primary.
As the primary adds each change to its write-ahead log, it sends the replica a copy.
So the replica has a copy of every row, but it's read-only: it refuses every write.
Now say a Forbes article brings a rush of new learners.
Server A and server B send far more queries, and all of them still go to the primary.
Most of them only read.
Opening Courses reads your progress, and so does opening a lesson, or the dashboard.
Far fewer write: an answer you give, or a lesson you finish.
Say that's 9,000 reads a second, and just 500 writes a second.
The primary answers them one after another, so Courses starts loading slowly.
But how can the primary keep up with 9,500 queries a second on its own?
A replica has a copy of every row, so it can answer any read.
So Simplified sends every write to the primary, and every read to a replica.
And it adds two more replicas, so three of them share the reads.
The primary sends each change on to all three.
Replicas used like this are called read replicas.
Read replicas are replicas that take the reads, so the primary takes only the writes.
So how many reads does each read replica take?
The 9,000 reads a second are shared by the 3 read replicas.
9,000 divided by 3 is 3,000 reads a second for each.
And the primary takes just the 500 writes a second.
So if reads grow again, Simplified adds another read replica.
Writes go to the primary, and the reads are shared among the read replicas.
Say Simplified gets 12,000 reads a second and 600 writes a second. It runs a primary and 4 read replicas, which share the reads equally.
How many reads a second does each read replica take?
Explanation
First, every read goes to a read replica, and the 4 of them share the reads equally.
Then, 12,000 divided by 4 is 3,000.
Therefore, each read replica takes 3,000 reads a second, and the primary takes the 600 writes.
Say Instagram sends every new post to its primary, and shares feed views among its read replicas. Feed views grow fast, while new posts stay about the same.
What might Instagram do?
Databases · Lesson 3.2
Does a write wait for the copies?
So when Ada finishes lesson 3, server A sends her transaction to the primary.
And Postgres replies COMMIT only once the COMMIT is in its write-ahead log, on its disk.
So does the primary also wait for the read replicas, before it replies COMMIT?
Server A sends Ada's transaction, and the primary sets her XP to 130.
It adds that change to its write-ahead log, and sends the replica a copy.
Then it adds the COMMIT, so the change is safe on its own disk.
And it replies COMMIT to server A straight away, without waiting for the replica.
The replica gets its copy, and applies it, a moment later.
This is called asynchronous replication.
Asynchronous replication means the primary replies COMMIT without waiting for the replicas, which get the change a moment later.
So does Ada wait any longer for her COMMIT than with one database server?
No: the primary waits only for its own disk, as one database server did.
The replica may get the change a moment after Ada already has her COMMIT.
So her COMMIT is as fast as on one machine.
Asynchronous replication is Postgres's usual way.
With asynchronous replication, Ada's COMMIT never waits for a replica.
Ben has 40 XP on Simplified's primary and on a read replica. Ben finishes a lesson worth 10 XP. The primary sets his XP to 50, then adds the change and the COMMIT to its write-ahead log, on its disk. Simplified uses asynchronous replication.
What does the primary do right after the COMMIT is on its disk?
Explanation
First, with asynchronous replication, the primary waits only for its own disk.
Then, it doesn't wait for the replica, which gets its copy of Ben's change a moment later.
Therefore, the primary replies COMMIT to the server straight away.
Simplified's primary uses asynchronous replication, and sends the replica a copy of each log entry as it writes it. Ada finishes lesson 3, and the primary replies COMMIT.
Which of these is sure to be true at the moment it replies?
Say Simplified's primary takes 3 milliseconds to get a COMMIT into its own write-ahead log, on its disk. Its replica then takes another 40 milliseconds to get the change and apply it.
With asynchronous replication, how many milliseconds after the COMMIT reaches the primary does the primary reply COMMIT?
Simplified's primary has 3 read replicas, and uses asynchronous replication. Ada's COMMIT reaches the primary.
How many of the read replicas must have her change before the primary replies COMMIT?
Simplified uses asynchronous replication. Ada had 120 XP. Her lesson's COMMIT reaches the primary, and the primary replies COMMIT. Just then, the replica still has her at 120.
Why?
Simplified's primary replies COMMIT once the COMMIT is in its own write-ahead log, without waiting for its replicas, which get the change a moment later.
What is this called?
Simplified moved from one database server to a primary with read replicas, using asynchronous replication.
Why does Ada's COMMIT take no longer than before?
Databases · Lesson 3.3
Why your new comment vanishes
So the primary replies COMMIT to Ada's lesson 3 straight away.
And it doesn't wait for the replica, which gets its copy a moment later.
So how far behind the primary is the replica?
Ada's COMMIT reaches the primary's log at 0 milliseconds, so the primary has her 130.
As it wrote the entry that sets Ada's XP to 130, it sent the replica a copy.
Say that copy takes 50 milliseconds to arrive and be applied.
Until then, the replica still has Ada at 120.
That delay is called replication lag.
Replication lag is how long a change takes to reach a replica, after the primary has it.
It's often around 50 milliseconds, and sometimes seconds, when the primary is very busy.
But what does Ada see, if her dashboard reads from the replica during that lag?
At 20 milliseconds, her dashboard reads her XP from the replica.
The change hasn't arrived yet, so the replica sends back 120.
So Ada sees 120, though she has just earned 10 XP.
At 80 milliseconds, she refreshes, and the replica sends back 130.
A read like the one at 20 milliseconds is called a stale read.
A stale read is a read from a replica that returns a value the primary has already changed.
Now why might your new comment vanish for a moment?
Say you comment on a friend's Instagram post, and Instagram writes it to its primary.
You refresh at once, and the page reads the comments from a read replica.
The replica hasn't got your comment yet, so it isn't there.
A moment later, you refresh again, and there it is.
A replica that is behind sends back the old value, until the change arrives.
Simplified's replication lag is 50 milliseconds. Ben has 40 XP. The COMMIT for his lesson, worth 10 XP, reaches the primary's log at 0 milliseconds. At 30 milliseconds, his dashboard reads his XP from a read replica.
What XP does his dashboard show?
Explanation
First, the change reaches the replica 50 milliseconds after the primary has it.
Then, 30 milliseconds is before that, so the replica still has 40.
Therefore, his dashboard shows 40 XP: a stale read.
Simplified's replication lag is 50 milliseconds. Dee has 300 XP. The COMMIT for her lesson, worth 20 XP, reaches the primary's log at 0 milliseconds. At 70 milliseconds, her dashboard reads her XP from a read replica.
What XP does it show?
Say Simplified's primary is very busy, and the replication lag is 2 seconds. Eli has 40 XP. The COMMIT for his lesson, worth 10 XP, reaches the primary's log at 0 seconds. His dashboard reads his XP from a read replica, again and again.
What is the earliest time, in seconds, that it can show 50?
Fay's XP changes from 75 to 85 on Simplified's primary at 0 milliseconds. The replication lag is 50 milliseconds.
Which of these reads is a stale read?
Simplified's replication lag is 50 milliseconds. Ben has 40 XP. The COMMIT for one lesson, worth 10 XP, reaches the primary at 0 milliseconds. The COMMIT for another, also worth 10 XP, reaches it at 30 milliseconds. At 60 milliseconds, his dashboard reads his XP from a read replica.
What XP does it show?
Say Instagram writes every change to its primary, and serves profile views from read replicas. You change your bio, then open your profile at once, and see your old bio. A moment later, you see the new one.
What happened?
Simplified's primary has Ada's new XP at 0 milliseconds, and the change reaches a read replica 50 milliseconds later.
What is that delay called?