Databases · Lesson 2.1
How a replica gets every change
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?
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?
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.
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.
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?
Databases · Lesson 2.2
Why only one machine takes writes
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?
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.
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.
So what happens to Ada's two finishes now?
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.
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.
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?
Simplified has two database servers, each with a copy of every row.
Why does it let only one of them, the primary, take writes?
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?
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?
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?
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?
Databases · Lesson 2.3
What if a write goes to the copy?
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?
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?
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.
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.
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?
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?
Simplified's replica is read-only.
Which of these queries can it answer?
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?
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?
Simplified's replica answers queries that read, but refuses every write sent to it, with an error.
What word describes a replica like this?