Back to blog

Two copies of the same data: safety or trouble?

Thắng Đoàn
Thắng Đoàn

FotoFlow has 15,000 users and one database server at 90 percent busy, most of it reads: profiles, feeds, photo pages. It is also a single point of failure: if it dies, so does everything. Both problems have one standard fix, and that fix brings three new problems.

Leader and followers

Add two followers (replicas). The leader still takes every write and streams each change to the followers through a replication log; each follower replays the changes and ends up with the same data. Send reads to the followers and writes to the leader, and read capacity doubles while the leader cools down. Need more reads? Add another follower. The leader takes every write and streams each change to followers through a replication log. This is built into PostgreSQL, MySQL and MongoDB, and it works brilliantly for read-heavy apps. Write-heavy ones get nothing: every write still goes through one leader.

The lag problem

Replication is usually asynchronous: the leader confirms a write immediately and ships it to followers a moment later. Alice renames herself, reloads her profile, and the read lands on a follower that has not caught up. She sees her old name and thinks the save failed. Lag is normally milliseconds, but under load it can grow to seconds or minutes.

The fix: read-your-writes. For a few seconds after someone writes, send their reads to the leader; everyone else keeps using followers. The alternative, synchronous replication, has the leader wait for a follower to confirm before saying saved: no lag, slower writes, and writes stall when that follower is down. A common safety setup is one synchronous follower plus asynchronous rest.

The failover problem

The leader crashes. Reads keep working, but every write fails: nobody can sign up, post, or pay. Failover promotes the most up-to-date follower to leader, points the app's writes there, and adds a fresh follower to restore the spare. Writes resume within seconds to a minute. Failover promotes the most up-to-date follower, and any writes the old leader had not shipped may be lost.

The hard part is being sure the leader is dead. If it was only slow or cut off, you now have two leaders accepting writes: split brain. Doing failover safely takes a majority vote, covered in the consensus post. Managed databases automate all of this.

The trouble replicas do not fix

A monthly finance report runs a 20-minute query and points it at a follower; that follower, also serving app reads, grinds to a halt and falls minutes behind. Fix: a dedicated analytics replica the app never reads from, or a separate analytics warehouse at larger scale.

Worse: a deploy runs UPDATE users SET email = NULL with a missing WHERE. The leader obeys, and replication faithfully copies the mistake to every follower within a second. Replicas are not backups: replication copies your mistakes as faithfully as your data. Backups mean a nightly snapshot plus the archived write log, which together rebuild the database as it was at any chosen moment: point-in-time recovery. A backup you have never restored is a hope, not a backup. Rehearse restores and know how long one takes.

What this does not solve

Replicas scale reads and survive losing a machine. When the data or the writes themselves outgrow one leader, splitting the data across machines is the only way out. That is sharding, the next post.

If your leader died right now, how long would your users go without writes, and who would press the button?
When did you last actually restore a backup, and how long did it take?

One database, many copies: still one database.

Next in the series: The database is full: buy a bigger one, or split the data?

Sources

Designing Data-Intensive Applications

System Design Primer

Google SRE Books

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.