Distributed SystemsVol. 5 · Part V
0 / 0

Shelves / Distributed Systems

DS·00

Start Here: What a Distributed System Is

For readers who have only written programs that run on one machine: what changes when a system spans several, the failures that can't happen on one computer, a measured demonstration of an ambiguous timeout turning into a duplicate order, the vocabulary of the volume, and how to study it.

8 topics · ~6 min read · 10-question quiz
Updated Suggest a correction

Start here: how to use this volume

Two reading paths

This volume is about systems whose parts run on different machines and talk over a network: messaging and Kafka, delivery guarantees, consistency models, CAP, replication, consensus, distributed transactions, idempotency, time and ordering, traffic control, coordination, and how all of it fails. Every chapter has the same shape: a plain-language Start here section with small, verified experiments; a How deep to go ladder; the full-depth sections; a Lab of real measurements; and a Revision sheet.

  • New to distributed systems? Read this chapter, then the chapters in order. Run the experiments (they use small, free tools: etcd, NATS, Toxiproxy and Python or Go), answer Check yourself before opening the answers, and take each quiz.
  • Experienced? Skip this chapter. In each chapter read the revision sheet first, take its quiz, then use How deep to go to choose the internals and lab experiments to study.

Mastery levels

Every chapter uses the same six levels:

Level Name You can… Example for retries
L1 Recognition define it and spot it "that's an at-least-once retry"
L2 Working use it correctly add retries with backoff and an idempotency key
L3 Internals explain why it works explain why a timeout can't tell you whether the request ran
L4 Debugging diagnose failures trace duplicate charges to a retry after an ambiguous timeout
L5 Design choose between approaches decide between exactly-once processing and idempotent at-least-once
L6 Production run it at scale set retry budgets and deadlines across hundreds of services

What a distributed system is

More than one computer, cooperating

A distributed system is a set of programs on different machines that coordinate by sending messages over a network to provide one service. Almost every system you use is one: a web app with its database and cache; a payment flow across your service, a gateway and a bank; Kafka with its brokers; a mobile app syncing with a server. You distribute for good reasons: scale (more load than one machine handles), availability (keep working when a machine dies), latency (be near users) and organisation (separate teams own separate services). And every one of those brings problems a single program never has.

The failure you can't see

On one computer, a function call either returns or throws. Over a network there's a third outcome: nothing comes back, and you can't tell why. The demonstration below runs a tiny order service behind Toxiproxy, a proxy that can inject network faults on demand:

Python
def place(dish, timeout=1.0):
    r = urllib.request.urlopen(Request("http://127.0.0.1:9101/orders", data=…, method="POST"), timeout=timeout)
Text
1) normal network:                201 {"order": 1} in 43 ms | orders stored: 1
2) slow replies, 100 ms timeout:  TimeoutError: timed out after 101 ms | orders stored: 2
   ...the client gave up, but the server had already stored it. Orders stored: 2 ['Idli', 'Vada']
3) the client retries:            201 {"order": 3} in 323 ms | orders stored: 3 ['Idli', 'Vada', 'Vada']
4) reply path cut:                RemoteDisconnected: Remote end closed connection without response after 22 ms | orders stored: 4

Case 2 is the heart of the whole volume. Replies were delayed by 300 ms, the client's 100 ms timeout fired, and the client reported an error, yet the server had stored the order. From the client's side, a timeout is consistent with three different stories: the request never arrived, it arrived and failed, or it succeeded and only the reply was slow or lost. In case 3 the client did the natural thing and retried, and the customer now has two Vadas. In case 4 the reply path was cut entirely, and again the order was stored though the client saw an error.

ClientOrder service ✕ request lost crash, not stored order stored reply lost ✕ timeout timeout timeout
Why a timeout is ambiguous. The client sees the same thing, no reply, in three different situations, so it cannot know whether the server performed the request.

What's different from one machine

On one machine Across machines
A call returns or throws A call can also vanish: no reply, unknown outcome
Failure is total (the program crashes) Failure is partial: some parts work while others don't
Memory is shared and instant State is copied over a network that's slow, lossy and can split in two (partition)
One clock Many clocks that disagree; "which happened first?" is hard
A lock or a transaction coordinates everything Agreement needs messages, quorums and consensus protocols

Nearly every technique in this volume answers one of these rows:

  • Retries, idempotency and delivery semantics (chapters 68, 74): making repeated messages harmless, so the Vada problem can't happen.
  • Messaging and Kafka (66, 67): decoupling services so one can be down without the others failing.
  • Replication, consistency, CAP and PACELC (69–71): keeping copies of data and deciding what each copy may answer while they disagree.
  • Consensus and coordination (72, 77): getting machines to agree on one value or one leader despite failures.
  • Distributed transactions and sagas (73): changes spanning services without a shared database.
  • Time and ordering (75): ordering events without trusting clocks.
  • Traffic control and failure modes (76, 78): staying up when load and failures spread.

How to study distributed systems

A routine that works

  1. Read one topic of a Start here section.
  2. Run the experiment, then change one thing: a timeout, a replica count, a fault.
  3. Inject failures: kill a process, add latency with Toxiproxy, partition a node. Distributed systems are understood through how they fail.
  4. Draw the timeline: for any surprising result, sketch which messages went where and when, as in the figure above.
  5. Answer Check yourself without looking, take the quiz, and revisit a few days later.

Warm-up exercises

  1. Run the demonstration above (any HTTP server plus Toxiproxy) and reproduce the duplicate order.
  2. Make the duplicate impossible: the client sends an Idempotency-Key header, and the server remembers keys it has processed (Volume 3 chapter 45).
  3. Add 5% packet loss with Toxiproxy's limit_data or slicer toxics and send 1,000 requests with retries. How many duplicates without keys, and with them?
  4. Start three etcd members on one machine (etcd --name a --initial-cluster …), write a key, kill the leader, and time how long writes fail.
  5. Explain to someone else, without the figure, why a timeout can't tell you whether a payment happened.

Glossary

  • Node: one machine or process in the system.
  • Partial failure: some components fail while others keep running.
  • Network partition: nodes that are up but can't reach each other.
  • Timeout: giving up waiting; it reveals nothing about what happened.
  • Idempotent: safe to repeat; repeating has the same effect as doing it once.
  • Replication: keeping copies of data on several nodes.
  • Consensus: getting nodes to agree on one value despite failures.
  • Quorum: a majority (or other overlapping subset) of nodes whose agreement is required.