Volume 9 · Chapter 7 — Enterprise Application Architecture & System Design
Distributed System Design
7.1 Introduction
Distributed systems are collections of independent components collaborating across unreliable networks, and their defining reality is partial failure: at any moment some part is slow, unreachable, or returning stale data, while the rest keeps running. Designing them well means internalizing a few hard truths - the network is unreliable, there is no global clock, and consistency and availability trade off under partition - and then applying the patterns that keep the system correct and available despite all of it.
7.2 The Fallacies and Core Truths
The classic “fallacies of distributed computing” warn against assuming the network is reliable, latency is zero, bandwidth is infinite, or topology is stable. The practical consequences that shape every design: messages can be lost, duplicated, or reordered; calls can time out without you knowing whether they succeeded; clocks drift so you cannot rely on timestamps for ordering; and any node can fail independently. This is why idempotency, timeouts, retries, and observability are the substance of distributed design, not optional polish.
7.3 The CAP Theorem
CAP states that when a network partition occurs, a distributed data store can guarantee at most one of Consistency or Availability - you must choose which to sacrifice while partitioned. It is a per-data-set decision: a payments ledger chooses consistency (reject rather than double-spend), while a product catalog chooses availability (serve slightly stale data rather than error). PACELC extends this: even without a partition (Else), you trade Latency against Consistency, which is why replicas and caches (eventual consistency) are accepted for reads that tolerate staleness.
flowchart TB
P{"Network partition?"} -- "Yes" --> CA{"Choose"}
CA --> C["Consistency\n(reject/parts unavailable)\nledger, inventory"]
CA --> A["Availability\n(serve possibly stale)\ncatalog, feed"]
P -- "No (PACELC: Else)" --> LC{"Trade"}
LC --> L["Lower Latency\n(replicas, cache)"]
LC --> SC["Stronger Consistency\n(primary reads)"] 7.4 Consistency Models
Consistency is a spectrum, not a binary. Strong (linearizable) consistency makes the system behave as if there were one copy - simplest to reason about, costliest to provide. Eventual consistency guarantees replicas converge given no new writes - cheap and available but requires the application to tolerate temporary disagreement. Between them, causal and session consistency (read-your-writes) offer useful middle grounds. The architect picks the weakest model the business can tolerate, because weaker models scale better.
7.5 Fault Tolerance Patterns
Because components fail independently, the system must contain and recover from failure: timeouts bound every call, retries with backoff handle transient faults, circuit breakers stop hammering a dead dependency, bulkheads isolate resources so one failure cannot exhaust everything, and redundancy plus failover keep the system serving when instances die. These combine into graceful degradation - shedding non-essential features while the core keeps working.
7.6 Distributed Transactions and the Saga
You cannot hold an ACID transaction across services. The saga pattern replaces it with a sequence of local transactions, each emitting an event that triggers the next, and compensating actions that semantically undo prior steps if a later one fails. Combined with the outbox (atomic state-plus-event) and idempotent handlers, sagas provide reliable multi-service workflows under eventual consistency.
sequenceDiagram autonumber participant O as Orchestrator participant Pay as Payment participant Inv as Inventory participant Ship as Shipping O->>Pay: reserve funds Pay-->>O: reserved O->>Inv: reserve stock Inv-->>O: reserved O->>Ship: schedule Ship--xO: FAILED O->>Inv: compensate (release stock) O->>Pay: compensate (refund)
7.7 Load Balancing and Distributed Caching
Horizontal scale relies on load balancers distributing traffic across healthy, stateless instances (with health checks removing bad ones). Distributed caches (Redis) cut latency and backend load by serving hot data from memory shared across instances - but introduce their own consistency and invalidation concerns, and must be protected against stampedes.
Explain the CAP theorem and give a concrete design choice it forces.
CAP says that during a network partition, a distributed data store can provide either consistency or availability, not both - while partitioned you must choose. Concretely, for a bank’s ledger I choose consistency: if replicas cannot agree, I would rather reject or delay a transaction than risk a double-spend, so the system becomes partially unavailable under partition but never returns wrong balances. For a product catalog or a “likes” counter I choose availability: serving slightly stale data during a partition is far better than an outage, so those tolerate temporary inconsistency. The decision is made per data set, which is why one platform holds a strongly consistent ledger and an eventually consistent catalog side by side. PACELC adds that even without a partition I trade latency for consistency - which is exactly why I serve reads from replicas or caches for data that tolerates a little staleness.
What are the biggest challenges in distributed systems?
The fundamental challenge is partial failure combined with an unreliable network: at any time some components are slow, unreachable, or returning stale data, and a call can time out without telling you whether it succeeded. From that flow the specific hard problems - maintaining data consistency without cross-service transactions (hence sagas and eventual consistency), coordinating multi-step processes across services, coping with messages that duplicate or reorder (hence idempotency), the absence of a reliable global clock for ordering, latency and its effect on user experience, and the sheer operational complexity of understanding a system whose behavior emerges from many interacting parts. That last point is why observability - distributed tracing, correlation, centralized metrics and logs - is not optional: without it you cannot diagnose a system you cannot see. Good distributed design accepts failure as the normal case and engineers idempotency, timeouts, resilience patterns, and observability as first-class concerns.
What is eventual consistency and when is it acceptable?
Eventual consistency guarantees that, in the absence of new updates, all replicas will converge to the same value - but for some window different parts of the system may disagree. It is the price of high availability and horizontal scale, and it is acceptable whenever the business can tolerate a brief period of staleness without harm: a product view count, a social feed, a cached profile, a read-model dashboard, or cross-service reactions that catch up shortly after an event. It is not acceptable where a stale or conflicting read causes real damage - a bank balance used to authorize a withdrawal, an inventory count that could oversell, a compliance record. The architect’s job is to decide per data set whether the business can absorb the inconsistency window, use eventual consistency where it can for scale and availability, and reserve strong consistency for the places where correctness of the latest value is non-negotiable - then design the UX to handle the eventual-consistency gap gracefully.
How do you design a distributed system to be reliable under constant partial failure?
I design assuming failure is the normal case and engineer containment and recovery throughout. Every network call has a timeout so nothing waits forever; transient faults get bounded retries with backoff and jitter; a circuit breaker trips when a dependency is genuinely down so we fail fast instead of cascading; and bulkheads isolate resources so one sick dependency cannot exhaust the threads or connections the whole system shares. I make operations idempotent so retries and at-least-once delivery are safe, and I replace cross-service transactions with sagas and compensations under eventual consistency, using the outbox for reliable eventing. I choose consistency versus availability per data set based on business harm, keeping the money-critical paths strongly consistent and letting the rest be eventually consistent for scale. Redundancy across zones with health-checked load balancing and automated failover keeps the system serving when instances die, and graceful degradation lets non-essential features shed under stress while the core survives. Finally I make it all observable with tracing and correlation so failures are visible, and I validate the whole thing with fault-injection / chaos testing so the first time a breaker opens for real is not the first time it has ever opened. Reliability is the sum of these deliberate choices, not a single feature.
How do you reason about consistency requirements across a large platform with many data sets?
I refuse to pick one global consistency model and instead decide per data set based on the cost of a wrong or stale answer, because that cost varies enormously across a platform. For each significant data set I ask: if a read is stale or a write is briefly lost or conflicting, what is the business consequence? Where the answer is financial loss, regulatory breach, or safety - a ledger balance, an inventory decrement that could oversell, a compliance record - I require strong consistency and accept the latency and availability cost, keeping those operations within a single aggregate or strongly consistent store. Where the answer is merely a brief cosmetic discrepancy that self-corrects - a view count, a recommendation, a denormalized read model, a cached profile - I choose eventual consistency for the availability, latency, and scale it buys. This is why the same platform holds a strongly consistent transactional core and eventually consistent read models and caches side by side, connected by events. I make these choices explicit (often in ADRs), design the user experience to handle the eventual-consistency windows where they exist, and keep the strongly consistent surface as small as it can be, because strong consistency is the expensive, scale-limiting resource I spend only where correctness of the latest value truly demands it.
7.8 Banking Enterprise Scenario
A bank’s distributed platform makes consistency a per-data-set decision: the ledger is strongly consistent (CAP-consistent under partition, because a wrong balance is unacceptable), while transaction-history views and dashboards are eventually consistent read models fed by events. Every cross-service call is wrapped in timeouts, retries, circuit breakers, and bulkheads, and multi-service workflows like transfers run as orchestrated sagas with compensation because no ACID transaction spans services. Services run redundantly across zones behind health-checked load balancers with automated failover, hot data is served from a Redis cache with stampede protection, and the whole system is traced end to end. Chaos testing regularly injects dependency failures to prove the resilience patterns actually engage.
Chapter Revision Notes
- Partial failure is the defining reality - design assuming the network is unreliable.
- CAP: choose consistency or availability per data set under partition; PACELC adds latency vs consistency.
- Pick the weakest consistency model the business tolerates - weaker scales better.
- Contain failure with timeouts, retries, circuit breakers, bulkheads, and redundancy.
- Replace distributed transactions with sagas, outbox, and idempotency.
- Observability and chaos testing make reliability real, not assumed.