Simplified
Simplified

L2.1How a replica gets every change

YOU

How a replica gets every change

Ada: lesson 3+10 XPserver Aserver Bdatabase serverusersidnamexp1Ada120write-ahead logusers: set Ada's xp to 130COMMITreplicausersidnamexp1Ada120write-ahead logusers: set Ada's xp to 130COMMITusers: set Ada's xp to 130COMMITreplication?
your friend's emailGmail's serverdatabase serveremailsidfrom41Netflix42your friendwrite-ahead logemails: add (42, your friend)replicaemailsidfrom41Netflix42your friendwrite-ahead logemails: add (42, your friend)emails: add (42, your friend)?

Databases · Lesson 2.1

Keeping the Copy Current

How a replica gets every change

Simplified
0:00 / 1:22

So Simplified runs a replica: a second database server with a copy of every row.

And the replica is kept up to date, so it has every change the first one makes.

But how does the replica's copy stay up to date, as learners finish lessons?

Ada: lesson 3+10 XPserver Aserver Bdatabase serverusersidnamexp1Ada130write-ahead logusers: set Ada's xp to 130COMMITreplicausersidnamexp1Ada130write-ahead logusers: set Ada's xp to 130COMMITusers: set Ada's xp to 130COMMITreplication

Say Ada finishes lesson 3, worth 10 XP, and server A sends the transaction.

The first database server sets her XP to 130, and adds that change to its write-ahead log.

As it writes that log entry, it sends the replica a copy too.

The replica adds the entry to its own write-ahead log, then makes the same change in its users table.

Then the COMMIT goes into the log, and on to the replica, the same way.

Keeping a replica up to date like this is called replication.

Replication means sending the replica every write-ahead log entry, so it makes every change too.

Now how might Gmail's replica get a new email from your friend?

your friend's emailGmail's serverdatabase serveremailsidfrom41Netflix42your friendwrite-ahead logemails: add (42, your friend)replicaemailsidfrom41Netflix42your friendwrite-ahead logemails: add (42, your friend)emails: add (42, your friend)

Say Gmail keeps your emails on a database server, with a replica of it.

Your friend emails you, so the first database server adds your friend's email.

As it writes that log entry, it sends the replica a copy, so the replica adds it too.

Then say the first database server dies.

Gmail can still show your inbox from the replica, and your friend's email is there.

Replication sends the replica every change, as the first database server writes it.

Example: How a replica gets every change

Simplified's first database server and its replica both have Ben at 40 XP. Ben finishes a lesson worth 10 XP. The first database server sets his XP to 50, adds the change to its write-ahead log, and sends that entry to the replica, which applies it. Then the first database server dies.

What XP does the replica hold for Ben?

Explanation

First, the first database server sent the replica its log entry for Ben's 50, as it wrote it.

Then, the replica made the same change in its own users table.

Therefore, the replica holds 50 XP for Ben, though the first machine has died.

Question 1

Simplified's first database server and its replica both have Eli at 40 XP. Eli finishes two lessons, one worth 10 XP and one worth 20 XP. For each, the first database server writes the change and sends its write-ahead log entry to the replica, which applies it. Then the first database server dies.

How much XP does the replica hold for Eli?

XP

Why only one machine takes writes

Ada: lesson 3+10 XPserver Aserver Bdatabase serverusersidnamexp1Ada130write-ahead logusers: set Ada's xp to 130COMMITreplicausersidnamexp1Ada120write-ahead logusers: set Ada's xp to 130COMMITusers: set Ada's xp to 130COMMITreplication?
server Aserver Aserver Bserver Busers: set Ada's xp to 130database serverusersidnamexp1Ada120read-onlydatabase serverusersidnamexp1Ada120replicausersidnamexp1Ada120read-onlyreplicausersidnamexp1Ada120UPDATE users SET xp = xp + 10WHERE id = 1UPDATE users SET xp = xp + 10WHERE id = 1laptop: lesson 3phone: another lesson
server Aserver Aserver Bserver Busers: set Ada's xp to 130users: set Ada's xp to 140database serverusersidnamexp1Ada130read-onlydatabase serverusersidnamexp1Ada130replicausersidnamexp1Ada130read-onlyreplicausersidnamexp1Ada130UPDATE users SET xp = xp + 10WHERE id = 1UPDATE users SET xp = xp + 10WHERE id = 1?

Databases · Lesson 2.2

Keeping the Copy Current

Why only one machine takes writes

Simplified
0:00 / 1:50

So Simplified has two database servers, and the first sends the replica each log entry as it writes it.

The replica adds it to its own log, then makes the same change.

Right now the first machine takes every write, while the replica only follows.

So could the replica take some of the writes too?

server Aserver Aserver Bserver Bdatabase serverusersidnamexp1Ada130read-onlydatabase serverusersidnamexp1Ada130replicausersidnamexp1Ada130read-onlyreplicausersidnamexp1Ada130UPDATE users SET xp = xp + 10WHERE id = 1UPDATE users SET xp = xp + 10WHERE id = 1

Say Ada has 120 XP on both machines.

And say server A sends its writes to the first machine, and server B sends its writes to the replica.

She finishes lesson 3 on her laptop, and server A sends the first machine its UPDATE.

At the same moment, she finishes another on her phone, and server B sends the replica its UPDATE.

Each UPDATE adds 10 to that machine's own 120, so each writes 130.

server Aserver Aserver Bserver Busers: set Ada's xp to 130database serverusersidnamexp1Ada130read-onlydatabase serverusersidnamexp1Ada130replicausersidnamexp1Ada130read-onlyreplicausersidnamexp1Ada130

And each sends the other its log entry: set Ada's xp to 130.

So both copies end on 130, and neither has 140.

One award of 10 XP is lost: the lost update you've met, now across two machines.

So Simplified lets only one database server take writes: the primary.

The primary is the one database server that takes every write, and sends each change on to the replicas.

server Aserver Aserver Bserver Busers: set Ada's xp to 130users: set Ada's xp to 140primaryusersidnamexp1Ada120read-onlyprimaryusersidnamexp1Ada120replicausersidnamexp1Ada120read-onlyreplicausersidnamexp1Ada120

So what happens to Ada's two finishes now?

server Aserver Aserver Bserver Busers: set Ada's xp to 130users: set Ada's xp to 140primaryusersidnamexp1Ada140read-onlyprimaryusersidnamexp1Ada140replicausersidnamexp1Ada140read-onlyreplicausersidnamexp1Ada140UPDATE users SET xp = xp + 10WHERE id = 1UPDATE users SET xp = xp + 10WHERE id = 1

Server A sends its UPDATE to the primary, which writes 130.

Then server B sends its UPDATE to the primary too.

The primary starts from its 130, adds 10, and writes 140.

It sends the replica both log entries, in that order: 130, then 140.

So the replica ends at 140 too.

One primary means one order of changes, and every replica follows that order.

With one primary, every award counts, and every copy ends on the same value.

Example: Why only one machine takes writes

Simplified's primary and its replica both have Ben at 40 XP. Server A sends UPDATE users SET xp = xp + 10 WHERE id = 2; to the primary. A moment later, server B sends the same UPDATE to the primary.

What XP does the replica end with for Ben?

Explanation

First, the primary starts from 40 and writes 50, then starts from its 50 and writes 60.

Then, it sends the replica both log entries, in that order: 50, then 60.

Therefore, the replica ends with 60 XP for Ben, the same as the primary.

Question 1

Say Simplified let both of its database servers take writes. Dee has 300 XP on both. Server A sends UPDATE users SET xp = xp + 20 WHERE id = 4; to the first machine. At the same moment, server B sends the same UPDATE to the second machine.

How much XP does the first machine end with for Dee?

XP

Question 2

Simplified has two database servers, each with a copy of every row.

Why does it let only one of them, the primary, take writes?

Question 3

Simplified's primary and its replica both have Fay at 75 XP. Fay finishes three lessons, each worth 10 XP. Server A sends the first and third UPDATEs, and server B sends the second, all to the primary. Each UPDATE sets xp to xp plus 10.

How much XP does the replica end with for Fay?

XP

Question 4

Say Instagram let two database servers both take writes. Your post has 41 likes on both. Two friends like it at the same moment, and one like reaches each machine. Each machine runs likes = likes + 1 on its own copy, then sends the other its log entry: set likes to 42.

What count do the two copies end on?

Question 5

Simplified's primary and its replica both have Eli at 40 XP. The primary takes server A's UPDATE adding 10 to Eli's XP, then server B's UPDATE adding 20, and sends the replica a log entry for each.

What does the replica do?

Question 6

Simplified has two database servers. One takes every write, and sends each change on to the other, which follows it.

What is the one that takes every write called?

What if a write goes to the copy?

server Aserver Aserver Bserver Busers: set Ada's xp to 130users: set Ada's xp to 140primaryusersidnamexp1Ada140read-onlyprimaryusersidnamexp1Ada140replicausersidnamexp1Ada140read-onlyreplicausersidnamexp1Ada140140UPDATE users SET xp = xp + 10WHERE id = 1UPDATE users SET xp = xp + 10WHERE id = 1SELECT xp FROM usersWHERE id = 1
server Aserver Aserver Bserver Busers: set Ada's xp to 130users: set Ada's xp to 140primaryusersidnamexp1Ada120read-onlyprimaryusersidnamexp1Ada120replicausersidnamexp1Ada120read-onlyreplicausersidnamexp1Ada120ERROR: cannot execute UPDATE in aread-only transactionUPDATE users SET xp = xp + 10WHERE id = 1?
server Aserver Aserver Bserver Bprimaryusersidnamexp1Ada120read-onlyprimaryusersidnamexp1Ada120replicausersidnamexp1Ada120read-onlyreplicausersidnamexp1Ada120120SELECT xp FROM usersWHERE id = 1?

Databases · Lesson 2.3

Keeping the Copy Current

What if a write goes to the copy?

Simplified
0:00 / 1:13

So only the primary takes writes, and the replica follows its changes, in the same order.

But the replica has every row too, so server B could send it a write.

Then two machines would take writes again, and an award would be lost.

So Postgres on a replica refuses every write: it's read-only.

Read-only means it answers queries that read, but refuses every write.

Say Ada has 120 XP on both machines.

So what happens if server B sends its UPDATE to the replica by mistake?

server Aserver Aserver Bserver Busers: set Ada's xp to 130users: set Ada's xp to 140primaryusersidnamexp1Ada120read-onlyprimaryusersidnamexp1Ada120replicausersidnamexp1Ada120read-onlyreplicausersidnamexp1Ada120ERROR: cannot execute UPDATE in aread-only transactionUPDATE users SET xp = xp + 10WHERE id = 1

Postgres on the replica refuses it, with an error: cannot execute UPDATE in a read-only transaction.

So Ada's XP on the replica stays at 120.

The UPDATE never reached the primary either, so it isn't made anywhere.

And what if server B sends the replica a query that only reads?

server Aserver Aserver Bserver Bprimaryusersidnamexp1Ada120read-onlyprimaryusersidnamexp1Ada120replicausersidnamexp1Ada120read-onlyreplicausersidnamexp1Ada120120SELECT xp FROM usersWHERE id = 1

Say it sends a SELECT for Ada's XP.

A read changes nothing, so no award can be lost.

So the replica answers it from its own copy: 120.

So the replica only ever changes when the primary sends it a change.

A replica answers reads and refuses writes, and changes only by following the primary.

Example: What if a write goes to the copy?

Simplified's primary and its read-only replica both have Dee at 300 XP. Server B sends UPDATE users SET xp = xp + 20 WHERE id = 4; to the replica by mistake.

What XP does the replica hold for Dee afterwards?

Explanation

First, the replica is read-only, so it refuses the UPDATE with an error.

Then, the primary never got the UPDATE, so it sends the replica no change for Dee.

Therefore, the replica still holds 300 XP for Dee.

Question 1

Simplified has a primary and a replica, both with Eli at 40 XP. Server B sends UPDATE users SET xp = xp + 10 WHERE id = 5; to the replica.

What does the replica send back?

Question 2

Simplified's primary and its read-only replica both have Fay at 75 XP. Server A sends UPDATE users SET xp = xp + 10 WHERE id = 6; to the primary. Server B sends the same UPDATE to the replica by mistake.

How much XP does the replica end with for Fay?

XP

Question 3

Simplified's replica is read-only.

Which of these queries can it answer?

Question 4

Simplified's replica is read-only. Server B sends INSERT INTO completions (user_id, lesson_id) VALUES (2, 3); to the replica by mistake, instead of to the primary.

Does the row (2, 3) end up on the primary?

Question 5

Simplified's replica is read-only: it refuses every write sent to it. Ada finishes lesson 3, and her XP goes from 120 to 130 on the primary.

How does the replica still get her 130?

Question 6

Simplified's replica answers queries that read, but refuses every write sent to it, with an error.

What word describes a replica like this?

Assistant

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