Failure models
medium 30 minA failure model is a list of the ways a machine or message may go wrong that your design promises to survive.
See it run
Watch a node stop, come back, lose messages, slow down and lie, and what the others do about it.
Operations
Crash-stop
A node halts. The rest notice and elect a new leader.
Step 1 of 5
Normal operation
Node 2 leads and sends heartbeats.
Step 1: Normal operation
if now - last_heartbeat[leader] > timeout: term += 1 votes = ask_all("vote for me", term) if votes >= members // 2 + 1: become_leader(term)- Nodes needed
- 2f + 1
- Tool
- Heartbeats
- Data only
- f + 1 copies
Count votes against all members, not only against those that answer.
In the real world
The same failure models doing real jobs. Pick one and step through it.
The leader is frozen for 20 seconds. A new one is elected. Then the old one returns.
Step 1 of 4
Leader with token 1
It writes to storage with token 1.
Step 1: Leader with token 1
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Promoting without fencing
criticalThe silent primary may only be slow. Two primaries then accept writes.
Wrongif timeout(primary): promote(standby)Rightif majority_agrees_dead(primary): token = next_token() fence(primary) promote(standby, token)A log without fsync
criticalThe data sits in the operating system's cache and goes with the power.
Wrongf.write(entry)return "ok"Rightf.write(entry); f.flush()os.fsync(f.fileno())return "ok"Retrying for ever
majorEvery client repeats into a system that is already down, and it cannot recover.
Wrongwhile True: try_again()Rightfor i in range(4): try_again() sleep(random() * min(cap, 2 ** i))Treating never seen as failed
minorA node that is just joining would be removed before its first heartbeat.
Wrongreturn last.get(node) is None or late(node)Rightif node not in last: return "unknown"
Interview tips
How to explain it
- Say what may go wrong and how many at once.
- Each step of the ladder costs more.
- Detection is a guess.
Memory trick: COOTB
- Crash-stop, crash-recOvery.
- Omission, Timing.
- Byzantine.
Numbers to know
- f + 1 copies to keep data.
- 2f + 1 to vote after crashes.
- 3f + 1 to vote among liars.
Watch out for
- Slow nodes.
- Old leaders that wake up.
- Lost answers.