TimeSlice algorithm for Leader Election in Distributed Systems



834 views Distributed Systems



Leader Election is a critical component in any distributed system. It enables the system to auto-recover from leader failures. When a leader node goes down, the Leader Election algorithm is triggered to elect the new leader.

TimeSlice Algorithm

TimeSlice algorithm for leader election is highly impractical, unbounded, yet an interesting algorithm to understand.

The algorithm assumes

  • each node has a UID that is a positive integer
  • the nodes are arranged in a virtual ring
  • each node knows its immediate neighbor to the right
  • each node knows the total number of nodes n in the network

The flow

The election proceeds in phases 1, 2, 3, and so on. Each phase consists of n rounds. Because the algorithm is synchronous, each node knows when the algorithm is proceeding with rounds and phases.

In phase i, nodes can only forward the message having the candidature of UID i. Hence, in phase 3, a node will forward the message to the next node, only when it is having the candidature of UID 3.

The flow

In phase 1, the node with UID 1 will send the message with its candidature across to the next node in the ring. If no such node exists, then no message is sent.

Thus, for n rounds within phase 1 there is void silence in the network. Then beings the phase 2.

Hence, we see that the messages will be sent across the ring only when the phase i beings where i is the smallest UID in the network. For all (i - i) * n rounds, there will be void silence in the network.

When phase i begins the node with UID i will know that it is the new leader and it initiates the message and sends it to the next neighbor. For n successive rounds, the message is sent across the network and thus all n nodes know about the new leader i.

Complexity Analysis

The algorithm thus elects the node with the minimum UID as the new leader in just O(n) messages but takes time proportional to the O(n*i).

If the minimum UID is a large integer, then the algorithm will take a longer time to elect the leader, and hence it is unbounded on the number of nodes in the network.


Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 38000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.




Other essays that you might like



Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.


Paid Courses

System Design for Beginners

A masterclass that helps early engineers and product managers become great at designing scalable systems.

300+ learners

Details →

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

1000+ learners

Details →

Redis Internals

Learn internals of Redis by re-implementing some of the core features in Golang.

98+ learners

Details →

Free Courses

Designing Microservices

A free playlist to help you understand Microservices and their high-level patterns in depth.

823+ learners

Details →

GitHub Outage Dissections

A free playlist to help you learn core engineering from outages that happened at GitHub.

651+ learners

Details →

Hash Table Internals

A free playlist to help you understand the internal workings and construction of Hash Tables.

1027+ learners

Details →

BitTorrent Internals

A free playlist to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

692+ learners

Details →