The leader of an asynchronously replicated database fails. Walk through failover and what can go wrong.
Sketch your own answer first, then compare it with the one below.
A strong answer
First detect the failure, usually with timeouts, which can be wrong if the leader is only slow. Then choose a new leader. The best candidate is the follower that has applied the most of the log. Then reconfigure clients and the other followers to use it, and make sure the old leader, if it returns, becomes a follower and does not keep accepting writes.
What can go wrong: writes the old leader acknowledged but never replicated are lost, and the client was told they succeeded. If the old leader rejoins with writes the new one never saw, they conflict or get discarded. If two nodes both think they are leader, both accept writes and the data diverges. And a timeout set too low triggers needless failovers under load.
How much is lost depends on the replication mode and on the lag at the moment of failure. With semi-synchronous replication an acknowledged write is on at least one follower, so promoting the furthest-ahead survivor loses nothing acknowledged.
Sources
- Martin Kleppmann (2017). Designing Data-Intensive Applications.