Surviving a failed node
What happens in the ten seconds after a database or cache node dies, and how to make it boring.
When you finish, you should be able to say
Walk through a primary failure step by step, including the write that was in flight.
The route
7 blocks (1 fundamental, 3 senior, 3 staff), then the practice problem.
- Pull broken instances out of rotationFundamental
- Survive losing a nodeSenior
- Make an expired lease harmlessStaff
- Tune reads and writes with quorumsSenior
- Agree on one leader or one valueStaff
- Spread across zones and regionsStaff
- Recover from bad data, not just dead disksSenior
- Distributed Key-Value Cache (Redis / Memcached-like)
Next, step 1 adds Cluster manager (outlined). A box fills in when you tick “I can explain this” on the block that introduces it; plain boxes come from the problem itself.
Failure handling starts with noticing; this is how the system decides a node is gone.
The load balancer keeps sending traffic to a crashed or overloaded server.
Say it like this: The balancer needs a signal that is true, cheap and local, so each instance exposes a liveness probe that says the process is alive and a readiness probe that says it can serve, and neither one calls a downstream dependency.
Adds to the design: Cluster manager
Check yourself: Why should readiness not check the database?
Because the database is shared. If it fails, all instances fail the check together and the balancer removes the entire fleet, which turns degraded responses into no responses at all. Report dependency health as a metric and let the request path degrade; use readiness only for what is specific to this instance.
The promotion itself, and the writes that can be lost on the way.
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.
Adds to the design: Shard 3 replica
Check yourself: 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.
The old primary may not know it was replaced; this is what stops it from corrupting data.
A worker holding a lease pauses (GC, network), the lease expires, another worker takes it, then the first wakes up and writes anyway.
Say it like this: A lock cannot stop a paused worker that still thinks it holds the lease, so every lease carries an increasing token and the trip store rejects any write with a token older than the last one it accepted.
Adds to the design: Shard 3 primary
Check yourself: Why is the lease TTL alone not enough?
How many copies must agree for a read or write to be safe, and the formula behind it.
You want strong reads sometimes and fast writes other times on a replicated store.
Say it like this: With three replicas I write to two and read from two, so any read overlaps the latest write without waiting for all three, and I can drop to one-of-three reads where stale data is fine.
Adds to the design: Ring store (etcd)
Check yourself: Prove that R + W > N gives you the latest write.
A write is acknowledged once W replicas hold it. A read consults R replicas. If R + W > N, the two sets must share at least one replica, because together they exceed the total. That shared replica holds the latest write, and the coordinator returns the newest version among the replies.
The service that holds the answer to "who is the leader" when nodes disagree.
Nodes must agree on configuration, leadership or a lock even when some crash.
Say it like this: The ring version and the leader must be agreed on by every node even if one crashes, so I keep that metadata in etcd, a Raft-replicated log, and never put high-volume data there.
Adds to the design: Ring store (etcd)
Check yourself: Why three or five nodes and not two or four?
A majority of 2 is 2, so two nodes tolerate zero failures and are worse than one. Four nodes need 3 for a majority and tolerate one failure, the same as three nodes, at more cost. Odd numbers give the best fault tolerance per node.
Zoom out: a whole zone or region can fail, and the same ideas apply at that scale.
A data-centre outage takes everything down.
Say it like this: A zone failure should be invisible and a region failure should be a controlled failover, so every tier runs in at least two zones behind the balancer, data replicates to a second region, and DNS health checks move traffic there when the primary region goes dark.
Adds to the design: Shard 3 replica
Check yourself: How do you size a region for zone loss?
Compute peak capacity, then divide by the number of zones minus one. With three zones each must carry half of peak, so the fleet runs at about 67% utilisation. With two zones each must carry all of it. Three zones is the sweet spot between headroom cost and failure isolation.
Replication copies mistakes faithfully; backups are the only way back from a bad write.
Replication faithfully copies a bad migration or a DELETE without WHERE to every replica.
Say it like this: Replication protects against hardware failure but copies a bad DELETE faithfully, so I keep nightly base backups plus a continuous archive of the write-ahead log, which lets me restore the database to any second before the mistake, and I rehearse that restore every month.
Adds to the design: Backing store
Check yourself: What is the difference between replication and backup?
Replication gives you a live copy that survives hardware loss; it cannot undo a write because it applies every write. Backup gives you a copy frozen at a point in the past; it can undo any write after that point but takes time to bring online. You need both, for different failures.
Putting it together
How the blocks above combine into one design for Distributed Key-Value Cache. The numbers on the boxes are the steps that introduce each part.
Shard failover in the distributed cache, assembled from the path Each number is a step of this path; a box with two numbers is shaped by both blocks. The ring colour is the block's category, as on the route map. Boxes without a number come from the problem itself. Point at a step below, or at a bubble, to see where it appears.
When shard 3's primary stops answering, the manager promotes the replica in another zone and publishes ring v8 through etcd; clients refresh, and the epoch keeps the old primary from accepting writes. The backing store, not the cache, is what gets backed up.
How each block shows up in the design
- 1. Pull broken instances out of rotation: the cluster manager health-checks every node each second and declares a primary dead after three missed checks, so a failure is acted on within a few seconds instead of waiting for clients to time out one by one.
- 2. Survive losing a node: each shard has one asynchronous replica fed by the primary's write stream, so when shard 3's primary dies the manager promotes the replica; the few writes not yet streamed are lost, which a cache can afford because the backing store holds the truth.
- 3. Make an expired lease harmless: the ring epoch acts as the fencing token, so once ring v8 is published a request sent with epoch 7 gets MOVED, and an old primary that comes back still believing it leads cannot accept writes for a shard it no longer owns.
- 4. Tune reads and writes with quorums: cache data deliberately uses no quorum, one write to the primary and an async copy, because a stale read is acceptable; the quorum lives in the ring store, where etcd commits a new ring only once a majority of its three members has it.
- 5. Agree on one leader or one value: etcd runs Raft, so there is exactly one current ring and one answer to which node leads shard 3, even if the network splits or two managers race, and every client watches that one source.
- 6. Spread across zones and regions: each replica sits in a different availability zone from its primary, so losing a zone becomes a set of promotions rather than a miss storm against the database; multi-region replication stays out of scope.
- 7. Recover from bad data, not just dead disks: the cache keeps no backups, since a bad value can be deleted or left to expire and is reloaded from the backing store on the next miss; backups and point-in-time recovery belong to that store.
- ★
Distributed Key-Value Cache (Redis / Memcached-like)
Now run the timed practice. The blocks above should appear in your answer without prompting; if one does not, that is the one to reread.
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 | Shard 3 primary, Shard 3 replica | Compute Engine | Azure Virtual Machines | DigitalOcean Droplets, any VM or bare metal |
| Amazon EC2 Auto Scaling | Application, Cluster manager, Instance 11, Instance 12, Matcher A, Matcher B, Zones A, B, C, Region 2 balancer + API | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon Aurora | Backing store, URL store, Postgres primary, Postgres replica, Sync replica, Restore instance | AlloyDB for PostgreSQL, Cloud SQL | Azure Database for PostgreSQL or MySQL | PostgreSQL, MySQL, CockroachDB |
| Application Load Balancer | Load balancer, Region 1 balancer | Cloud Load Balancing | Application Gateway, Azure Load Balancer | NGINX, HAProxy, Envoy |
| Amazon EKS | Orchestrator | GKE | AKS | Kubernetes, k3s |
| Amazon Route 53 | DNS health checks | Cloud DNS | Azure DNS, Traffic Manager | Cloudflare DNS, NS1 |
| Amazon CloudFront | CDN | Cloud CDN, Media CDN | Azure Front Door | Cloudflare, Fastly, Akamai |
| Amazon S3 | Base backup 02:00, WAL archive | Cloud Storage | Blob Storage | Cloudflare R2, MinIO, Ceph |