← Building blocks

Survive losing a node

Reliability & failure handlingsenior

A single database or cache node dies and the service goes down with it.

Say it like this

A single cache node dying would send every miss to the database at once, so each shard has an async follower in another zone, a manager that promotes it after three failed health checks, and a fenced ring version so the old primary cannot keep accepting writes.

Basic solution

Replicate to a standby in another zone and fail over automatically via health checks.

leader / follower replicationRedis SentinelRDS Multi-AZPatroni

Detect, promote, fence, redirect

Monitors in three zones agree the primary is down, promote the follower, bump the epoch so the old primary is fenced, and push the new address to clients.

How it works

  1. Every write goes to the primary. The primary records the change in its replication log and sends that log to one or more followers, which apply the same changes to their own copy. With synchronous replication the primary waits for a follower to confirm before acknowledging the client; with asynchronous replication it acknowledges first and ships the log afterwards.
  2. A monitor (Redis Sentinel, Patroni, or the cloud provider's own) sends the primary a health check about once a second and expects a reply within a short timeout.
  3. After N consecutive failed checks (say 3, so about 3 seconds) the monitor declares the primary down. Several monitors in different zones must agree by majority vote, so that one monitor with a broken network link cannot start a failover on its own.
  4. The monitors pick the follower that has applied the most of the replication log and promote it: it stops applying the log and starts accepting writes as the new primary. At the same time they fence the old primary so it cannot keep accepting writes if it is merely unreachable rather than dead: they revoke its role, increment an epoch or ring version that every client must present, or cut its network path.
  5. Clients learn the new primary's address through whichever channel is fastest: a DNS update, a push from the config service, a redirect from the old address, or a MOVED response from the follower.
  6. When the old primary comes back it rejoins as a follower. Any writes it accepted that never reached the followers are discarded, because the new primary's history is now the truth.

Recovery time

  • Detection (3 s) plus election (1 s) plus client refresh (1 to 30 s). Design clients to retry during that window.

Worked example

Distributed cache for the URL shortener

  • 10 shards, each primary with one follower in a second availability zone. Hot set 10 GB per shard, 40K reads/s total.
  • Shard 3’s host dies. Without a follower: 4K reads/s miss and hit the database. The URL store replica handles 8K reads/s so it survives, but p99 for 10% of redirects rises from 1 ms to 6 ms for the 5 minutes it takes to warm a new node.
  • With async replication and automatic failover: detection 3 s, promotion 1 s, clients refresh the ring within 5 s. 9 seconds of errors or stale reads for shard 3 (about 36K requests), then full speed from a warm follower. No cold-start miss storm.
  • Lost writes: async replication lag was 50 ms at failure, roughly 20 SETs. For a cache that is harmless; the next read repopulates them. For the URL store itself the same loss would be unacceptable, so there replication is synchronous to at least one replica.
  • Memory cost: double, 20 GB per shard across the pair. That is the price of a warm standby.

Variants and when each wins

Asynchronous leader-follower

  • The primary acknowledges a write before the follower has it and ships the log afterwards. It wins for caches and for databases that can lose a few hundred milliseconds of writes, because it is the fastest option.

Synchronous to one replica

  • The primary waits for at least one follower to confirm each write before acknowledging it. It wins when no acknowledged write may be lost; the price is a cross-zone round trip of about 1 ms per write, and the primary stalls if the follower is down unless it is allowed to fall back to async.

Quorum replication (Raft, Paxos)

  • Each write commits only once a majority of N nodes have stored it, and any majority can elect a new leader without losing data. It wins for configuration and coordination stores, where correctness matters more than throughput.

Multi-primary

  • Every node accepts writes and conflicts between them are resolved later. It wins only for data with natural merge rules, such as counters and sets, because correctness is hard everywhere else.

Recommendation: async for caches, synchronous-to-one for transactional databases, quorum for the metadata that everything else depends on.

Failure modes

Split brain

  • For a minute two nodes both accept writes, and afterwards their data disagrees. It happens because the old primary was cut off from the monitors but not from clients, and nothing stopped it serving. Fence the old primary with an epoch number that followers and clients check on every request, or by revoking its credentials or network path, and require a majority of monitors to agree before any failover.

Failover flapping

  • Three failovers happen in ten minutes during a short network blip. It happens because the detection window is short and a single monitor can trigger a failover alone. Require several consecutive failed checks from a majority of monitors, and enforce a cool-down period after each failover.

Clients keep writing to the old address

  • Writes succeed against the old primary and then vanish. It happens because DNS answers are cached for five minutes, or a client holds an open connection and never looks the address up again. Use short DNS TTLs, make clients re-resolve on any connection error, or push the new address from a config service with a version the client must present.

Follower too far behind

  • The promoted follower is missing the last ten seconds of writes. It happens because replication lag grew under load and nobody noticed. Alert on lag, always promote the most up-to-date follower, and use synchronous replication for data that cannot be lost.

Failover works but the follower is cold

  • The new primary answers, but at disk speed for several minutes. It happens because the database follower was never queried, so its buffer cache is empty. Route some reads to followers so their caches stay warm, or run a warm-up query set immediately after promotion.

Interviewer follow-ups

Answer out loud before you open each one.

How do you prevent the old primary from accepting writes after failover?

Fencing: every failover increments an epoch, followers and clients include it in requests, and a node refuses writes carrying an older epoch than the one it has seen. Combine with the monitor revoking the old node’s credentials or network path. A lock alone is not enough because the old primary may not know it lost.

What is the recovery point objective of async replication?

The replication lag at the moment of failure, typically tens of milliseconds and up to seconds under load. Measure it continuously and alert on it. If the business cannot lose that window, replicate synchronously to at least one node and accept the write latency.

Why do you need a quorum of monitors?

A single monitor cannot distinguish the primary being down from its own network being down. With three monitors in different zones and a majority rule, a monitor that is isolated cannot trigger a failover on its own, so a partition on the monitor side does not cause a spurious promotion.

How do clients find the new primary quickly?

Avoid relying on DNS TTLs. Use a config service or the cache library’s ring with a version number, have the demoted node return a redirect to the new primary, and make clients re-resolve on connection errors. The target is a few seconds, not minutes.

Connections

In study paths
Surviving a failed node
Built on
Write-ahead log · Merkle tree
Pairs with
Spread across zones and regions · Pull broken instances out of rotation · Agree on one leader or one value · Make sure only one worker does it · Tune reads and writes with quorums
Pulls against
Accept a short delay for scale
Used in
Distributed Key-Value Cache (Redis / Memcached-like) · URL Shortener (TinyURL / bit.ly)