Distributed Systems
Reasoning About Distributed System Architectures
Distributed System Architectures place cooperating software and data on multiple networked computers. You encounter them when an application must serve more work, operate across locations or continue through some failures. A distributed system architecture specifies how these computers divide responsibilities, communicate and coordinate shared state. Moving a function to another machine changes more than its location: calls take time, messages may be delayed, and different components may observe different information.
Communication becomes part of the design
A local function call and a remote request have different costs. A remote request includes network travel, processing and possibly waiting behind other work. Its latency can vary even when the application code stays unchanged.
For independent remote operations with fixed durations, compare sequential execution with execution in parallel:
Here, T0 is common processing that occurs before the operations, and each Ti includes the complete remote operation, including its network round trip. The parallel formula assumes every operation starts together, all results are required, and no resource contention or extra coordination adds delay.
Parallel execution helps only when the operations can proceed independently. If one request needs another request's result, that dependency remains on the response path. Adding machines does not remove it.
Decide where state lives
Data placement affects both capacity and correctness. Replication can provide alternative places to read data or recover service. Sharding spreads data and associated work across machines. A design can combine both: divide the dataset, then maintain several copies of each subset.
Choice | Potential benefit | Obligation or cost |
|---|---|---|
Replicate the dataset | Alternative copies can serve requests | Store extra copies and coordinate updates |
Shard the dataset | Spread storage and work | Route requests to the correct subset |
Replicate each shard | Combine distribution with redundancy | Manage both placement and copy agreement |
Synchronous replication makes an acknowledgement wait for the required replica updates. Asynchronous replication permits acknowledgement before those updates finish. The latter can shorten the response path, but another copy may temporarily return an older value. A failed primary may also leave acknowledged updates unavailable on a surviving copy if they had not reached it.
Replication alone does not specify which copy may accept writes, when reads are current or how conflicting updates are resolved. Those rules belong in the architecture.
Design for incomplete knowledge
A partial failure occurs when some components or communication paths fail while other parts keep running. A network partition can leave two healthy machines unable to coordinate.
A timeout tells you that a response did not arrive within a deadline. It does not establish whether the remote operation completed. Retrying a reservation could therefore reserve twice unless duplicate requests are handled safely. An idempotent operation avoids an additional state change when that request is repeated. One implementation stores a request identifier and its result atomically with the state change.
During a partition, preserving a shared rule may require refusing some operations. For example, disconnected copies cannot safely grant the same last item independently unless exclusive reservation rights were already divided. Evaluate availability for the particular operation and failure scenario, rather than assuming that extra machines guarantee it.
What to remember
- Count communication and dependencies on the response path.
- Distinguish distributing data from copying data.
- State when an acknowledgement is allowed and who may change shared state.
- Treat timeouts as uncertain outcomes and make retries safe.
Example: Planning a Checkout Response
Example: An online shop's checkout gateway takes 6 ms to prepare a request. It then needs both a stock check and a delivery quote from separate machines. Neither operation needs the other's result.
The stock check takes 8 ms of network travel in each direction and 12 ms of remote processing. The delivery quote takes 15 ms of network travel in each direction and 18 ms of remote processing. Assume these durations are fixed, with no queueing, retries, resource contention or additional response-assembly time. Measure elapsed time from the start of gateway preparation until both results have returned to the gateway.
a) Calculate the response time if the gateway completes the stock check before starting the delivery quote.
First calculate each complete remote operation, including both network directions. The stock-check duration is ________:
The delivery-quote duration is ________:
Because the calls are sequential, neither duration overlaps the other. Add both after gateway preparation:
The sequential response time is 82 ms.
b) Calculate the response time if both calls start together after gateway preparation.
The calls can overlap because neither depends on the other's result. The gateway still needs both results, so it waits for the slower branch, whose duration is ________.
The critical path is the longest chain of required dependent work that determines the earliest completion time. Here it consists of gateway preparation followed by the delivery-quote branch.
The parallel response time is 54 ms. The stock check finishes earlier, but that does not allow the gateway to respond before the delivery quote returns.
c) The checkout has a 60 ms response budget. Under the stated assumptions, determine whether each design meets it and give the margin.
Compare each complete response time with the same 60 ms budget. For the sequential design:
It exceeds the budget by 22 ms. For the parallel design:
It finishes 6 ms before the budget expires. The parallel design therefore meets the budget by 6 ms, while the sequential design misses it by 22 ms. These results apply to the fixed timing assumptions; they do not establish a guarantee when network or processing times vary.
A payments service stores each account balance on one leader database and two follower databases. The team is deciding when the leader should tell the client that a write has succeeded.
With replication, the leader acknowledges a write as soon as it has stored the write itself, before any follower has a copy, so a leader crash immediately after the acknowledgement can lose that write.
With replication, the leader waits for the required followers to store the write before acknowledging it, so the write survives a leader crash but its latency includes the slowest required follower.
The time between a write being applied at the leader and the same write being applied at a follower is called .
A booking request requires 5 ms of gateway processing followed by a 7 ms local write at the primary. Only after that write completes does the primary send an update to one replica. Network travel takes 11 ms in each direction, and the replica takes 3 ms to store the update before replying.
The primary waits for the replica acknowledgement before starting delivery of the success reply, which takes another 5 ms. All stages are sequential. Ignore queueing and every other delay. Calculate the elapsed time from the start of gateway processing until success is received, in ms, to the nearest whole millisecond.