← Study paths

Surviving a failed node

What happens in the ten seconds after a database or cache node dies, and how to make it boring.

7 blocks · about 39 min of reading · ends with Distributed Key-Value Cache

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.

  1. Pull broken instances out of rotationFundamental
  2. Survive losing a nodeSenior
  3. Make an expired lease harmlessStaff
  4. Tune reads and writes with quorumsSenior
  5. Agree on one leader or one valueStaff
  6. Spread across zones and regionsStaff
  7. Recover from bad data, not just dead disksSenior
  8. Distributed Key-Value Cache (Redis / Memcached-like)
Consistency & coordinationReliability & failure handlingFundamentalSeniorStaff
Read each block, then tick “I can explain this”: the path remembers where you are.Start with step 1
Your Distributed Key-Value Cache so far0 of 7 blocks in
Shard 3 primaryAmazon EC2zone A; fenced byepoch on returnShard 3 replicaAmazon EC2zone B; promoted toprimaryApplicationAmazon EC2 Auto Scalingcache-aside on a missCache client libraryholds the ring, epoch 7to 8Cluster managerAmazon EC2 Auto Scalingchecks every 1 s, 3missesBacking storeAmazon Aurorathe truth; backups andPITRRing store (etcd)3 members, Raft,majority commitStep 3: Make an expired lease harmless3Step 2: Survive losing a node2Step 6: Spread across zones and regions6Step 1: Pull broken instances out of rotation1Step 7: Recover from bad data, not just dead disks7Step 4: Tune reads and writes with quorums4Step 5: Agree on one leader or one value5get / set1. set times outasync stream2. no reply3. promote4. write ring v85. watch, refreshon miss

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.

Each block has a diagram. Open one beside its step, or all of them at once.
  1. Pull broken instances out of rotation

    Reliability & failure handlingfundamental~5 min

    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.

    Read this block →

  2. Survive losing a node

    Reliability & failure handlingsenior~6 min

    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.

    Read this block →

  3. Make an expired lease harmless

    Consistency & coordinationstaff~6 min

    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?

    The TTL tells the lock service when to give the lease away, but it cannot reach into a paused process and stop it. When the process resumes it has no idea time passed. Only the resource being written can enforce that the writer is still the legitimate holder, and it needs the token to do that.

    Read this block →

  4. Tune reads and writes with quorums

    Consistency & coordinationsenior~5 min

    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.

    Read this block →

  5. Agree on one leader or one value

    Consistency & coordinationstaff~6 min

    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.

    Read this block →

  6. Spread across zones and regions

    Reliability & failure handlingstaff~6 min

    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.

    Read this block →

  7. Recover from bad data, not just dead disks

    Reliability & failure handlingsenior~5 min

    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.

    Read this block →

  8. 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
    Shard 3 primaryAmazon EC2zone A; fenced byepoch on returnShard 3 replicaAmazon EC2zone B; promoted toprimaryApplicationAmazon EC2 Auto Scalingcache-aside on a missCache client libraryholds the ring, epoch 7to 8Cluster managerAmazon EC2 Auto Scalingchecks every 1 s, 3missesBacking storeAmazon Aurorathe truth; backups andPITRRing store (etcd)3 members, Raft,majority commitStep 3: Make an expired lease harmless3Step 2: Survive losing a node2Step 6: Spread across zones and regions6Step 1: Pull broken instances out of rotation1Step 7: Recover from bad data, not just dead disks7Step 4: Tune reads and writes with quorums4Step 5: Agree on one leader or one value5get / set1. set times outasync stream2. no reply3. promote4. write ring v85. watch, refreshon miss

    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.

    1. Pull broken instances out of rotation
    2. Survive losing a node
    3. Make an expired lease harmless
    4. Tune reads and writes with quorums
    5. Agree on one leader or one value
    6. Spread across zones and regions
    7. Recover from bad data, not just dead disks

    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.
  9. ★

    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.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2Shard 3 primary, Shard 3 replicaCompute EngineAzure Virtual MachinesDigitalOcean Droplets, any VM or bare metal
Amazon EC2 Auto ScalingApplication, Cluster manager, Instance 11, Instance 12, Matcher A, Matcher B, Zones A, B, C, Region 2 balancer + APICompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon AuroraBacking store, URL store, Postgres primary, Postgres replica, Sync replica, Restore instanceAlloyDB for PostgreSQL, Cloud SQLAzure Database for PostgreSQL or MySQLPostgreSQL, MySQL, CockroachDB
Application Load BalancerLoad balancer, Region 1 balancerCloud Load BalancingApplication Gateway, Azure Load BalancerNGINX, HAProxy, Envoy
Amazon EKSOrchestratorGKEAKSKubernetes, k3s
Amazon Route 53DNS health checksCloud DNSAzure DNS, Traffic ManagerCloudflare DNS, NS1
Amazon CloudFrontCDNCloud CDN, Media CDNAzure Front DoorCloudflare, Fastly, Akamai
Amazon S3Base backup 02:00, WAL archiveCloud StorageBlob StorageCloudflare R2, MinIO, Ceph