Shivank Gautam

Senior Backend Engineer, Educator

Consensus in Distributed Systems

Consensus is like a group of friends deciding where to eat. Everyone must agree on one place (e.g., pizza), even if some friends are late or disagree. In distributed systems, nodes use consensus to agree on things like:

  • The order of transactions (e.g., in Bitcoin, who spent what first).
  • The current state of data.

Why is it Tricky?

  • Nodes might crash (like your library node failing).
  • Networks can delay or lose messages.
  • Some nodes might act maliciously (Byzantine faults, like lying about data).
  • The CAP theorem makes it hard to balance consistency, availability, and partition tolerance.

Common Consensus Mechanisms (Super Basic) 📚

Paxos:

  • A protocol where nodes vote to agree on a value (like choosing a leader or a transaction).
  • Example: Used in Google’s databases to ensure all servers agree on data updates.
  • Vibe: Like a class election—everyone votes, and the majority wins.
  • Pros: Handles node failures well.
  • Cons: Complex and slow for large systems.

Raft:

  • A simpler version of Paxos, where one node is the “leader” and others follow.
  • Example: Used in systems like etcd (a distributed key-value store).
  • Vibe: Like one friend leading the group to decide on pizza, others follow.
  • Pros: Easier to understand and implement.
  • Cons: If the leader fails, a new one must be elected.

Proof of Work (PoW):

  • Nodes compete to solve a hard math problem to propose the next state (like adding a block in Bitcoin).
  • Example: Bitcoin uses PoW to agree on transactions.
  • Vibe: Like a race where the winner gets to decide the next step.
  • Pros: Secure against malicious nodes.
  • Cons: Slow and uses lots of energy.