Copies for reading
After this: Send writes to a primary and reads to replicas, and count the replicas a load needs.
On this page · 4 sections · 3 checks
The shop in lesson 1 sends 9,000 reads per second to a database that serves 3,000. Most of the work is reading, and reading changes nothing. So could several machines, each holding the same data, share the reading between them?
Copies of the database
Keep full copies of the database on several machines. One rule keeps them the same: only one machine takes changes.
- The is the copy that takes every write. After it has stored a change, it answers the user and sends the change on to every other copy.
- A is one of those other copies. It receives every change from the primary and serves reads.
Sending each change from the primary to the replicas is called . Reads take turns over the replicas: the first read goes to Replica 1, the next to Replica 2, and so on. This is , the same rule the load balancer used for servers.
A small example
Three replicas, with every change reaching them at once for now, and eight requests in this order: write, read, read, read, write, read, read, read.
- The first write, x = 1, goes to the primary. The primary sends it on, and all three replicas now hold x = 1.
- The three reads go to Replica 1, Replica 2 and Replica 3 in turn. Each answers 1.
- The second write, x = 2, goes to the primary and reaches all three replicas.
- The last three reads go to Replica 1, 2 and 3 again. Each answers 2.
- ReplicationPress Run it and step through. Watch where each write goes and where each read goes. Count the reads on each replica, and watch each write reach all three replicas.
Press Run it, then step through and watch the requests move.
At the end the primary has taken 2 writes, each replica has served 2 reads, and the replicas have applied 6 changes between them: each of the 2 writes, on each of the 3 replicas.
How many replicas?
Back to the shop: 9,000 reads per second, and one replica serves 3,000. 3 replicas serve 3 × 3,000 = 9,000, so 3 replicas cover the reads.
Now look at the writes. All 1,000 writes per second still go to the primary. And each of the 3 replicas must apply all 1,000 writes too, or it would stop being a full copy. Replicas share the reading, not the writing.
When the reads do not divide exactly, round up. The program below adds one replica at a time until there are enough. Java does not run here: the Run button runs the Python version, and the Java tab shows the same program for reading.
replicas_neededreplicasNeeded starts with one replica. While that many replicas serve fewer reads than arrive, it adds one more. For 9,000 it stops at 3, because 3 × 3,000 is enough. For 10,000 it goes on to 4, because 3 × 3,000 = 9,000 is not enough. Change the numbers and run it again.
Check yourself
An app has a primary and three replicas. A user places an order, which is an INSERT. Where does it go first?
The app now gets 10,000 reads per second, and one replica can serve 3,000. How many replicas does it need for the reads?
The app has 9,000 reads and 1,000 writes per second, a primary and 3 replicas. Writes grow to 3,000 per second. Will adding more replicas help with the writes?
- Send every write to the primary, which passes each change on to every replica.
- Send reads to the replicas in turn (round robin), so they share the reading.
- Count replicas by rounding up: 9,000 reads at 3,000 each need 3, and 10,000 need 4.
- Say why replicas do not share writing: the primary takes every write, and every replica applies every write too.