Distributed Systems TheoryExpert15 min read2 questions

Consensus, Leader Election and Distributed Locks

How a group of unreliable machines agrees on anything. Raft in plain language, why split brain is the danger, and why most distributed locks are quietly broken.

Covers: Why consensus is needed, Raft explained, quorums and split brain, ZooKeeper and etcd, fencing tokens, distributed locks

Consensus is the primitive underneath everything that must be decided exactly once: who is the leader, which configuration is current, who holds the lock, what order the writes happened in. It is also where the largest gap sits between what engineers think they have built and what they have actually built.

Filter
0/2 mastered
Expertraftconsensusleader electionAsked at Google, CockroachLabs

30-second answer

Raft elects a single leader per term. Followers who stop hearing heartbeats become candidates, increment the term and request votes; a node votes at most once per term, so at most one candidate can win a majority. The leader accepts all writes, appends them to its log, replicates to followers, and commits an entry once a majority have stored it. Split brain is impossible because two disjoint majorities of the same set cannot exist - any two majorities of N nodes must share at least one member, and that member's single vote per term prevents a second leader.

Expertdistributed lockredisfencingAsked at Amazon, Stripe

30-second answer

The naive version is `SET key value NX PX ttl` plus a delete on release, and it is unsafe for two reasons. First, releasing must verify ownership atomically - otherwise a client whose lock expired can delete a lock now held by someone else, which requires a Lua script comparing the token. Second, and more fundamentally, a TTL-based lock cannot guarantee mutual exclusion: a client can be paused by GC or a network delay, its lock expires, another client acquires it, and then the first client wakes up and acts believing it still holds the lock. The only real fix is a monotonically increasing fencing token that the protected resource itself checks and rejects if stale.

Showing 2 of 2 questions for consensus-and-leader-election.

Check your understanding

3 questions · no sign-up, nothing stored

0/3 answered
Question 1

1.Why is a 4-node Raft cluster a poor choice?

Question 2

2.A Raft leader is partitioned from the majority. What can it still do?

Question 3

3.What makes a TTL-based distributed lock unsafe for correctness?

Hands-on challenge

Build it - this is what you talk about in a deep-dive round.

Build a leader-elected job scheduler

Exactly one instance in a fleet of ten should run a scheduled job every minute, and the system must survive the leader dying mid-run.

Requirements

  • Implement leader election with a consensus store or a lease with renewal.
  • Ensure a leader that loses its lease stops working before another instance starts.
  • Add fencing tokens so a paused former leader cannot write results after losing leadership.
  • Handle a leader crashing halfway through a job: define exactly-once or at-least-once semantics and justify the choice.
  • Test by pausing the leader process for longer than the lease and verifying no duplicate side effects.

Stretch goals

  • Remove the need for leader election entirely by partitioning jobs across instances.
  • Measure failover time and reduce it without increasing false failovers.