System Design and Scalable Web Architecture

Chapter 5
Distributed Systems Principles

Post Graduate Diploma in Full Stack Development
System Design and Scalable Web Architecture   •   Post Graduate

5. Distributed Systems Principles

Introduction and Context

A distributed system is a collection of independent computers that cooperate over a network and appear to users or applications as a coherent system. Modern web applications are distributed almost by necessity: traffic is served from multiple instances, data is replicated across regions, background work is delegated to queues, and services communicate through APIs. Distribution improves scalability, availability, and geographic reach, but it also introduces uncertainty. Messages can be delayed or lost, machines can fail independently, clocks can disagree, and two components may temporarily hold different views of reality.

In a single-process application, an operation can often be understood as a sequence of instructions executed by one clock and one memory space. In a distributed application, there is no universally visible execution order and no guaranteed instant at which all participants observe the same state. Consequently, system design requires explicit reasoning about time, ordering, failure, consistency, coordination, and recovery.

This chapter develops the principles needed to design and evaluate such systems. It connects theoretical models—including CAP, PACELC, consensus, and consistency models—with practical mechanisms such as leader election, replication, quorums, distributed transactions, sagas, and conflict resolution. The emphasis is not on selecting a fashionable technology, but on matching coordination mechanisms to business requirements and failure assumptions.

Learning Objectives

After studying this chapter, students should be able to:

1. Explain why distribution creates uncertainty and partial failure.

2. Distinguish physical, logical, and hybrid logical clocks.

3. Analyze event ordering and causal relationships.

4. Explain consensus, leader election, replication, and quorum techniques.

5. Apply CAP and PACELC to architectural decisions.

6. Compare strong, eventual, causal, session, and monotonic consistency.

7. Select suitable approaches for distributed transactions and sagas.

8. Evaluate conflict-resolution strategies and failure modes.

9. Design reliable distributed workflows and identify their operational risks.

The Distributed-System Problem: Cooperation Without Shared Reality

The central difficulty of a distributed system is that participants communicate by messages rather than by shared memory. A message may arrive late, arrive twice, arrive out of order, or never arrive. A process may be alive but unable to communicate. A network partition may make a healthy server appear unavailable to another server. These conditions are examples of partial failure: part of the system fails while other parts continue operating.

A useful model separates three kinds of uncertainty. First, communication uncertainty concerns whether and when a message will arrive. Second, process uncertainty concerns whether a node is slow, crashed, overloaded, or merely unreachable. Third, state uncertainty concerns which version of data is current. Timeouts are therefore not proof of failure; they are decisions made under incomplete information.

Distributed design begins by stating assumptions. Is the network asynchronous, with no reliable upper bound on delay? Are machines fail-stop, or can a compromised node send misleading messages? Can data be lost after acknowledgement? Are duplicate requests possible? The answers determine which guarantees are feasible and which protocols are appropriate.

Time, Clocks, and the Ordering of Events

Physical clocks and their limits

Physical clocks measure calendar time, but clocks on different machines drift because of differences in hardware, temperature, and synchronization. Network Time Protocol can reduce disagreement, but it cannot guarantee perfect agreement. A timestamp such as 10:00:00.100 on one host may not precede 10:00:00.090 on another in any meaningful causal sense.

Physical time is useful for expiry, certificates, monitoring, and user-visible dates. It is unsafe as the sole basis for determining whether one distributed event caused another. Clock adjustments can also move time backward or forward, creating problems for duration calculations and ordering.

Logical clocks

A logical clock represents ordering rather than wall-clock time. In Lamport’s logical-clock algorithm, each process increments its counter before an event. When sending a message, it includes the counter; when receiving a message with timestamp t, the receiver sets its clock to at least t+1. If event a causally precedes event b, then L(a) < L(b). The reverse is not guaranteed: a smaller Lamport timestamp does not prove causality.

Vector clocks record a vector of counters, one for each relevant process. They can identify whether two events are causally related or concurrent. If every component of vector V is less than or equal to the corresponding component of W, and at least one is smaller, V happened before W. If neither vector dominates the other, the events are concurrent. Vector clocks provide richer information but require metadata that grows with the number of participants.

Hybrid logical clocks

Hybrid logical clocks combine physical-clock readings with logical counters. They preserve useful relationships with wall time while compensating for clock skew and concurrent events. They are valuable in globally distributed databases where timestamps must support ordering, debugging, and bounded-time operations without treating imperfect physical time as absolute truth.

Ordering and causality

A total order places every event before or after every other event. A partial order places events in relation only when their causal relationship is known. Distributed systems often need a deterministic total order for replication, achieved by combining a logical timestamp with a unique node identifier. Such tie-breaking creates an order, but it does not turn concurrent events into causally related events. This distinction matters when resolving updates, auditing actions, or interpreting user behavior.

Replication, Quorums, and the Distribution of Authority

Replication stores copies of data on multiple nodes. It can improve read performance, fault tolerance, and geographical latency, but replicas introduce the possibility of divergence. A replication design must specify how writes are accepted, how changes are propagated, when an acknowledgement is issued, and how stale replicas recover.

In leader-based replication, one leader accepts writes and followers replicate the resulting log or state. This simplifies ordering and conflict management, but leader failure requires failover. In leaderless replication, clients or coordinators communicate with several replicas, often using quorum rules. Multi-leader replication permits writes in several regions and reduces write latency, but concurrent updates require conflict handling.

If a system has N replicas, a write quorum W, and a read quorum R, then R + W > N ensures that a read and a write overlap in at least one replica, assuming the protocol correctly selects and compares versions. W + W > N can prevent two successful writes from being disjoint, although it does not by itself solve semantic conflicts. Quorums are not simply percentages; they are protocol commitments involving membership, failure detection, version comparison, and repair.

Read repair updates stale replicas when a read discovers disagreement. Anti-entropy processes compare replicas in the background, commonly using hashes or Merkle trees, and repair differences. Hinted handoff temporarily stores a write on behalf of an unavailable replica. These mechanisms improve convergence but do not automatically provide application-level correctness.

Replication also raises the question of acknowledgement durability. An acknowledgement may mean that data reached memory, a local disk, a majority of nodes, or a remote region. Professional designs state this guarantee explicitly because a successful response that survives only in volatile memory may be lost after a power failure.

Consensus and Leader Election

Consensus is the problem of having distributed participants agree on a value or an ordered sequence of values despite failures. A consensus protocol typically aims for safety—nothing contradictory is decided—and liveness—decisions eventually occur under stated conditions. These goals depend on the failure model and network assumptions.

Consensus is stronger than replication. Replication copies data, whereas consensus establishes a common decision or log order. Protocols such as Raft and Paxos use a replicated log and quorum-based voting. Raft is often taught through the roles of follower, candidate, and leader. A candidate requests votes after an election timeout; a leader sends heartbeats and proposes log entries; followers accept entries and acknowledge them. An entry becomes committed when the protocol’s quorum and log rules establish that it is safely replicated.

Leader election must prevent split brain, in which two nodes both believe they are authoritative. Terms, epochs, fencing tokens, and quorum votes help ensure that an old leader cannot continue making valid changes after a new leader is elected. A lease can limit leadership to a time interval, but leases depend on clock and network assumptions. Fencing is safer for resources such as storage: every operation carries a monotonically increasing token, and the resource rejects operations from older tokens.

Consensus is expensive because it requires coordination and usually sacrifices availability during certain partitions. It should therefore be used for decisions that genuinely require a single authoritative order, such as membership, configuration, locks, or a transactional log—not indiscriminately for every read.

CAP and PACELC as Decision Frameworks

The CAP theorem states that, in the presence of a network partition, a system cannot simultaneously guarantee both consistency and availability in the strongest interpretations of those terms. Consistency here means that operations appear to follow a single up-to-date history; availability means that every request to a non-failing node receives a non-error response. Partition tolerance is generally unavoidable in a networked system, so the practical choice during partition is whether to reject or delay some operations to preserve consistency, or to continue responding while accepting divergent or stale results.

CAP is not a permanent label saying that a database is always “CP” or “AP.” A system may offer different behavior for different operations, configurations, and failure periods. Furthermore, consistency has multiple meanings, and availability may be measured by latency objectives rather than a simple binary condition.

PACELC extends the discussion: if there is a Partition, choose Availability or Consistency; Else, during normal operation, choose Latency or Consistency. A system may provide stronger consistency across regions at the cost of additional round trips, or choose lower latency with weaker read guarantees. PACELC is especially useful for geo-distributed architecture because it exposes the normal-case cost of coordination, which CAP alone tends to hide.

A design decision should begin with business semantics. A payment authorization, inventory reservation, and account balance generally need stronger coordination than a social-media counter, recommendation feed, or presence indicator. “Eventually consistent” is not a complete requirement; engineers must specify acceptable staleness, ordering, duplicate effects, and behavior during partition.

Consistency Models and Their Practical Meaning

Linearizability makes each operation appear to take effect atomically at one point between invocation and response, respecting real-time order. It is intuitive but may require coordination and can reduce availability across regions.

Sequential consistency requires that operations appear in one order consistent with each process’s program order, but that order need not match real-time order. Causal consistency preserves the order of causally related operations while allowing concurrent operations to be observed differently. Eventual consistency guarantees that replicas converge if no new updates occur, but says little about the temporary states observed by clients.

Session guarantees refine weak consistency for individual users. Read-your-writes ensures that a user sees their own successful updates. Monotonic reads prevent a user from moving backward to an older version. Monotonic writes preserve the order of that user’s writes, while writes-follow-reads ensures that a later write incorporates what the user has observed. These guarantees can often be achieved through session routing, version tokens, or dependency metadata without imposing global linearizability.

Consistency models should be selected per data type and operation. A product catalogue may tolerate brief staleness; stock reservation may not. A user profile can use read-your-writes, while analytics dashboards may use bounded staleness. This data-oriented approach is more precise than declaring an entire application strong or weakly consistent.

Distributed Transactions and Sagas

A transaction spanning multiple services cannot assume a single local database transaction. Two-phase commit (2PC) divides execution into prepare and commit phases. A coordinator asks participants to prepare; once all agree, it instructs them to commit. 2PC can provide atomicity, but the coordinator or network can leave participants blocked while they await a decision. It also introduces coordination latency and operational complexity.

The saga pattern decomposes a business transaction into a sequence of local transactions. Each step commits independently and emits an event or command for the next step. If a later step fails, compensating actions attempt to undo the business effect of earlier steps. A saga may be choreography-based, where services react to events, or orchestration-based, where a coordinator directs the workflow.

Compensation is not the same as rollback. A payment refund, cancellation email, or restocking operation may have different timing and side effects from the original action. Sagas therefore require explicit states, durable workflow history, idempotent commands, retry policies, deadlines, and a method for handling permanently failed compensation. The transactional outbox pattern helps ensure that a local database update and the publication of its corresponding event are not separated by a crash: the event is written to an outbox in the same local transaction and delivered later by a relay.

Exactly-once processing is rarely an end-to-end property. Systems usually achieve effectively-once business outcomes through at-least-once delivery combined with idempotency keys, deduplication records, conditional updates, and deterministic state transitions.

Conflict Resolution and Failure Modes

Conflicts arise when concurrent writers update the same logical object. Last-write-wins is simple but can discard a valid update and is sensitive to clock quality. Version numbers can reject stale writes through compare-and-set. Field-level merging preserves independent changes but may produce invalid combinations. CRDTs provide data structures whose operations converge under defined mathematical rules; they are useful for sets, counters, and collaborative data, but not every business invariant can be expressed safely as a CRDT.

Application-specific resolution is often superior. For example, a reservation system may reject competing claims rather than merge them. A document editor may retain both revisions for user review. Conflict policies must define whether data is discarded, combined, quarantined, or presented to an operator. They should also be observable, because silent conflict resolution can conceal business loss.

Common failure modes include duplicate message delivery, out-of-order events, stale reads, lost acknowledgements, retry storms, cascading failure, split brain, data corruption, and permanent divergence. Defensive measures include bounded retries with exponential backoff and jitter, circuit breakers, bulkheads, rate limits, dead-letter queues, idempotency, health checks, fencing, snapshots, backups, and tested restoration procedures. A timeout should lead to a safe uncertainty state—not an assumption that the operation definitely failed.

Applied Case: A Multi-Region Event Ticketing Platform

Background

Consider an illustrative platform that sells tickets for concerts across three regions. It maintains a catalogue, accepts payments, reserves seats, sends notifications, and exposes a mobile application. The business wants low latency for international users and continued operation if one region becomes unreachable.

Situation and problem

During a popular event, thousands of users attempt to reserve the same seats. A design that lets each regional replica accept writes independently may create duplicate reservations. Conversely, routing every request to one distant leader may produce unacceptable latency and make the service unavailable during a regional partition.

Analysis

The catalogue and event descriptions can tolerate eventual consistency. User profile changes need read-your-writes. Seat ownership is a scarce business invariant: two successful reservations for one seat are unacceptable. Payment status and reservation status must also be correlated, but payment providers may respond slowly or repeat callbacks.

Approach

The platform assigns each event, or a carefully chosen seat partition, to an authoritative reservation leader. Writes use a consensus-backed log or a quorum protocol, with a short reservation lease and a fencing token to prevent an old leader from committing after failover. Reads of available seats may be served from replicas, but the final reservation is validated by the authority.

A reservation saga coordinates seat allocation, payment authorization, order creation, and notification. The seat service first creates a pending reservation with an expiry time. Payment authorization is idempotent and carries the reservation identifier. If payment succeeds, the order is confirmed; if it fails or expires, the seat is released by a compensating action. Local database changes and outgoing events use an outbox. Repeated payment callbacks are deduplicated, and all state transitions are recorded in an audit log.

If a region becomes partitioned, the platform may continue serving catalogue reads but refuses new reservations for events whose authority cannot be reached. This is a deliberate CP choice for scarce inventory, while less critical features remain available. Monitoring tracks quorum health, stale-read age, saga duration, compensation rate, duplicate callbacks, and reconciliation failures.

Outcome

The design does not maximize availability for every operation. Instead, it preserves the business invariant that a seat cannot be sold twice, while allowing low-risk functions to remain responsive. Users receive explicit pending or unavailable states rather than misleading success messages.

Learning points

The case demonstrates that CAP decisions should be made per operation, not per product label. It also shows that leader election, fencing, idempotency, durable workflow state, and observability are complementary controls. No single replication setting solves the complete business problem.

Professional and Governance Considerations

Distributed systems affect financial records, personal information, audit trails, and legally significant decisions. Architects should define data ownership, retention, recovery objectives, access controls, and regional data-residency requirements. Replication can unintentionally copy sensitive data across jurisdictions. Logs and tracing must avoid exposing secrets while remaining sufficient for incident investigation.

Operational governance should include documented consistency guarantees, failure-handling responsibilities, change procedures, disaster-recovery exercises, and service-level objectives. Teams should test partitions, delayed messages, clock skew, duplicate delivery, replica loss, and restore procedures through controlled experiments. Architecture reviews should ask not only whether the system works when healthy, but also what users observe when components disagree.

Emerging Directions

Modern platforms increasingly combine consensus-based metadata systems with scalable, eventually consistent data stores. Globally distributed SQL databases provide stronger transactional guarantees across regions, but their latency and operational cost must be evaluated against workload needs. Conflict-free data types, event sourcing, workflow engines, and streaming platforms support new forms of collaboration and asynchronous processing.

Observability is also becoming more causal: trace context, event identifiers, logical timestamps, and workflow correlation make it possible to reconstruct distributed behavior. Automated remediation and machine-learning-based anomaly detection can identify unusual lag or retry patterns, but automation must be bounded by safety policies. It should not promote leaders, delete data, or compensate transactions without clear authority and auditability.

Best-Practice Design Heuristics

1. State the failure model and consistency requirement before selecting technology.

2. Separate user-visible business invariants from implementation details.

3. Treat timeouts as uncertainty, not proof of failure.

4. Make commands and event handlers idempotent.

5. Use fencing when old leaders could still access a resource.

6. Record durable state transitions and correlate every distributed operation.

7. Prefer local transactions plus explicit workflows over hidden cross-service assumptions.

8. Define staleness, ordering, and recovery guarantees in measurable terms.

9. Test partitions, duplication, reordering, clock skew, and restoration.

10. Make conflict policies explicit, observable, and appropriate to the business domain.

Chapter Summary

Distributed systems coordinate independent components that lack shared memory, a perfectly synchronized clock, or complete knowledge of one another’s state. Partial failure, message delay, and concurrency make ordering and correctness difficult. Physical, logical, vector, and hybrid clocks provide different ways to reason about time and causality. Replication improves resilience but introduces divergence, while quorums establish overlap among reads and writes under defined assumptions.

Consensus creates safe agreement and ordered logs, commonly supporting leader election and failover. CAP explains the unavoidable consistency–availability choice during partition; PACELC adds the normal-case latency–consistency trade-off. Consistency models range from linearizability to eventual consistency, with session and causal guarantees offering useful intermediate choices. Distributed transactions may use 2PC or, more flexibly, sagas with compensation, outboxes, idempotency, and durable workflow state. Conflict resolution must reflect business semantics, and failure handling requires explicit safeguards, observability, governance, and testing.

Key Terms

Distributed system: A set of independent networked components that cooperate as one logical system.

Partial failure: Failure of some components while others continue operating.

Physical clock: A clock intended to represent real-world elapsed or calendar time.

Logical clock: A counter used to represent event ordering rather than wall-clock time.

Vector clock: A set of counters used to represent causal relationships among processes.

Causality: A relationship in which one event can influence or precede another.

Replication: Maintaining multiple copies of data or state.

Quorum: A sufficient subset of participants required to accept or validate an operation.

Leader election: A protocol for selecting one authoritative coordinator among nodes.

Consensus: Agreement among distributed participants on a value or ordered sequence.

Split brain: A condition in which multiple nodes incorrectly act as authoritative leaders.

Linearizability: A consistency guarantee in which operations appear atomic and respect real-time order.

Eventual consistency: A guarantee that replicas converge if updates stop and communication continues.

Causal consistency: A model preserving the order of causally related operations.

CAP theorem: The principle that partitioned systems cannot simultaneously guarantee strong consistency and availability.

PACELC: A framework expressing partition-time availability/consistency and normal-time latency/consistency trade-offs.

Saga: A sequence of local transactions linked by events or commands and compensated when necessary.

Idempotency: The property that repeating an operation produces the same intended business result.

Fencing token: A monotonically ordered token that prevents obsolete leaders from acting.

Conflict resolution: A rule or mechanism for reconciling concurrent or divergent updates.

Review Questions

Short Answer Questions

1. Why is a timeout not definitive proof that a remote process has failed?

2. Distinguish a Lamport clock from a vector clock.

3. What condition involving N, R, and W creates overlap between a read quorum and a write quorum?

4. Why is consensus stronger than ordinary replication?

5. What is the purpose of an idempotency key in a distributed workflow?

Long Answer Questions

1. Explain the role of leader election, terms, epochs, and fencing in preventing split brain.

2. Compare linearizability, causal consistency, session guarantees, and eventual consistency, using suitable web-application examples.

3. Explain CAP and PACELC and discuss why they should be applied to operations or data domains rather than simplistic database labels.

4. Describe two-phase commit and sagas, including their operational strengths, limitations, and failure-recovery requirements.

5. Discuss replication, anti-entropy, read repair, hinted handoff, and acknowledgement durability as parts of a complete replication strategy.

Analytical / Application Questions

1. A social application must display a user’s newly uploaded photograph immediately to that user, while other users may see it several seconds later. Which consistency guarantees and mechanisms would you choose, and why?

2. An inventory service has three replicas and must prevent two customers from purchasing the final item. Analyze whether a quorum-only design is sufficient and propose additional safeguards.

3. A payment callback may be delivered three times, while the order service can be temporarily unavailable. Design an idempotent saga workflow and identify the states, retries, compensation, and observability it requires.

22. Multiple Choice Questions with Answers

Question 1

Which statement best describes a limitation of physical clocks in distributed systems?

A. They cannot measure elapsed time on a single machine.

B. They guarantee a common total order for events on different machines.

C. Clock drift and synchronization error mean timestamps do not reliably establish causality.

D. They can only be used when a consensus protocol is available.

Correct Answer: C. Clock drift and synchronization error mean timestamps do not reliably establish causality.

Question 2

If event a causally precedes event b, what does a Lamport clock guarantee?

A. L(a) is greater than L(b).

B. L(a) is less than L(b).

C. L(a) and L(b) are always equal.

D. The two events must occur on the same node.

Correct Answer: B. L(a) is less than L(b).

Question 3

A system has N replicas, read quorum R, and write quorum W. Which condition provides overlap between every read quorum and every write quorum?

A. R + W <= N

B. R = W regardless of N

C. R + W > N

D. R x W = N

Correct Answer: C. R + W > N

Question 4

What is the primary purpose of a fencing token after leader failover?

A. To compress replicated logs.

B. To allow all former leaders to continue serving requests.

C. To prevent an obsolete leader from modifying a protected resource.

D. To synchronize physical clocks exactly.

Correct Answer: C. To prevent an obsolete leader from modifying a protected resource.

Question 5

During a network partition, a system continues accepting writes in both isolated regions and later reconciles them. Which CAP trade-off does this behavior emphasize?

A. Choosing availability over strong consistency during partition.

B. Choosing strong consistency over availability during partition.

C. Eliminating the need for partition tolerance.

D. Guaranteeing linearizability without coordination.

Correct Answer: A. Choosing availability over strong consistency during partition.

Question 6

Which statement correctly distinguishes a saga from rollback in a single database transaction?

A. A saga always undoes effects atomically at the same instant.

B. A saga uses independent local transactions and compensating business actions.

C. A saga eliminates the need for retries and durable state.

D. A saga requires every service to share one database.

Correct Answer: B. A saga uses independent local transactions and compensating business actions.

Question 7

A payment provider sends the same success callback twice. Which design most directly prevents two orders from being created?

A. Use an idempotency key and record processed callback identifiers.

B. Increase the network timeout for callback requests.

C. Use last-write-wins conflict resolution without identifiers.

D. Ignore all callbacks received after the first minute.

Correct Answer: A. Use an idempotency key and record processed callback identifiers.

Question 8

What does PACELC add to the CAP discussion?

A. It removes the need to consider network partitions.

B. It analyzes latency versus consistency during normal operation as well as partition behavior.

C. It states that every system must use eventual consistency.

D. It defines a universal algorithm for leader election.

Correct Answer: B. It analyzes latency versus consistency during normal operation as well as partition behavior.

Question 9

Which consistency guarantee ensures that a user does not observe an older version after observing a newer version?

A. Monotonic reads

B. Write skew

C. Last-write-wins

D. Eventual delivery

Correct Answer: A. Monotonic reads

Question 10

In the ticketing case, why might catalogue reads remain available while new seat reservations are rejected during a partition?

A. Catalogue data and seat ownership have identical consistency requirements.

B. The design applies stronger coordination to the scarce inventory invariant while allowing stale low-risk reads.

C. Rejecting reservations makes the system fully AP.

D. Consensus is unnecessary for seat ownership.

Correct Answer: B. The design applies stronger coordination to the scarce inventory invariant while allowing stale low-risk reads.

System Design and Scalable Web Architecture
Chapter 5 of 15