14. Replication
14.3 Should the leader wait for followers before confirming a write?
At the bank: Head office can tell a customer "your notice is filed" only after a branch confirms it has a copy (slower, but safe if head office burns tonight). Or it can say "filed" immediately and send the copies afterwards (fast, but if head office burns tonight, today's notices are gone).
In the system:
Figure: synchronous trades speed for safety; asynchronous trades safety for speed.
- Synchronous replication: the leader confirms a write only after a follower has it. No confirmed write is lost if the leader dies. But every write waits for a network round trip, and if that follower is slow or down, writes slow down or stop.
- Asynchronous replication: the leader confirms immediately and followers catch up later. Fast, and followers can't block writes. But if the leader dies, its most recent writes may never have reached any follower, and they're lost when a follower is promoted. In the chapter's code, a leader with 10 writes dies while its follower has 7: promoting the follower loses 3 orders.
- Semi-synchronous (the common middle ground): wait for one follower, let the others catch up asynchronously. One confirmed copy elsewhere, without waiting for all of them.
Many databases default to asynchronous (PostgreSQL's streaming replication does) and let you turn on synchronous replication for specific standbys or even specific transactions. Choose per data: orders and payments deserve at least one synchronous copy; view counts don't.
Memory code: "Wait for a copy and be safe, or don't wait and be fast."