Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Election algorithms

Leader (election) algorithms choose one process as coordinator in a distributed system; common algorithms are Bully, ring/Chang-Roberts and Hirschberg–Sinclair and they trade message and time costs under different failure and synchrony assumptions.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Leader election is needed when a distributed system needs a single coordinator (for example, to serialize updates, perform global tasks, or act as a primary). Engineers use election algorithms during node joins, coordinator crashes, partition recovery, and when starting distributed services that require a single leader (see also Consensus: Paxos and Raft).

Key ideas

  • Crash-stop failure — a process may stop and never recover (common failure model for many election algorithms). Define the failure model before running any algorithm.
  • Synchronous system — a model where bounds on message delivery time and processing time exist (algorithms can use timeouts reliably).
  • Asynchronous system — no timing bounds; timeouts cannot distinguish slow from crashed.
  • Correctness properties (commonly required):
    • Uniqueness — at most one leader is elected.
    • Termination — eventually every non-faulty process decides on a leader.
    • Agreement — all non-faulty processes decide on the same leader identifier.
    • Validity — the elected leader is a correct process and (for these algorithms) is the one with the maximal ID among live processes. (Some definitions combine uniqueness and agreement; state what your exam expects.)

Basic idea: give each process a unique identifier (ID). Use message exchanges to compare IDs and eliminate candidates until one remains. Algorithms differ by topology assumption (fully connected, ring), synchrony, and efficiency.

Common algorithms and their assumptions:

  1. Bully algorithm (for fully connected networks, assumes process IDs are totally ordered and reliable message delivery; usually assumes synchronous timeout for detecting crash):

    • Any process detecting leader failure starts an election by sending an ELECTION message to all processes with higher ID.
    • If no higher-ID process responds, the starter becomes leader and sends COORDINATOR to all lower IDs.
    • If a higher-ID process responds, the starter stops and waits for COORDINATOR; the higher-ID process itself will start an election if needed.
    • Assumptions: unique IDs, processes can detect crash via timeouts, messages delivered reliably. Assumes a single connected component; network partitions can temporarily yield multiple leaders (split-brain) unless handled by higher-level mechanisms.
    • Informal correctness: highest alive ID will always win (it can always respond and will start election, and lower IDs defer). Termination follows if the highest alive eventually responds.
  2. Chang–Roberts algorithm (ring, unidirectional):

    • Processes are arranged in a logical ring with a fixed direction for messages. Each process forwards the largest ID seen so far.
    • An initiator sends its ID to successor. When a process receives an ID:
      • if ID > own ID, forward that ID;
      • if ID < own ID, replace it with own ID and forward (forward your own ID at most once per election to avoid duplicates);
      • if ID == own ID, that process is leader and broadcasts a COORDINATOR around the ring.
    • Assumes reliable delivery around ring and unique IDs. Works in asynchronous rings. The ring must remain connected (no crashes that break the ring) during the election, or there must be a bypass/repair mechanism.
  3. Hirschberg–Sinclair (HS) algorithm (efficient ring algorithm using doubling):

    • Improvement over Chang–Roberts that reduces messages by limited scope probes.
    • Election proceeds in phases k = 0,1,2,... where in phase k a process sends probes distance 2^k both ways; a process continues as candidate only if it is the largest among the reachable set in that phase.
    • HS achieves asymptotically fewer messages (about O(n log n) total) compared with naive ring algorithms. Assumes ring topology and reliable messages.

What to draw (exam diagram instructions):

  • For Bully: draw n nodes arranged loosely, label each with its ID (e.g., 2,4,7,9). Draw arrows from lower-ID node to all higher-ID nodes when it starts election, label messages ELECTION/OK/COORDINATOR.
  • For Ring/Chang–Roberts: draw nodes in a circle labelled with IDs in their ring order. Mark message flows around the circle in the single direction. Show one ID circulating, compare with local ID, show discards and final return.

Proof sketches commonly required:

  • Show uniqueness: argue that only one process (the one with highest live ID) can satisfy the condition that makes it announce COORDINATOR (Bully) or return its own ID (ring).
  • Show termination: argue that messages progress and there are finite processes; the maximal live ID cannot be blocked indefinitely.

Relations to other topics: Election is simpler than full distributed consensus (Paxos/Raft) but is used as a subroutine (e.g., to select a leader before running consensus). Election algorithms assume particular topologies (ring, fully-connected); compare with Group communication and Fault tolerance topics when reasoning about failures.

Formulas

M_bully_worst = O(n^2) — worst-case message complexity for Bully. Here n is number of processes; applies in fully-connected case with concurrent elections.

T_bully = O(n) — time complexity in message-rounds (rough order, depends on synchrony assumptions). Symbols: n = number of processes; applies when leaders are detected and higher IDs reply in sequence.

M_CR_worst = O(n^2) — worst-case message complexity for Chang–Roberts (unidirectional ring). n = number of processes; worst-case when IDs are arranged so many smaller IDs circulate.

T_CR = O(n) — time (number of hops) until leader found; applies to ring.

M_HS = O(n log n) — message complexity for Hirschberg–Sinclair.

T_HS = O(n) — time (number of hops/rounds) for Hirschberg–Sinclair.

Notes: These are asymptotic; exact constants depend on initiation pattern and fault model. Use these in exams to compare algorithms. Also know best-case bounds: Bully O(n) messages, Chang–Roberts O(n) messages, Hirschberg–Sinclair O(n) messages (excluding the final COORDINATOR announcement where applicable).

Worked examples

Example 1 — Bully algorithm trace (small fully-connected system):

  • Nodes with IDs {2, 4, 7, 9, 11}. Assume 11 was leader but crashes. Node 4 detects failure and starts election.
  1. Node 4 sends ELECTION to {7,9,11}. Messages to 7 and 9 are delivered; 11 is down so that message gets no reply.
  2. Nodes 7 and 9 reply OK to 4 (they are higher). Each of 7 and 9 then starts its own election (if not already in progress). Node 7 sends ELECTION to {9,11}; 9 replies OK and 11 is down. Node 9 sends ELECTION to {11}; no reply, so 9 becomes leader and sends COORDINATOR to {2,4,7}.
  3. After receiving COORDINATOR(9), all accept 9 as leader.
  • Final leader: 9. Messages counted (delivered): 4->(7,9) ELECTIONs = 2; 7->4 OK = 1; 9->4 OK = 1; 7->9 ELECTION = 1; 9->7 OK = 1; 9->(2,4,7) COORDINATOR = 3. Total ≈ 9 messages in this trace (messages to crashed 11 are sent but not delivered). In worst case many overlapping elections cause O(n^2).

Example 2 — Chang–Roberts trace (ring of 5 nodes in order clockwise: IDs [3, 9, 5, 2, 7])

  • Suppose node with ID 3 initiates and sends message with value 3 -> successor 9.
  • At 9: received 3; 3 < 9, so 9 forwards its own ID 9.
  • 5 receives 9, 9 > 5 so forwards 9.
  • 2 receives 9 -> forwards 9.
  • 7 receives 9 -> forwards 9.
  • 3 receives 9 -> since 9 > 3, forwards 9; the message continues until 9 returns to its origin: when 9 receives its own ID back, 9 declares leader and sends COORDINATOR around ring.
  • Final leader: 9. Message hops ~5 for the largest ID to make a full loop plus COORDINATOR loop.

Example 3 — small HS phase illustration (ring IDs: [1, 6, 3, 8, 2])

  • Phase 0: each node sends probe distance 1; only local comparisons happen; highest in its neighborhood continues.
  • Phase 1: survivors send probes distance 2 in both directions (2^1). If a node sees a higher ID within that window, it drops out; otherwise it continues. After O(log n) phases the global maximum remains and declares leader.
  • This reduces message blow-up compared with naive CR for large n.

(Exams expect step-by-step message lists as above; give message names and hop counts.)

Common mistakes

  • Mixing synchronous and asynchronous timeouts: using timeouts in an asynchronous system without qualification.
  • Wrong message-count arithmetic: forget that multiple processes may start concurrent elections (Bully) increasing cost.
  • In ring algorithms, forgetting unidirectional vs bidirectional message flow (CR is unidirectional; HS uses both directions).

For your exam

Typical question types:

  • Definitions and short notes: define leader election, uniqueness, termination, different failure models.
  • Derivations: show message/time complexity of Bully, Chang–Roberts, HS; give worst-case and best-case.
  • Traces: give a small network and ask for a full step-by-step trace of Bully or ring algorithm messages.
  • Proofs: show uniqueness and termination for a given algorithm under stated assumptions.
  • Diagrams: draw a ring and show the message sequence.

What full-marks answers include:

  • A clear statement of assumptions (topology, IDs unique, failure model, synchrony or not).
  • Correct algorithm steps and termination condition.
  • Clear message trace or complexity analysis with correct accounting of messages and rounds.
  • A short correctness argument (why highest live ID wins) and note on failure scenarios.

Quick check

  1. What property ensures at most one leader is chosen?
  2. Which algorithm arranges processes in a logical ring and forwards the largest seen ID?
  3. Which algorithm reduces messages using probes of distance 2^k per phase?

Answers: 1) Uniqueness; 2) Chang–Roberts algorithm; 3) Hirschberg–Sinclair algorithm

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?