Databases · Lesson 1.1
Many servers, one database
When Ada opens Courses on Simplified, Simplified's server builds that page for her.
A server is a computer that runs an app for everyone who uses it.
But the server doesn't keep Ada's progress itself: it asks its database for it.
A database is a program whose whole job is to keep data on disk, and answer questions about it.
Simplified's database is Postgres, which keeps every learner's row, like Ada's 120 XP.
So the server asks Postgres for Ada's row, and puts her 120 XP on the page.
But what happens when thousands of learners open lessons at the same moment?
Every page takes the server some work: building it, checking answers, saving them.
One server can only handle so many requests a second, so the rest would wait.
So Simplified runs two servers side by side: server A and server B.
Each learner's request goes to one of them, so each does about half the work.
So does each server get its own database too?
Say server A had its own database, and server B another.
Ada finishes lesson 3, and server A saves her 130 XP in its database.
Then she opens Courses, and server B handles it, from its own database.
There, Ada still has 120 XP, so her XP depends on which server she reaches.
So instead, server A and server B both ask the same database, Postgres.
Postgres runs on a computer of its own, so both servers can reach it.
This computer is called the database server.
The database server is the computer that runs the database, for every server that asks it.
It's like your Supabase project: Supabase started your Postgres on one of its servers.
So Ada sees 130 XP, whichever server she reaches.
So however many servers Simplified runs, they all share one database server.
Simplified runs server A and server B, and both ask one database server. Ada has 120 XP. She finishes lesson 3, worth 10 XP, on her laptop, and server A saves her new XP. Then she opens Courses on her phone, and server B handles that request.
What XP does server B load for Ada?
Explanation
First, server A saved Ada's 130 XP to the one database server.
Then, server B asks that same database server, so it reads the 130 that server A saved.
Therefore, server B loads 130 XP.
Simplified's server A and server B both ask Postgres for learners' rows. Postgres runs on a computer of its own.
What is that computer called?
Databases · Lesson 1.2
What if the database server dies?
So server A and server B share the work, and both ask one database server.
And every row Simplified keeps is on that one machine's disk.
But what happens to Simplified if that one database server dies?
Say the database server dies, and doesn't come back.
This isn't a restart: the machine is gone, and its disk with it.
Server A and server B still run, but every request that needs the database fails.
Ada finishes lesson 3, and server A can't save her 130 XP.
Ben opens Courses, and server B can't load his progress.
A part like this database server is called a single point of failure.
A single point of failure is one part whose loss stops everything.
Now what might happen to WhatsApp, if it had just one database server?
Say WhatsApp kept every message on one database server.
Say it dies, and doesn't come back.
Your friend sends you a message, and WhatsApp's server can't save it.
You open your chats, and WhatsApp's server can't load them.
WhatsApp's servers still run, but no one can send or read a single message.
So why didn't running two servers, A and B, remove it?
If server A dies, server B is still running, and still reaches the database.
So losing server A doesn't stop everything.
But both servers send every query to the same database server.
So when that one machine dies, both of them fail together.
More servers like A and B don't help while they all need one database server.
Say Simplified adds a third server, C, so servers A, B and C share the work. All three send every query to one database server.
Which machine is a single point of failure?
Explanation
First, if server A, B or C dies, the other two still run, and still reach the database, so that loss doesn't stop everything.
Then, if the database server dies, every request that needs the database fails, on all three servers.
Therefore, the database server is the single point of failure.
Say Duolingo runs four app servers side by side, to share the work, and all four send every query to one database server. For a rush of new learners, it adds a fifth app server, which sends its queries to the same database server. A single point of failure is one part whose loss stops everything.
How many single points of failure does Duolingo have now?
Simplified runs server A and server B side by side, and both send every query to one database server. Say the database server dies, and doesn't come back, while server A and server B keep running. Dee answers a question in a lesson, and server A sends the database the write that saves her answer.
What happens to that write?
Simplified runs server A and server B side by side, and both send every query to one database server. Say server A dies, and doesn't come back, while server B and the database server keep running. Ada has 120 XP. She finishes lesson 3, worth 10 XP, and server B handles it.
What happens?
Simplified runs server A and server B side by side, and both send every query to one database server. Say the database server dies, and doesn't come back.
Why does every request that needs the database fail, even though server A and server B are still running?
Simplified's server A and server B both send every query to one database server, so if it dies, everything stops.
One part whose loss stops everything is called a what?
You made a free Supabase project, and Supabase started your Postgres database just for you, on one of its servers. Say your app sends every query to that one database server, and that machine dies, and doesn't come back.
What happens to your app's queries?
Databases · Lesson 1.3
Rows that outlive their machine
So when Simplified's one database server dies, every request that needs the database fails.
Because every row Simplified keeps is on that one machine's disk.
And if it never comes back, those rows are gone with it.
So how can Simplified keep its rows, even when that machine dies?
Not with a third server like A and B, which keep no rows, but a second database server.
The second one holds a copy of every row the first one has.
This second database server is called a replica.
A replica is a second database server that holds a copy of every row, kept up to date.
So what happens now, if the first database server dies?
Say it dies, just after Ada finished lesson 3 with 130 XP.
Server A and server B can't reach the first database server any more.
But the replica has a copy of Ada's row, with her 130 XP.
And of every other row, because the replica is kept up to date.
We will explore how the replica stays up to date in the next lesson.
For now, just know it gets every change the first one makes.
And how server A and server B switch to it comes in a later lesson.
Now how might Instagram keep your photos, when one of its machines dies?
Say Instagram keeps your photos on a database server, with a replica of it.
You post a photo, and the replica gets a copy of it too.
Then say the first database server dies, and doesn't come back.
Your photo is still on the replica, so it isn't lost.
A replica keeps a copy of every row, so the rows outlive the machine that first wrote them.
Simplified's first database server and its replica both have Dee at 300 XP. Dee finishes a lesson worth 20 XP, and the replica gets the change too, so both now have 320. Then the first database server dies, and doesn't come back.
What XP does the replica hold for Dee?
Explanation
First, the replica holds a copy of every row, kept up to date, so it got Dee's 320.
Then, the first machine dying doesn't touch the replica's copy.
Therefore, the replica holds 320 XP for Dee.
Simplified runs a second database server beside its first one.
What makes the second one a replica?
Say Simplified kept all 2,000,000 learners' rows on one database server, with no replica. That database server dies, and doesn't come back.
How many of those learners' rows can Simplified still read?
Simplified wants every learner's row to survive its database server dying, and never coming back.
Which of these does that?
Say Simplified copied every row onto a second database server on Monday, and never again. Ada had 120 XP on Monday. On Tuesday she finishes lesson 3, worth 10 XP, and the first database server saves her 130. On Wednesday, the first database server dies.
How much XP does the Monday copy hold for Ada?
Say Spotify keeps your playlists on a database server, with a replica of it. You add a song to a playlist, and the replica gets the change too. Then the first database server dies, and doesn't come back.
Where is your playlist with the new song now?
Simplified runs one database server and one replica, and each holds all 2,000,000 learners' rows, kept up to date. The first database server dies, and doesn't come back.
How many learners' rows can Simplified still find?