


























Everything we interact with today is a “distributed system”. From microservices to cloud-native applications, from databases to message queues. We are constantly building systems that span multiple machines. But there comes a fundamental challenge - how do we ensure that all independent nodes agree on shared state?
This is where consensus algorithms like Raft come in. While I talk about Raft in some other write-up, in this one, let me help you understand why there is a need for consensus and why it is a tough nut to crack.
At first glance, getting multiple machines to agree seems straightforward. Can’t we just have one machine make decisions and tell the others? Or use timestamps to order operations? Unfortunately, distributed systems introduce complexities that make these naive approaches fail catastrophically.
Distributed systems face several simultaneous challenges; here are some of them.
Network Partitions: Networks can split, leaving groups of nodes unable to communicate. Unlike a simple “network down” scenario, partitions create islands of nodes that continue operating independently.
Node Failures: Machines crash, sometimes permanently, sometimes temporarily. They might restart with partial memory loss or a corrupted state.
Asynchronous Communication: Message delivery times are unpredictable. A message sent first might arrive second, or might never arrive at all.
Clock Skew: Different machines have different notions of time, making timestamp-based ordering unreliable.
Majority Vote: Sounds simple, but what happens during network partitions? You might have multiple groups, each thinking they’re the majority.
Imagine a distributed banking system with account data replicated across three servers in different data centers. A customer has 1000andtriestowithdraw1000 and tries to withdraw 800. Simultaneously, their spouse tries to withdraw $500 from a different location.
Without proper consensus:
You can consider these servers A, B, and C are geographically distributed database nodes, and all of them are capable of accepting and handling writes.
Now you can see why consensus is a legitimate real-world problem.
Consensus algorithms (like Paxos or Raft) provide two fundamental guarantees
Safety: Nothing bad ever happens. In practical terms:
Liveness: Something good eventually happens. In practical terms:
Given a set of nodes in a distributed system, consensus algorithms solve the problem of getting all non-faulty nodes to agree on a single value, even in the presence of failures and network issues.
The algorithm must satisfy:
Databases like PostgreSQL with streaming replication need to elect a primary after the current primary fails. Without consensus, you might end up with split-brain scenarios where multiple nodes think they’re the primary.
When a transaction spans multiple database shards, all participants must agree on whether to commit or abort. The two-phase commit protocol is essentially a consensus algorithm.
Systems like Apache Kafka use consensus (via Zookeeper or KRaft) to manage broker membership, partition assignments, and configuration changes.
When services start up, they need to register with a service discovery system. Multiple discovery nodes must agree on the current set of healthy services.
Changing application configuration across a fleet of microservices requires consensus to ensure all services get the same configuration version.
Kubernetes etcd stores cluster state and scheduling decisions. Multiple master nodes must agree on pod placements and resource allocations.
The cluster must agree on which nodes are healthy and available for scheduling workloads.
Consensus algorithms like Raft are the foundation of reliable distributed systems.
The next time you are building a system that needs to keep state in sync across services or data centers, remember that Raft and similar algorithms have already solved the hardest problems. Your task is to know when and how to use them effectively.
Also, knowing them helps you understand why and how distributed systems are both a beauty and a beast.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。