HS Algorithm for Leader Election in Distributed Systems

Arpit Bhayani

Arpit Bhayani

Aug 24, 2022 • 8 min read

Play

In any distributed architecture that relies on a coordinator or primary node, leader failure is an inevitability. When the leader crashes, the system must either wait for manual intervention or automatically recover. Manual recovery introduces high latency and operational overhead, making automatic leader election a fundamental requirement for building self-healing distributed systems.

While classic ring election algorithms like the LeLann-Chang-Roberts (LCR) algorithm operate on unidirectional rings with a communication complexity of O(n2)\mathcal{O}(n^2), the Hirschberg-Sinclair (HS) algorithm optimizes this to O(nlogn)\mathcal{O}(n \log n) messages by leveraging bidirectional communication and exponential neighborhood exploration.


Core Assumptions and Network Model

The HS algorithm operates under specific structural and operational assumptions:

  1. Bidirectional Ring Topology: Nodes are arranged in a ring where each node knows only its two immediate neighbors: left and right (clockwise and counter-clockwise). Nodes do not need to know the global layout or any nodes beyond their immediate neighbors.
  2. Unknown Network Size (nn): The algorithm functions correctly even when individual nodes do not know the total count of nodes in the network.
  3. Unique Comparable Identifiers (UIDs): Every node possesses a unique, totally ordered identifier (e.g., integers). The node with the largest UID is elected as the leader.
  4. Synchronous Execution: The election proceeds in distinct, synchronous phases (rounds). All participating nodes advance through phases concurrently upon election initiation.
graph LR
    NodeA((Node 3)) <--> NodeB((Node 7))
    NodeB <--> NodeC((Node 9))
    NodeC <--> NodeD((Node 1))
    NodeD <--> NodeE((Node 5))
    NodeE <--> NodeA

The Problem with O(n2)\mathcal{O}(n^2) Ring Election (LCR)

In unidirectional algorithms like LCR, every node initiates a message containing its UID that travels around the ring. In the worst-case scenario (e.g., nodes arranged in decreasing order of UIDs), smaller UIDs travel multiple hops before encountering a larger UID and getting discarded. This results in O(n2)\mathcal{O}(n^2) total messages sent across the network, creating significant network congestion as cluster size scales.

To achieve O(nlogn)\mathcal{O}(n \log n) complexity, the HS algorithm prunes candidates aggressively using the concept of local maxima before allowing messages to traverse long distances.


Core Intuition: Local Maxima to Global Maxima

Instead of sending a message entirely around the ring from day one, each candidate node verifies whether it is the largest node within an exponentially expanding neighborhood:

  • Phase 0: Is my UID the largest in a neighborhood of distance 20=12^0 = 1 hop?
  • Phase 1: Is my UID the largest in a neighborhood of distance 21=22^1 = 2 hops?
  • Phase 2: Is my UID the largest in a neighborhood of distance 22=42^2 = 4 hops?
  • Phase ii: Is my UID the largest in a neighborhood of distance 2i2^i hops?

If a node finds that an adjacent node within distance 2i2^i has a higher UID, it immediately concedes: it will never become the global leader. It steps out of contention and ceases initiating new probe messages in subsequent phases. However, it continues to act as a relay for other surviving nodes.

Because the neighborhood size doubles at each phase (1,2,4,8,,2i1, 2, 4, 8, \dots, 2^i), the maximum number of phases is log2n\lceil \log_2 n \rceil. At each step, the density of surviving candidate nodes decreases proportionally, capping the total communication complexity at O(nlogn)\mathcal{O}(n \log n).


The Message Structure

To coordinate bidirectional traversal and track distances without global coordinates, probe messages contain three key fields:

Message=UID,HopCount,Direction\text{Message} = \langle \text{UID}, \text{HopCount}, \text{Direction} \rangle

  • UID: The candidate node’s unique identifier.
  • HopCount: The remaining distance the probe must travel in this phase (initialized to 2i2^i).
  • Direction: Indicates travel path (outbound vs. inbound/reply, and clockwise vs. counter-clockwise).

Algorithm Mechanics Step-by-Step

1. Phase Initialization

At phase ii, every node that survived phase i1i-1 generates two identical probe messages containing its UID and a hop limit of 2i2^i. It sends one message clockwise (right) and one counter-clockwise (left).

2. Intermediate Node Processing (Outbound Probe)

When node vv receives an outbound probe u,h,dir\langle u, h, \text{dir} \rangle from a neighbor:

  1. Compare UIDs:
    • If u>vu > v (Probe UID is greater):
      • If h>1h > 1: Node vv decrements hh (hh1h \leftarrow h - 1) and relays the probe in the same direction.
      • If h=1h = 1: The probe has reached the boundary of its neighborhood for this phase. Node vv converts the message into an inbound_reply and sends it back in the reverse direction toward uu.
    • If u<vu < v (Probe UID is smaller):
      • Node vv simply discards the probe message. It does not forward it, nor does it reply. Candidate uu is eliminated from future phases.
    • If u==vu == v (Probe returned to sender):
      • Node vv has received its own probe message from the ring. This occurs when 2in2^i \ge n. Because the probe was not discarded anywhere along the ring, vv has the globally maximal UID. Node vv declares itself the leader.

3. Return Path (Inbound Reply)

When a boundary node turns a probe around, the reply travels back toward the originating candidate:

  • Intermediate relay nodes simply forward inbound replies back to the originator.
  • If candidate uu receives replies from both the left and right directions, it successfully survives phase ii.
  • Candidate uu then increments its phase counter (ii+1i \leftarrow i + 1) and initiates the next round with hop count 2i+12^{i+1}.

4. Victory Announcement

Once a node detects u==vu == v, it initiates an election termination broadcast. It sends an announcement message in both directions around the ring informing all other nodes of the new leader’s identity.


Concrete Execution Trace

Consider a small ring of 3 nodes: [Node 3] <-> [Node 7] <-> [Node 9].

sequenceDiagram
    autonumber
    participant N3 as Node 3
    participant N7 as Node 7
    participant N9 as Node 9

    Note over N3,N9: Phase 0 (Hops = 2^0 = 1)
    N7->>N3: Probe(7, hops=1, Left)
    N7->>N9: Probe(7, hops=1, Right)
    Note over N3: 7 > 3: Reach hop limit (1) -> Send Reply
    N3-->>N7: Reply(7, Right)
    Note over N9: 7 < 9: Discard probe
    Note over N7: Only 1 reply received -> N7 drops out

    N9->>N7: Probe(9, hops=1, Left)
    N9->>N3: Probe(9, hops=1, Right)
    Note over N7: 9 > 7: Send Reply
    Note over N3: 9 > 3: Send Reply
    N7-->>N9: Reply(9, Right)
    N3-->>N9: Reply(9, Left)
    Note over N9: 2 replies received -> N9 advances to Phase 1
  1. In Phase 0 (20=12^0 = 1 hop):

    • Node 7 sends probes with UID 7 to Node 3 and Node 9.
    • Node 3 receives UID 7 (7>37 > 3). Because h=1h=1, Node 3 sends a reply back to Node 7.
    • Node 9 receives UID 7 (7<97 < 9). Node 9 silently drops the message.
    • Node 7 never receives a reply from its right side and drops out of future contention.
    • Node 9 sends probes with UID 9 to Node 7 and Node 3. Both see 9>self9 > \text{self}, reached limit h=1h=1, and reply.
    • Node 9 receives both replies and advances to Phase 1.
  2. In subsequent phases, Node 9 eventually traverses the entire ring, receives its own probe message, and becomes the leader.


Complexity Analysis

Communication Complexity

  • In phase ii, a surviving node sends messages up to a distance of 2i2^i in both directions. The round trip for both sides requires at most 4×2i4 \times 2^i messages.
  • How many nodes can survive phase i1i-1 and participate in phase ii? A node can only survive if its UID is the strictly largest among all nodes within distance 2i12^{i-1}. Therefore, candidate nodes must be separated by at least 2i12^{i-1} hops.
  • The maximum number of surviving candidates in phase ii is at most: n2i1+1<n2i1\le \frac{n}{2^{i-1} + 1} < \frac{n}{2^{i-1}}
  • Multiplying surviving candidates by message cost per candidate: Messages in phase i(n2i1)×(42i)=n×4×2=8n\text{Messages in phase } i \le \left(\frac{n}{2^{i-1}}\right) \times (4 \cdot 2^i) = n \times 4 \times 2 = 8n
  • The number of phases is bounded by log2n\lceil \log_2 n \rceil.
  • Total message complexity across all phases is: i=0log2n8n=O(nlogn)\sum_{i=0}^{\lceil \log_2 n \rceil} 8n = \mathcal{O}(n \log n)

Time Complexity

Because the distance traversed doubles in each phase, the total time (rounds of synchronous message passing) is:

i=0log2n22i=O(n)\sum_{i=0}^{\lceil \log_2 n \rceil} 2 \cdot 2^i = \mathcal{O}(n)

The algorithm achieves high communication efficiency (O(nlogn)\mathcal{O}(n \log n) messages) while maintaining linear time complexity (O(n)\mathcal{O}(n) time steps).


Comparison: LCR vs. HS Algorithm

FeatureLCR AlgorithmHirschberg-Sinclair (HS) Algorithm
Ring TopologyUnidirectional (one direction only)Bidirectional (left and right neighbors)
Message Complexity (Worst Case)O(n2)\mathcal{O}(n^2)O(nlogn)\mathcal{O}(n \log n)
Message Complexity (Best Case)O(n)\mathcal{O}(n)O(nlogn)\mathcal{O}(n \log n)
Time ComplexityO(n)\mathcal{O}(n)O(n)\mathcal{O}(n)
Network Size (nn) Known?Not requiredNot required
Node State ComplexityMinimal (stateless relay)Requires tracking phase and reply status
Network OverheadHigh congestion on large clustersMinimal message footprint

Summary

The Hirschberg-Sinclair algorithm balances elegance and efficiency for ring networks. By dividing the election into synchronous phases and restricting message propagation using hop counters (2i2^i), the algorithm eliminates sub-optimal candidate nodes locally before they can flood the global network. This local-to-global maxima verification lowers the communication complexity from O(n2)\mathcal{O}(n^2) down to O(nlogn)\mathcal{O}(n \log n), making it one of the foundational leader election algorithms in distributed systems theory.

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses