AlgoViz

Fault tolerance and failure detection

medium 24 min

A cluster must notice that a machine is gone, keep serving without it, and bring it up to date when it returns.

Video lesson · Chapter 1 of 5

One server misses its heartbeat

0:00 / 4:08

Is it dead or just slow?

1:20Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Fault tolerance means that a system keeps doing its job while some of its parts are broken. In a replicated store this takes three steps: notice the failure, continue without the failed node, and repair it afterwards.

Failure detection is the first step. A node cannot look inside another node. It can only notice that messages have stopped, and silence has several causes: a crash, a slow node, a long pause or a broken link.

Why it exists

With many machines, some part is always failing. If each node fails once in three years, a cluster of 1,000 nodes sees about one failure a day. Waiting for a person to react is not a plan.

The detector has to choose between two mistakes. If it is hasty, it declares healthy nodes dead and moves work for nothing. If it is slow, requests keep going to a dead node. The tools on this page soften that choice, but none can remove it.

A short history

  1. 1979

    Ralph Merkle patents the hash tree, which later becomes the tool for comparing large copies of data.

  2. 1987

    Alan Demers and colleagues describe epidemic algorithms for keeping database copies in step: the root of gossip and anti-entropy.

  3. 1996

    Tushar Chandra and Sam Toueg publish their work on unreliable failure detectors: detectors that may be wrong can still be useful.

  4. 2004

    Naohiro Hayashibara and colleagues describe the phi accrual failure detector, which reports a level of suspicion instead of yes or no.

  5. 2007

    The Dynamo paper combines gossip membership, sloppy quorums, hinted handoff and Merkle trees in one store.

Words to know

Heartbeat
A small message sent at a fixed interval that says: I am alive.
Gossip
Each node regularly exchanges what it knows with a random other node.
Suspicion
The state between alive and declared failed.
Sloppy quorum
A quorum that may use the next healthy nodes when the normal replicas are down.
Hint
A stored write with a note about the node that should have received it.
Anti-entropy
Comparing copies in the background and repairing the differences.
Merkle tree
A tree of hashes in which each parent is the hash of its children.

How it works

Step through the idea, or press Play.

Step 1 of 3

Notice. Heartbeats stop, suspicion rises, others confirm

Understand it

Start here

Scouts in the forest

Imagine five scouts spread through a forest. There is no leader who counts them. Whenever two of them meet, they compare notes: whom have you heard from lately?

Who has been heard from?
SamAshaLeeNoorMax

Nobody can see Max. They can only compare what they have heard.

A team of firefighters works the same way. If one is sick, the others cover the area. They check on each other all the time, and the colleague who returns is told what happened meanwhile.

The problem

What can fail, and what helps

FailureWhat it looks like
CrashThe node stops and stays stopped
OmissionMessages get lost on the way in or out
ByzantineThe node answers wrongly, by a fault or on purpose
These and the kinds between them are explained in failure models. Heartbeats find the first, and suspect the second. They do not find the third.

Six mechanisms

  • Failure detection

    Heartbeats and gossip show who is silent.

  • Replication

    Several copies, so that one lost node loses no data.

  • Sloppy quorum

    Write to the next healthy nodes instead of giving up.

  • Hinted handoff

    A short-term home for writes that a node missed.

  • Read repair

    Fix an old copy when a read finds it.

  • Anti-entropy

    Compare copies in the background with Merkle trees.

How the copies are made is the topic of data replication.

Notice 1

Heartbeats and timeouts

A node sends a small message at a fixed interval, to its peers or to a central watcher. If none arrives within the timeout, it counts as failed. Typical starting points for a store: an interval of 1 to 5 seconds, a timeout of 3 to 10 seconds, and 2 or 3 missed beats before marking a node down. Coordination services inside one data centre often use much shorter values.

Two beats, then silence

How long until it is noticed?

tmax⁡≈interval+timeout1 s+5 s=6 st_{\max} \approx \text{interval} + \text{timeout} \qquad 1\text{ s} + 5\text{ s} = 6\text{ s}
Here the timeout is checked against the last beat that arrived, and the checker runs once per interval, so a crash right after a beat is seen between 5 and 6 seconds later. A pause of 8 seconds, for example for memory cleanup, would look the same as a crash.

Notice 2

Suspicion instead of yes or no

A fixed timeout suits one network. The phi accrual detector learns how the heartbeats of each node usually arrive and turns the current silence into a number, phi. The longer the silence compared with normal, the higher phi. Each user of the number sets its own limit: stop sending reads at a low one, move data at a high one.

Phi

φ=−log⁡10P(a beat comes even later)P=e−t/μ, μ=1 s:φ=1 at 2.3 s,φ=8 at 18.4 s\varphi = -\log_{10} P(\text{a beat comes even later}) \qquad P = e^{-t/\mu},\ \mu = 1\text{ s}:\quad \varphi = 1 \text{ at } 2.3\text{ s},\quad \varphi = 8 \text{ at } 18.4\text{ s}
With a simple model in which gaps between beats are exponential with mean 1 second. Phi 1 means a 10% chance that the node is fine and just late, phi 2 means 1%, phi 8 means one in 100 million, as far as the model fits.
PhiMeaning in the modelPossible reaction
110% chance of being wrongProbably alive; prefer other nodes
30.1%Stop sending new requests
80.000001%Mark the node down
10 and moreEven lessLikely dead
Cassandra's phi_convict_threshold is 8 and the default threshold in Akka is 8 as well.

Spread the news

Gossip

Every T seconds, each node

  1. 1

    Picks a random peer

    Often one, sometimes a few.

  2. 2

    Exchanges state

    Membership, heartbeat counters, load. Push, pull, or both.

  3. 3

    Merges

    For each node it keeps the entry with the newer counter.

  4. 4

    Repeats

    Like a rumour, the news spreads without any centre.

One rumour, three rounds

The number of informed nodes can at most double in each round if each tells one other.

How many rounds?

log⁡2n≤rounds≈log⁡2n+ln⁡nn=1000:;10 at best, about 10+7=17\log_2 n \le \text{rounds} \approx \log_2 n + \ln n \qquad n = 1000:; 10 \text{ at best, about } 10 + 7 = 17
When each informed node tells one random node per round, some tell a node that already knows. With one round per second the news needs about 17 seconds in 1,000 nodes.

In Cassandra every node gossips once a second, and each node decides for itself who is up. In the Dynamo paper gossip spreads membership, meaning who belongs to the ring, while temporary failures are noticed locally: if B does not answer A, A uses other nodes and checks on B now and then. Adding or removing a member was made an explicit step, so that a reboot does not trigger a reshuffle.

Make sure

Ask others before you decide

If A cannot reach B, perhaps only the path between them is broken. The SWIM protocol lets A ask some other members to probe B. If one reaches it, B is alive. If none does, B becomes suspect, and it has time to object before it is declared failed. The picture of that indirect probe is in failure models; Consul's gossip names SWIM among its foundations.

From silence to action

Avoiding a node is cheap and can be undone. Moving its data is expensive, so that needs more certainty.

Continue 1

Sloppy quorum

Key K normally lives on A, B and C. If A is down, a strict quorum counts only answers from A, B and C. A sloppy quorum takes the first N healthy nodes on the ring: B, C and D. D stands in for A. The store of this example is the one from inside a key-value store, and the ring is explained in consistent hashing.

Good

  • Writes succeed even if several normal replicas are gone
  • No waiting for timeouts of a node that is known to be down

Costs

  • R + W greater than N no longer promises overlap: a read may ask A, B and C while the write sits on D
  • More conflicting versions to settle
  • The stand-in may fail before it hands the write over

Continue 2

Hinted handoff

Node D keeps a write for node A

A client sends a put to the coordinator. The coordinator writes to node B and node C, and to node D, which keeps the write as a hint for node A. Node A is down. Later, when A is back, D delivers the hint to A.
Pink arrows happen at once. The orange dashed arrow happens when A has returned.
JSON
{  "target_node": "node-A",  "key": "user:123",  "value": { "name": "Alice" },  "timestamp": 1703001234567,  "ttl": 86400}
What a hint contains. The time limit, here 24 hours in seconds, is an example.
Hinted handoff
HelpsWrites succeed during short outages
HelpsThe returning node needs no full copy
LimitNo help if a node is gone for good
LimitHints use space on the stand-in, and replay adds load
LimitAfter the time limit, hints are no longer kept; other repair must find the difference
In Cassandra the coordinator keeps the hints, for outages of up to 3 hours by default, and calls them best effort. The consistency level ANY accepts a stored hint as the answer to a write.

Repair 1

Read repair

A read asks A, B and C. A and C return version 3, B returns version 2. The client gets version 3 and B is updated. It repairs exactly the data that people use, and nothing else. How the versions are compared is the topic of versioning data.

Repair 2

Merkle trees

Two nodes hold the same million keys, or nearly so. Sending all of them to compare would be wasteful. A Merkle tree has the hash of each block of data at the leaves. Each parent is the hash of its children. If two roots are equal, the data is equal, except for an astronomically unlikely hash collision.

Find one different block of four

  1. 1

    Compare the roots

    They differ, so go deeper.

  2. 2

    Compare H12 and H34

    H12 is the same: blocks 1 and 2 are done. H34 differs.

  3. 3

    Compare H3 and H4

    H4 is the same. H3 differs.

  4. 4

    Copy block 3

    Only that block travels over the network.

Only the red path differs

What it saves

220 keys,;210 leaves:1+2×10=21 hashes compared, then 220210=1024 keys sent2^{20}\text{ keys},; 2^{10}\text{ leaves}:\quad 1 + 2 \times 10 = 21 \text{ hashes compared, then } \frac{2^{20}}{2^{10}} = 1024 \text{ keys sent}
For one different key, instead of 1,048,576 keys. With d different leaves the comparison costs about d times the depth. If every leaf differs, the tree saves nothing.

Back again

A node recovers

  1. 1

    Detect the recovery

    Its heartbeat resumes and gossip spreads the news.

  2. 2

    Receive the hints

    Other nodes deliver the writes that they kept, at a limited speed.

  3. 3

    Run anti-entropy

    Merkle trees show what is still missing, for example after expired hints.

  4. 4

    Resume traffic

    It serves requests again. Many stores let it take part at once and rely on quorums to hide its old values.

The whole life of an outage

In practice

Settings and habits

Defaults in the documents

SystemSettingDefault
CassandraGossipevery second
Cassandraphi_convict_threshold8
Cassandramax_hint_window3 hours
CassandraGrace time of tombstones10 days
AkkaHeartbeatevery second
AkkaPhi threshold8
Defaults change between versions.

Do

  • Combine detectors: heartbeats, real requests and other nodes' views
  • Choose timeouts from measured delays, not too short
  • Limit the age and size of hints
  • Run anti-entropy regularly in the background
  • Watch repair backlog, hint counts and flapping nodes

Do not

  • Rely on hints alone for durability
  • Use very short timeouts
  • Skip tests that kill, pause and cut off nodes
  • Ignore the disk space of hints
  • Forget that a network split looks like a crash from both sides

Summary

The whole topic in one minute.

Nodes send heartbeats, and gossip spreads who has been heard from lately. Silence raises suspicion, others confirm it, and only then is a node treated as failed. A detector can always be wrong, so the protocol that uses it must stay safe when it is.

While a node is away, sloppy quorums and hints keep writes flowing. When it returns, hints bring the recent writes, read repair fixes what people read, and Merkle trees find the rest cheaply by comparing hashes from the top down.

  • Silence is all a detector sees; slow and dead look alike.
  • Short timeouts raise false alarms, long ones delay the reaction.
  • Gossip spreads news in about log n rounds without a centre.
  • Suspect first, ask others, act on certainty that matches the cost.
  • Sloppy quorums and hints keep writes going through short outages.
  • Merkle trees find differences from the top down; hints alone are not enough.

Sources