The idea: one critical section, and only messages

Threads on one machine protect shared data with a mutex: a variable in shared memory that says who is inside. Processes on different machines share no memory. They still sometimes need one critical section (CS): only one of them may write a shared file, print, or update a record at a time. All they can do is send messages. Distributed mutual exclusion is the set of rules for those messages. The page runs the three classic algorithms on the same four processes P1 to P4: a central coordinator, Ricart–Agrawala and a token ring.

Three small diagrams side by side. Central: P1 to P4 around a coordinator C; P1 sends REQUEST to C, C sends GRANT back, P1 sends RELEASE when done: 3 messages per entry. Ricart–Agrawala: P1 sends REQUEST to P2, P3 and P4 and gets a REPLY from each: 2(N−1) = 6 messages per entry. Token ring: a TOKEN goes P1 to P2 to P3 to P4 and back to P1; only its holder may enter.
The same job, three message patterns: ask one coordinator, ask everyone, or wait for the token.
On the canvasMeaning
circles P1 to P4 (and C)processes; white = RELEASED, yellow = WANTED, green = in the CS, grey = down ✗
boxes above / below the rowmessages going right / left; each takes exactly one tick; a grey box with ✗ was lost
REQUEST(1,P3), L = 1Ricart–Agrawala: a request with timestamp 1 from P3; each process's Lamport clock
CRITICAL SECTION boxwho is inside (at most one, or the box says ✗)
right paneleach process's state, replies and deferred replies; the messages by type and per entry; each entry's asked → in → out times; the synchronization delay

What a solution must guarantee

  • Safety: at most one process is in the CS at any time.
  • Liveness: every request is granted in the end; no deadlock and no starvation.
  • Ordering (fairness): if one request happened before another, it is served first.

Algorithms are compared by three costs: the messages per entry, the client delay (from asking to entering when nobody else wants the CS) and the synchronization delay (from one process leaving to the next waiting one entering). The last one limits how many entries per second the system can do under load. Here every message takes one tick and a process stays inside for 2 ticks, so delays are counted in ticks.

The central coordinator

One process, the coordinator C, plays the mutex. A process sends REQUEST to C. If the CS is free, C answers GRANT; otherwise it puts the process at the end of a FIFO queue and answers nothing yet. When the holder leaves it sends RELEASE, and C sends GRANT to the head of the queue. That is 3 messages per entry, and the client delay is 2 ticks (REQUEST, then GRANT). In Demo 1: P1 and P3 ask at once, both REQUESTs reach C at t = 1. P1 enters at t = 2 and leaves at t = 4; P3 waits in the queue and enters at t = 6. Synchronization delay: 2 ticks, because RELEASE must reach C before GRANT can go out. Total 6 messages for 2 entries.

Ricart–Agrawala: ask everyone

Ricart and Agrawala (1981) removed the coordinator. Each process keeps a Lamport clock L:

  1. to ask, a process sets L = L + 1 and sends REQUEST(L, id) to all N − 1 others;
  2. a process that gets REQUEST(ts, j) sets L = max(L, ts). It replies at once, unless it is in the CS, or it wants the CS itself and its own (ts, id) is smaller. Then it defers the REPLY: it notes j and answers later;
  3. with N − 1 REPLYs a process enters; when it leaves, it sends every deferred REPLY.

That is 2(N − 1) = 6 messages per entry for N = 4, and the client delay is 2 ticks (REQUEST, then REPLY). Pairs (ts, id) are compared by timestamp first and id second, so two requests are never equal: every process orders them the same way. In Demo 3: P1 and P3 ask at once, both send timestamp 1. P1 gets P3's REQUEST(1,P3), sees (1,P1) < (1,P3) and defers. P3 gets REQUEST(1,P1), sees that P1's pair is smaller and replies. P1 has 3 REPLYs at t = 2. When it leaves at t = 4, its deferred REPLY reaches P3 at t = 5 and P3 enters. Synchronization delay: 1 tick, one message instead of the coordinator's two. Total 12 messages for 2 entries.

Time runs down, columns P1 to P4. t=0: P1 sends REQUEST(1,P1) and P3 sends REQUEST(1,P3) to the three others. t=1: P2 and P4 reply to both, P3 replies to P1, and P1 defers its reply to P3 because (1,P1) is less than (1,P3). t=2: P1 has 3 replies and enters the CS. t=4: P1 leaves and sends the deferred REPLY to P3. t=5: P3 enters. t=7: P3 leaves.
Demo 3: the smaller pair (1,P1) wins; P1 holds back one REPLY, and sending it is what lets P3 in.

The smaller timestamp wins, not the smaller id

The id only breaks ties. In Demo 4: P1 asks one tick after P4, P4 asks at t = 0 with REQUEST(1,P4). At t = 1 that request has reached P1 (its clock becomes 1), and only then does P1 ask, so its request is REQUEST(2,P1). P4 defers P1 because (1,P4) < (2,P1), even though 1 is the smaller id. P4 enters at t = 2 and P1 at t = 5. The clock rule makes "P1 asked after it had seen P4's request" visible in the numbers, so requests are served in happened-before order. The rules of Lamport clocks themselves are on the Lamport clocks page.

Token ring: wait for the token

The processes form a logical ring, P1 → P2 → P3 → P4 → P1, and one message, the TOKEN, travels around it. Only the holder may enter. A holder that does not want the CS passes the token on; one that wants it enters, and passes the token on when it leaves. Safety is simple: there is one token. The wait depends on where the token is: 0 to N − 1 hops. In Demo 6: P4 asks, the token is at P1, it takes 3 hops (P1 → P2 → P3 → P4), so P4 enters at t = 3: the worst case N − 1. Under heavy load the ring is cheap: in Demo 7: all four ask at once, P1 enters at once, then each exit hands the token to a waiting neighbour, 4 entries for 4 TOKEN messages (1 per entry) with a synchronization delay of 1 tick. When nobody wants the CS, a real ring keeps passing the token anyway, so the messages per entry can be any number. (The page stops the clock between actions, so an idle token rests.)

When a process or the token is lost

None of the three algorithms survives a crash on its own:

  • Central (Demo 2: the coordinator crashes): P2's REQUEST to C is lost and P2 waits forever. C is a single point of failure; the others must notice and pick a new coordinator, for example with the algorithms on the leader election page. The new one has to rebuild the queue by asking everyone.
  • Ricart–Agrawala (Demo 5: P4 has crashed): P2 gets 2 of 3 REPLYs and waits forever. Every process must answer, so there are N points of failure instead of one. Fixes need a failure detector and a group membership list, and then the algorithm counts only live members.
  • Token ring (Demo 8: P1 crashes with the token): the token is gone and nobody can enter. Someone must notice (a timeout) and regenerate the token, and must make sure that the old one is really gone, or two tokens break safety. A crash that does not hold the token only needs the ring to be mended around the dead process.

Side by side

Central coordinatorRicart–AgrawalaToken ring
messages per entry32(N − 1) = 61 under heavy load; unbounded when idle
client delay (alone)2 ticks2 ticks0 to N − 1 = 3 hops
synchronization delay2 ticks1 tick1 to N − 1 hops
P1 and P3 ask at t = 06 messages, P3 in at t = 612 messages, P3 in at t = 53 TOKEN hops (token at P1), P3 in at t = 4
what one crash breaksthe coordinatorany processthe token holder (token lost)
order of servicearrival at C (FIFO)(timestamp, id)ring position

The surprise is that the decentralized algorithm is the most expensive and the least robust. The coordinator is cheap and simple, and its single point of failure can be removed by electing a replacement. That is what real systems do: they run the lock inside a small replicated service whose leader is elected by majority vote.

See also distributed locks with Redis (a lock service with leases and fencing tokens, the practical heir of the coordinator), Raft and Paxos (how a replicated coordinator agrees on who holds the lock), leader election (Bully and ring elections) and Lamport clocks (the timestamps used here).

See also totally ordered multicast, which uses the same Lamport timestamps and hold-back queue to give every replica one delivery order.

What the page leaves out

Lamport's own 1978 algorithm (3(N − 1) messages, with a RELEASE broadcast and FIFO channels), Maekawa's quorum algorithm (about √N messages, but it can deadlock), the Suzuki–Kasami broadcast token and Raymond's tree token, the Roucairol–Carvalho optimisation of Ricart–Agrawala (no new REQUEST to a process that has not asked since), failure detectors and token regeneration, messages that are delayed by different amounts or lost by the network, and a token that keeps moving while nobody wants it.

On the mind map

Open the Distributed Systems mind mapDistributedSystemsReplication: copies of data · what reads seeReplicationPartitioning: split data over nodesPartitioningTime and membership: order events, know who is aliveTime and membershipConsensus: agree on one value or logConsensusTransactions: one change across many servicesTransactionsMessaging: send events between systemsMessagingKafka internals: a replicated append-only logKafka internalsRedis internals: one thread, data in memoryRedis internalsKubernetes: reconcile desired stateKubernetesResilience and speed: stay up and fast under loadResilience and speedLamport and vector clocksLamport and vector clockslogical time; vectors spot concurrencyOrdered multicastOrdered multicastTotal order: same delivery order everywhereDistributed mutual exclusion3 vs 2(N−1) messages vs one tokenChandy-Lamport snapshotChandy-Lamport snapshotmarkers record a consistent cutGossip and SWIMGossip and SWIMspread in log N rounds; ping-req probesBully vs ring electionBully vs ring electionhighest id wins; ring passes a tokenHybrid logical clocksHybrid logical clockswall time plus counter; TrueTime waitsVector clocks and siblingsVector clocks and siblingsneither vector greater means concurrentOpen the Distributed Systems mind mapDistributed Systemsorder events, know who is aliveTime and membershipLamport and vector clocksLamport and vector clockslogical time; vectors spot concurrencyOrdered multicastOrdered multicastTotal order: same delivery order everywhereDistributed mutual exclusion3 vs 2(N−1) messages vs one tokenChandy-Lamport snapshotChandy-Lamport snapshotmarkers record a consistent cutGossip and SWIMGossip and SWIMspread in log N rounds; ping-req probesBully vs ring electionBully vs ring electionhighest id wins; ring passes a tokenHybrid logical clocksHybrid logical clockswall time plus counter; TrueTime waitsVector clocks and siblingsVector clocks and siblingsneither vector greater means concurrent
This page on the Distributed Systems mind map · 100 pages in 10 branches · open it to see the whole map and test yourself in recall mode.