← Lessons

quiz vs the machine

Gold1390

Concurrency

Leader Election the Ring Algorithm

Pass a token of ids around a logical ring and pick the max.

5 min read · core · beat Gold to climb

The ring structure

Nodes are arranged in a logical ring: each knows its successor. When a node detects a missing leader it builds an Election message containing its own id and forwards it to the next live successor.

  • Each node receiving the message appends its own id and passes it on.
  • When the message returns to the originator, it has collected every live id.
  • The node with the highest id in the list is the new leader, announced in a second pass with a Coordinator message.

Why a ring

The ring bounds message count to about 2n in the normal case, far better than the bully worst case. The trade off is latency: the message must traverse the whole ring twice, once to gather ids and once to announce.

  • Failures are handled by skipping a dead successor to the next live one.
  • Concurrent elections converge because they collect the same set of ids and pick the same maximum.

Key idea

The ring algorithm circulates a list of ids twice and elects the maximum, trading higher latency for a tidy linear message count.

Check yourself

Answer to earn rating on the learn ladder.

1. How does the ring algorithm choose the leader?

2. What is the main trade off of the ring versus the bully algorithm?