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.
| On the canvas | Meaning |
|---|---|
| circles P1 to P4 (and C) | processes; white = RELEASED, yellow = WANTED, green = in the CS, grey = down ✗ |
| boxes above / below the row | messages going right / left; each takes exactly one tick; a grey box with ✗ was lost |
REQUEST(1,P3), L = 1 | Ricart–Agrawala: a request with timestamp 1 from P3; each process's Lamport clock |
| CRITICAL SECTION box | who is inside (at most one, or the box says ✗) |
| right panel | each 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:
- to ask, a process sets
L = L + 1and sendsREQUEST(L, id)to all N − 1 others; - a process that gets
REQUEST(ts, j)setsL = 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 theREPLY: it notes j and answers later; - 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.
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 coordinator | Ricart–Agrawala | Token ring | |
|---|---|---|---|
| messages per entry | 3 | 2(N − 1) = 6 | 1 under heavy load; unbounded when idle |
| client delay (alone) | 2 ticks | 2 ticks | 0 to N − 1 = 3 hops |
| synchronization delay | 2 ticks | 1 tick | 1 to N − 1 hops |
| P1 and P3 ask at t = 0 | 6 messages, P3 in at t = 6 | 12 messages, P3 in at t = 5 | 3 TOKEN hops (token at P1), P3 in at t = 4 |
| what one crash breaks | the coordinator | any process | the token holder (token lost) |
| order of service | arrival 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.