Simplified
Simplified

L3.1Sharing the reads

YOU

Sharing the reads

server Aserver Bprimaryusersidnamexp1Ada120write-ahead logusers: set Ada's xp to 120users: set Ada's xp to 120replicausersidnamexp1Ada120read-onlywrite
server Aserver Bnew learners9,500 queries a secondprimaryusersidnamexp1Ada120replicausersidnamexp1Ada120read-onlyreadsopening Coursesopening a lessonopening the dashboardwritesan answer you givea lesson you finish12345?
server Aserver Bwritesprimaryusersidnamexp1Ada120replica 1usersidnamexp1Ada120replica 2usersidnamexp1Ada120replica 3usersidnamexp1Ada120reads9,000 ÷ 3 = 3,000read replicas3,000 a second3,000 a second3,000 a second?
server Aserver Bwritesprimaryusersidnamexp1Ada120replica 1replica 2replica 3replica 4reads

Databases · Lesson 3.1

Spreading the Reads

Sharing the reads

Simplified
0:00 / 1:46

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 Aserver Bnew learners9,500 queries a secondprimaryusersidnamexp1Ada120replicausersidnamexp1Ada120read-only9,000 reads a secondopening Coursesopening a lessonopening the dashboard500 writes a secondan answer you givea lesson you finish12345

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?

server Aserver Bwritesprimaryusersidnamexp1Ada120replica 1usersidnamexp1Ada120replica 2usersidnamexp1Ada120replica 3usersidnamexp1Ada120reads9,000 ÷ 3 = 3,000read replicas3,000 a second3,000 a second3,000 a second

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?

server Aserver B500 writes a secondprimaryusersidnamexp1Ada120replica 1usersidnamexp1Ada120replica 2usersidnamexp1Ada120replica 3usersidnamexp1Ada1209,000 reads a second9,000 ÷ 3 = 3,000read replicas3,000 a second3,000 a second3,000 a second

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.

Example: Sharing the reads

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.

Question 1

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?

Does a write wait for the copies?

server Aserver BAda's transactionprimaryusersidnamexp1Ada120write-ahead logusers: set Ada's xp to 130COMMITCOMMITusers: set Ada's xp to 130COMMITreplicausersidnamexp1Ada120read-onlyasynchronous replication?
server Aserver BAda's transactionprimaryusersidnamexp1Ada120write-ahead logusers: set Ada's xp to 130COMMITCOMMITusers: set Ada's xp to 130COMMITreplicausersidnamexp1Ada120read-onlyasynchronous replication?
one database serverwrite-ahead log, on diskCOMMITprimarywrite-ahead log, on diskCOMMITreplicaasynchronous replication

Databases · Lesson 3.2

Spreading the Reads

Does a write wait for the copies?

Simplified
0:00 / 1:12

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 Aserver B1Ada's transactionprimaryusersidnamexp1Ada130write-ahead log2users: set Ada's xp to 130COMMIT4COMMIT3users: set Ada's xp to 130COMMITreplicausersidnamexp1Ada120read-onlyasynchronous replication

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?

one database serverwrite-ahead log, on diskCOMMITprimarywrite-ahead log, on diskCOMMITreplicaasynchronous replication

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.

Example: Does a write wait for the copies?

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.

Question 1

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?

Question 2

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?

milliseconds

Question 3

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?

read replicas

Question 4

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?

Question 5

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?

Question 6

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?

Why your new comment vanishes

server Aserver BAda's transactionprimaryusersidnamexp1Ada130write-ahead logusers: set Ada's xp to 130COMMITCOMMITusers: set Ada's xp to 130COMMITreplicausersidnamexp1Ada120read-onlyasynchronous replication?
primary120COMMITusers: set Ada's xp to 130replica120Ada's dashboard120130stale read0 msreplication lag: 50 mssometimes seconds?
Instagram's serveryour commentprimarycommentsidby8Ben9youcomments: add (9, you)replicacommentsidby8Ben9youread-onlyrefresh?
primary120130COMMITusers: set Ada's xp to 130replica120130Ada's dashboard120130stale read0 ms20 ms50 ms80 msreplication lag: 50 mssometimes seconds

Databases · Lesson 3.3

Spreading the Reads

Why your new comment vanishes

Simplified
0:00 / 1:43

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?

primary120130COMMITusers: set Ada's xp to 130replica120130Ada's dashboard120130stale read0 ms50 ms50 mssometimes seconds

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?

primary120130COMMITusers: set Ada's xp to 130replica120130Ada's dashboard120130stale read0 ms20 ms50 ms80 msreplication lag: 50 mssometimes seconds

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?

Instagram's serveryour commentprimarycommentsidby8Ben9youcomments: add (9, you)replicacommentsidby8Ben9youread-onlyrefresh

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.

Example: Why your new comment vanishes

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.

Question 1

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?

XP

Question 2

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?

seconds

Question 3

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?

Question 4

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?

XP

Question 5

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?

Question 6

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?

Assistant

Highlight lesson text and click Ask AI, or type a question below.