Leader Election in Distributed Systems
Why is leader election important and complicated?

Just a guy who loves to write code and watch anime.
Search for a command to run...
Why is leader election important and complicated?

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

In the world of distributed systems, it's important to keep everything running smoothly, even if a server or node stops working. That's why the idea of leader election is very important.
Think of a well-known subscription service, like Netflix, where users are charged regularly through payment services like Stripe or PayPal. To handle transactions without sharing user credit card information with the payment service, a middleman service steps in to manage the charges.
However, using just one middleman service creates a risk. If it fails, all transactions stop, which can mess up the subscription system. To avoid this, we use several instances of the service for backup. But this leads to a new problem: we need to make sure only one instance (the leader) processes transactions at a time to prevent charging customers more than once.
Leader election is how different parts of a system agree on choosing a leader to manage tasks, like our service that handles credit card charges. If the leader stops working, they pick a new one to keep things running smoothly.
But, deciding on the new leader isn't easy. Problems like network partitions (when parts of the system can't talk to each other) and the split-brain scenario (when two parts pick different leaders, causing confusion) make the process challenging.
Consensus algorithms make sure that all nodes in a distributed system agree on one main truth, even if there are failures or network problems. Choosing a leader is part of this process, where one node is picked to be the leader.
Paxos: This algorithm is a basic way to reach agreement but is often seen as complex to understand and put into practice.
Raft: Known for being easy to understand, Raft simplifies the process of reaching a consensus.
Zab: This is used by Apache ZooKeeper for choosing a leader, aiming for fast and reliable communication.
Creating consensus algorithms from the ground up is tough and usually not needed. Instead, distributed systems use existing libraries and tools that include these algorithms, offering ready-made solutions for things like choosing a leader.
ZooKeeper: Provides coordination services for distributed applications, including leader election, configuration management, and synchronization.
etcd: A highly available, strongly consistent key-value store used for shared configuration and service discovery, using the Raft consensus algorithm.
Consul: Offers service discovery, configuration, and orchestration capabilities, also based on the Raft algorithm.
Etcd is a great example of a distributed key-value store that guarantees strong consistency, which is very important for choosing a leader. In our case of a subscription service, using etcd makes sure that at any moment, everyone agrees on who the main middleman service is. This stops users from being charged more than once for the same billing period.
Strong consistency in etcd means that every time you read or write data, you get the latest information. This is essential to make sure that when a new leader is chosen, the whole system knows about it right away.