0%
Distributed Systems: Consensus, Clocks, and Replication
Foundations
Time and Order
Replication
Consistency Models
Testing Distributed Systems
Distributed systems fail in ways that ordinary tests rarely exercise. A bug may need a specific message to be delayed while a node pauses and another restarts, an interleaving that happens once in millions of operations in production and never in a unit test. This section explains why such bugs hide, what kinds of failures cause them and why testing distributed systems requires deliberately controlling the things that ordinary tests leave to chance: time, the network, process scheduling and failures.
The state space explodes
A single threaded program given the same input follows the same path. A distributed system of five nodes exchanging messages has an enormous number of possible orderings: every message can be delayed, reordered, duplicated or dropped, every node can crash at any instruction, and clocks can drift. The number of distinct executions grows exponentially with the number of events. Unit tests explore one execution, usually the happy path in which every message arrives promptly. The bugs live in the executions nobody thought to try.
Real failure modes
Studies of production distributed systems catalog what goes wrong. Yuan and colleagues analyzed failures in Cassandra, HBase, HDFS, Hadoop MapReduce and Redis in 2014 and found that most catastrophic failures were caused by incorrect handling of non fatal errors that the system explicitly signaled, and that nearly all could be reproduced on three nodes or fewer. Other studies point to network partitions, partial failures, timeouts that are too short or too long, clock skew, and upgrades where old and new versions interact. These are the failure modes tests must reproduce.
Error handling is where bugs live
The Yuan study found that in a large share of catastrophic failures, the error handling code was trivially wrong: an empty catch block, a TODO comment or a handler that aborted the whole cluster on a recoverable error. This code runs rarely, so it is rarely tested, and when it finally runs in production, it fails. Simple practices catch many of these bugs: test every error path, inject the errors that callers are supposed to handle and review error handlers as carefully as the main path.
Nondeterminism makes bugs flaky
Even when a test does hit a rare interleaving, it usually cannot hit it again. Thread scheduling, network timing and timer firing vary from run to run, so a failing test passes on retry and is dismissed as flaky. Developers lose confidence in the suite and start ignoring failures. The root problem is that the test does not control the sources of nondeterminism, so it cannot replay what happened. The most effective testing techniques for distributed systems all start by taking control of nondeterminism.
What correctness means
Testing requires an oracle: a way to decide whether an execution was correct. For distributed systems the oracle is usually a consistency model. A linearizable key value store must behave as if each operation took effect at a single instant between its start and end. A serializable database must produce results equivalent to some serial order of transactions. A replicated log must never commit different entries at the same index on different nodes. Writing these invariants down precisely is half of the work, and it often reveals that the system never had a clear specification.
Safety and liveness in tests
Safety properties, such as no two leaders in the same term or no committed write lost, can be checked on any finite execution: if the bad thing happened, the test caught it. Liveness properties, such as every request eventually completes, cannot be violated by a finite prefix, so tests check them with timeouts after faults are healed: once the network is repaired, the system must make progress within some bound. Good test harnesses check both, injecting faults for a while, then healing everything and verifying recovery.
A spectrum of techniques
There is no single technique that finds all distributed bugs. Formal methods such as TLA plus find design bugs before code is written. Deterministic simulation finds implementation bugs by running the real code under controlled, reproducible fault schedules. Black box tests such as Jepsen run real deployments under faults and check consistency from the outside. Chaos engineering finds operational weaknesses in production. Each sits at a different point between thoroughness and realism, and mature teams use several. The rest of this lesson covers each in turn.
The cost of not testing
The consequences of distributed bugs are severe: lost writes, split brain, corrupted replicas and outages lasting hours. Many have been found by external testers after systems were widely deployed: Jepsen analyses have reported lost acknowledged writes or consistency violations in MongoDB, Elasticsearch, Redis, etcd, Cassandra, CockroachDB and many others at various versions, most of which were then fixed. Investing in testing before users find the bugs is far cheaper than recovering corrupted data after.
Partial failure is the normal case
In a single machine, a failure usually stops everything at once: the process crashes and restarts from a clean state. In a distributed system, parts fail while the rest keep running. A node may be reachable from some peers but not others, a disk may be slow rather than dead, and a request may reach a server whose reply is lost. The system must keep making correct decisions with incomplete and contradictory information. Code written for clean crashes often behaves badly under these partial failures, and tests that only kill processes cleanly never see the difference.
Timeouts encode assumptions
Almost every distributed system uses timeouts to detect failures, and every timeout encodes an assumption about how long things normally take. When the assumption is wrong, behavior changes sharply. A timeout that is too short causes false failure detection, unnecessary elections and retry storms under load. A timeout that is too long leaves clients waiting on a dead node. Tests should vary latency deliberately, including slow but not dead peers, garbage collection pauses and overloaded disks, to see whether timeouts hold up under conditions that production will eventually produce.
Upgrades and mixed versions
Many outages happen during deployments, when old and new versions of the software run side by side. A new version may write a field the old one does not understand, change a message format or alter the meaning of a configuration flag. Rolling upgrades mean the cluster spends real time in a mixed state that neither version was tested against alone. Good test suites run mixed version clusters, upgrade and downgrade nodes during workloads and check that invariants hold throughout, because this is one of the most common windows for data loss.
Configuration is code
Configuration errors are a leading cause of large outages. A wrong replication factor, a mismatched timeout between client and server or a quorum size that no longer matches the cluster after a resize can all break correctness without any code change. Treating configuration as code means validating it, reviewing it and testing the system under the configurations actually deployed, not only the defaults. Several public postmortems trace hours of downtime to a single configuration push that no test had exercised.
Small clusters are enough
The finding that most catastrophic failures reproduce on three nodes or fewer is encouraging for testers. It means a test harness does not need a production sized fleet to find serious bugs. What it needs is control over the order of events and the ability to inject the right faults at the right moments. A three node cluster under aggressive, well chosen faults explores far more of the dangerous state space than a hundred node cluster running only the happy path.
Distributed bugs hide in rare interleavings of messages, failures and timers, and ordinary tests cannot reproduce them. Effective techniques take control of time, the network and scheduling so rare executions can be explored deliberately and replayed exactly.