On this page
What you need first
This lesson uses ideas from these earlier lessons. Read them first if any idea is new to you.
01Why interviewers ask this
One database server can do only so much work in a second. When an app grows, the database is often the first part that is too busy. So interviewers ask: "The database is too busy. What do you do?"
A weak answer says "add more database servers" and stops. A strong answer says: "I keep extra copies of the data. Reads go to the copies. Writes still go to one server." It also says when the user hears "saved": before or after the copies have the change. Then it says what that choice costs.
At SDE-1 level you should explain what the copies are for. At SDE-2 level you should work out how many copies you need, and say that writes do not get faster. At SDE-3 level you should also choose how to copy by what the data is worth.
02The idea in plain words
Think of a school with one master register in the principal's office. Every change, such as a new student, is written there first. The school also keeps a copy of the register in each classroom. A clerk carries every new line from the master register to the copies. Teachers look at the copy in their classroom, so the principal's office is not crowded. But to change a line, a teacher must go to the principal.
The principal has a choice. She can say "done" as soon as she writes the line, and let the clerk carry it later. That is fast, but for a short time the copies do not show the new line. Or she can wait until the clerk confirms that a copy has the line. That is slower, but the copy is not behind.
A database works in the same way. Remember two words from lesson 14. A read looks at data. A write changes data.
Replication means keeping copies of the same data on more than one server. Each copy is a replica. The copies have two roles:
- The leader is the one server that accepts writes.
- A follower is a replica that copies the leader. It answers reads. Your app does not write to it.
Tools use other names. Postgres says "primary" and "standby". They mean the same two roles. This course says leader and follower.
How a follower copies the leader
In lesson 21 you saw that a database writes each change to a log on disk before it says "done". Postgres calls this log the write-ahead log. The leader sends this log of changes to every follower. A follower applies the changes one after another, in the same order. The same changes in the same order give the same data. A new follower first takes a full copy of the data, and then follows the log.
Why does only the leader accept writes? Two servers could change the same row in two different ways at the same time, and their data would stop matching. With one leader there is one order of changes, and every follower follows it.
What followers are for
- More reads. Each follower answers reads, so the reads are shared. If one follower can do 1,000 reads per second, three followers can do about 3,000.
- Another copy of the data. If the leader's disk breaks, the data is still on the followers.
Followers are not for two things:
- Faster writes. Every write still goes to the leader. The leader also has to send the log to each follower, so each follower adds a little work.
- Less data on each server. Every follower holds all the data. If the data is 2 TB, the leader and each follower hold 2 TB each.
Asynchronous or synchronous copying
When the leader gets a write, it must decide when to tell the app "saved".
- Asynchronous copying: the leader saves the change on its own disk and tells the app "saved". It sends the change to the followers later. The app does not wait for them.
- Synchronous copying: the leader sends the change to a follower and waits until the follower confirms that it saved the change. Only then does it tell the app "saved".
| Asynchronous | Synchronous | |
|---|---|---|
| Time for one write | Short: only the leader's disk | Longer: plus a trip to the follower and its disk |
| Does the follower have the change when the app hears "saved"? | Not always | Yes, the follower that was waited for |
| The follower is down or slow | Writes go on | Writes wait |
| The leader's disk is lost | The newest changes can be lost | They are safe on the follower |
A middle way is to wait for one follower and let the others copy later. Another is to wait for whichever of two followers answers first, so one slow follower does not stop writes.
In Postgres, copying to followers is asynchronous by default. The delay is typically under one second, if the follower can keep up. The setting synchronous_commit decides how long a commit waits. A commit is the moment the database keeps a transaction's changes (lesson 21). These are the settings that matter here:
| Setting | A commit waits until... |
|---|---|
local | the leader has saved the change on its own disk. It does not wait for any follower. |
remote_write | a follower has received the change and handed it to its system. It may not be safe on disk yet. |
on (the default) | a follower has saved the change on disk. It waits only if you have named the followers to wait for. If not, it waits only for the leader's disk. |
remote_apply | a follower has also applied the change, so a read on that follower can see it. |
The last three wait for a follower only if you have named the followers to wait for. If not, they act like local.
Stuck on something? The AI tutor has read this lesson. Ask it with a free account
03Pause and predict
Pause. Guess first.
One database server can take 700 writes per second. Reads are fine, but writes grow to 1,000 per second. You add 3 followers. Can the system now take 1,000 writes per second? Say yes or no, and why.
Write your guess before you read on. The answer is in the worked example.
04Worked example
SwadKart, a food-delivery app, has one database server. How many followers does it need, and what does each way of copying cost?
Assumptions (made up to teach with, not real traffic):
- The server has 8 CPU cores. One core gives 1,000 ms of work per second (lesson 15), so 8 cores give 8,000 ms per second.
- We plan to use at most 70% of a server at peak, which is 5,600 ms per second (the target from lesson 15).
- At peak there are 1,500 reads per second and 300 writes per second.
- A read costs 4 ms of CPU. A write costs 8 ms on the leader.
- A follower spends 0.5 ms of CPU to apply one change. The leader spends 0.25 ms of CPU to send one change to one follower.
- Reads go to followers only. The leader takes only writes.
Step 1: one server.
- Reads = 1,500 x 4 ms = 6,000 ms per second
- Writes = 300 x 8 ms = 2,400 ms per second
- Total = 8,400 ms. That is 8,400 / 8,000 = 105% of the server. It cannot keep up.
Step 2: one follower. It takes all 1,500 reads and applies all 300 changes.
- Follower = 1,500 x 4 + 300 x 0.5 = 6,000 + 150 = 6,150 ms. That is 6,150 / 8,000 = 77%. This is above our 70% target.
- Leader = 300 x (8 + 0.25) = 2,475 ms, which is 31%.
Step 3: two followers. Each takes 750 reads and applies all 300 changes.
- Each follower = 750 x 4 + 300 x 0.5 = 3,000 + 150 = 3,150 ms, which is 3,150 / 8,000 = 39%.
- Leader = 300 x (8 + 2 x 0.25) = 300 x 8.5 = 2,550 ms, which is 32%.
Two followers are enough. At our target, one follower can serve (5,600 - 150) / 4 = 1,362 reads per second (rounded down). Two can serve 2,725, which is more than 1,500.
Step 4: the answer to the predict box. How many writes can the leader take at our 70% target?
- With no follower: 5,600 / 8 = 700 writes per second.
- With three followers: 5,600 / (8 + 3 x 0.25) = 5,600 / 8.75 = 640 writes per second.
So the answer is no. Followers did not raise the write limit. They lowered it a little, because the leader sends the log to each one.
Step 5: the cost of waiting. Assumption: a disk write takes 1 ms on any server. A round trip inside one data centre takes 0.5 ms (lesson 14). A round trip is the time for a message to go and the answer to come back.
- Asynchronous: the app waits for the leader's disk only, so 1 ms.
- Synchronous with one follower: 1 ms for the leader + 0.5 ms for the trip + 1 ms for the follower's disk = 2.5 ms. This is the worst case: the steps run one after another.
Writes are also less available. Assumption: every server works 99.9% of the time (lesson 4), and one server failing does not make another fail. A synchronous write needs the leader and the follower, so it works 0.999 x 0.999 = 0.998001 of the time. In a 30-day month (43,200 minutes), that is (1 - 0.998001) x 43,200 = 86 minutes of downtime (rounded from 86.4). The leader alone gives 43.2 minutes. Suppose the leader waits for whichever of two followers answers first. Writes then stop only when the leader is down, or when both followers are down at once. So the downtime is about the same as the leader alone.
Step 6: the cost of not waiting. Assumption: a follower is 0.4 seconds behind the leader. At 300 writes per second, 300 x 0.4 = 120 changes are only on the leader at any moment. If the leader's disk were lost then, those 120 changes would be gone.
05Trade-offs
- Followers solve a database that is too busy with reads, and they give a second copy of the data. In the example, two followers took the load from 105% of one server to 39% each.
- Followers cost more servers. They do not raise the write limit, and every follower holds all the data. A read from a follower can show old data.
- Asynchronous solves fast writes that go on when a follower is down. It costs the newest changes if the leader's disk is lost.
- Synchronous solves losing changes. It costs slower writes, and writes that wait when the follower is down.
- I would change it when the data is money or orders that must never be lost: then I wait for one follower. I would also change it when writes, not reads, reach the limit: followers cannot help then.
06Common misconceptions
"More followers means more writes per second." It does not. All writes go to the leader. In the example the limit went from 700 to 640 writes per second after three followers were added.
"Synchronous copying is always the safer choice." It is safer for the data, but it costs speed. With one follower to wait for, a down follower stops every write. The example shows 86 minutes of write downtime a month against 43.
"A follower always shows the same data as the leader." Not with asynchronous copying. A follower applies the log a little later. In the example it was 0.4 seconds behind.
07How to say it in the interview
Say it like this
I would add two followers, so reads spread over them and all writes still go to one leader. Asynchronous copying keeps writes fast but leaves followers a little behind, so I would wait for a follower only for data we cannot lose.
08Check your understanding
- What is a leader, and what is a follower? What is the difference between asynchronous and synchronous copying?
- A database has 4 CPU cores (4,000 ms per second) and a 70% target (2,800 ms). At peak there are 800 reads per second at 3 ms each, and 100 writes per second at 6 ms each. A follower spends 0.5 ms to apply one change. The leader spends 0.25 ms to send one change to one follower. (a) What share of one server does the work use? (b) Is one follower enough, if it takes all the reads? (c) How many writes per second can the leader take with that follower?
- A teammate says: "Our writes will soon reach 3,000 per second, and the leader takes 700. Let us add 5 followers." What do you say?
09Answers
Show the answers
- The leader is the one server that accepts writes. A follower copies the leader and answers reads. With asynchronous copying the leader tells the app "saved" before the follower has the change. With synchronous copying it waits until the follower confirms.
- (a) Reads = 800 x 3 = 2,400 ms. Writes = 100 x 6 = 600 ms. Total = 3,000 ms, which is 75% of 4,000. It is above 70%. (b) Follower = 800 x 3 + 100 x 0.5 = 2,400 + 50 = 2,450 ms, which is 61%. Leader = 100 x (6 + 0.25) = 625 ms, which is 16%. Yes, one follower is enough. (c) 2,800 / 6.25 = 448 writes per second.
- Followers do not take writes, so the limit stays at about 700. It is a little less, because of the log the leader sends. 3,000 / 700 = 4.3 times too many writes. Followers help only with reads, so the writes need a different fix.
10Summary card: replication, leader and followers
One-page summary
- Replication keeps copies of the same data on more than one server. The leader takes all writes. A follower copies the leader's log of changes, in order, and answers reads.
- Followers give more reads and a second copy of the data. They do not give faster writes, and each follower holds all the data.
- Example: 1,500 reads and 300 writes per second made one server 105% busy. Two followers made each 39%. The write limit did not rise (700 before, 640 with 3 followers).
- Asynchronous: the leader says "saved" first, then copies. Fast, but followers are a little behind, and the newest changes can be lost.
- Synchronous: the leader waits for a follower. Safer, but writes are slower and stop if the follower is down (86 minutes a month against 43).
- Trade-off: followers cost servers and can show old data. Wait for a follower only for data that must not be lost.
- Trap: "more followers, more writes" is wrong.
11Sources
- Postgres, streaming replication. It is asynchronous by default, with a delay typically under one second. Synchronous replication waits until the commit is on disk at both servers. A standby can answer read-only queries. With asynchronous copying, transactions not yet shipped can be lost if the primary fails: https://www.postgresql.org/docs/current/warm-standby.html (opens in a new tab)
- Postgres,
synchronous_commitvalues (local,remote_write,on,remote_apply,off) and what each waits for: https://www.postgresql.org/docs/current/runtime-config-wal.html (opens in a new tab) - Postgres, the write-ahead log: https://www.postgresql.org/docs/current/wal-intro.html (opens in a new tab)
- Pages checked 19 Oct 2026.
- The round trip of 0.5 ms comes from the time ladder in lesson 14. All CPU costs, request rates, the 99.9% figure and the 0.4 second delay are assumptions for teaching, not measurements.
Try it now, no sign-up
- Play Level 1: one server and 100 users. Free, no sign-up.
- Read today’s Drill: one real situation, answered in about 7 minutes.
More practice, in the app (free account)
Related lessons and practice
Ask the AI tutor about this lesson
The tutor has read this lesson. Ask in your own words, any time. Free account, 20 questions a day.
Reading never needs an account. The account is for the tutor, the quiz, your notes and your progress.