AlgoViz

Implementing database replication

hard 30 min

Running replication in production: how to set up a replica, detect that the leader is gone, choose and promote a new one without ending up with two, use quorums, watch lag and slots, and bring the failed node back safely.

Where it shows up

The same pattern appears across everyday software.

  • GitHub, October 2018

    Their analysis describes how writes existed in both coasts' databases after an automatic failover, why they could not simply switch back, and why they chose data integrity over a faster recovery.

  • GitLab, January 2017

    A replica fell behind and needed log that the primary had already removed. During the rebuild the primary's data directory was deleted by mistake, and the dump backups turned out to be missing.

  • Patroni for PostgreSQL

    The leader must keep renewing a lock in a distributed store. If it cannot, its PostgreSQL is demoted to read-only, which prevents two leaders.

  • MongoDB replica sets

    Members vote, up to 7 of at most 50 members. A primary that can see only a minority steps down, and a majority elects a new one.

  • Redis Sentinel

    A configured number of Sentinels must agree that the master is down, and a majority must authorise the failover. Its documentation warns that confirmed writes can be lost because replication is asynchronous.

  • Apache Cassandra

    Clients choose a level per request. QUORUM is a majority of the copies, and LOCAL_QUORUM is a majority in the local data centre.