Information Technology is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Consistency protocols

Practical study notes on consistency protocols: definitions (linearizability, sequential, causal, eventual, FIFO), protocol families (primary-backup, quorum, gossip/anti-entropy), correctness conditions and trade-offs, with worked examples and exam guidance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Keeping copies of the same data on many machines improves availability and performance but creates the problem of keeping those copies consistent. Engineers use consistency protocols when designing distributed storage, caches, databases, and replicated services (for example: distributed key-value stores, CDN caches, or replicated configuration stores) to choose and implement the right consistency–availability–latency trade-off.

Key ideas

Start from first principles: a distributed system has multiple processes and multiple replicas of data. A history (or execution) is the sequence of operations (reads and writes) invoked by processes on replicated objects.

  • Correctness condition (consistency model): a rule that classifies which histories are allowed. We define common models exactly.

  • Linearizability (atomic consistency): A history is linearizable if there exists a total order of all operations that (1) respects the real-time order (if operation A completes before B starts in real time, then A appears before B in the order) and (2) each operation appears to execute instantaneously at some point between its invocation and response and returns the value determined by preceding writes in that order. Assumptions: a notion of real time that all processes agree on for ordering (or at least that completes-before relations are detectable). Linearizability implies sequential consistency and is compositional (if each object is linearizable, the system is linearizable).

  • Sequential consistency: A history is sequentially consistent if there exists a total order of all operations that (1) respects the program order of each process (each process's operations appear in the total order in the same order the process issued them) and (2) each read returns the most recent write in this total order. Note: Unlike linearizability, sequential consistency does not require the total order to respect real-time between different processes.

  • Causal consistency: Define a happens-before relation (→) using program order and message delivery, and take its transitive closure; additionally, if a read observes the value from a write, then that write → any subsequent operations at that process. A history is causally consistent if for any two operations A and B where A → B, every process must see A before B. Concurrent operations that are not ordered by → may be seen in different orders by different processes. Causal consistency can be implemented using vector clocks to track causal dependencies.

  • FIFO (PRAM) consistency: Also called PRAM. Writes from a single process are seen by others in the order issued from that process, but writes from different processes may be seen in different orders. This is weaker than causal consistency.

  • Eventual consistency: If no new updates are made, eventually all replicas converge to the same value. No bounds on how long convergence takes and no ordering guarantees for concurrent updates. Often used with anti-entropy gossip or background reconciliation. Eventual consistency is the weakest commonly used model for correctness.

  • Convergence and Monotonicity: Monotonic reads mean once a process sees a value, it will never see an older value later; monotonic writes mean writes from a process are applied in order. Also common session guarantees: read-your-writes (a client sees its own prior writes) and writes-follow-reads (a client's writes are ordered after the values it has previously read).

Consistency protocol families (high level):

  • Primary-backup (leader-based): One replica is the leader (primary). All writes go to primary; primary orders writes and propagates to backups. Simplest to reason about; leader failover needed. Provides linearizability if primary orders and synchronously replicates before acknowledging (with assumptions about synchrony and failure model).

  • Quorum-based (voting) protocols: Each operation contacts a subset (quorum) of replicas. Use read quorum size R, write quorum size W, total replicas N. To ensure latest writes are seen, choose R and W so that any read quorum intersects any write quorum.

    Exact quorum condition (derived below): R + W > N ensures read/write intersection; W > N/2 ensures that any two write quorums intersect.

  • Gossip/anti-entropy: Replicas exchange states and merge updates asynchronously (pull/push/push-pull). Works for eventual consistency. Conflict resolution may be by last-writer-wins (timestamp), CRDTs, or application-specific deterministic merge; vector clocks/version vectors track causality but are not a resolution policy.

  • Causal+ (causal consistency with convergent conflict handling): Track causality (vector clocks), and use CRDTs or deterministic merge to ensure convergence while preserving causal order.

Assumptions behind protocols: network may be asynchronous or partially synchronous, nodes may crash or recover, messages may be lost or delayed, clocks may be unsynchronized (so avoid relying on physical time unless you use synchronized clocks with bounds). Always state the failure model (crash-stop, fail-recover, Byzantine) — most basic protocols assume crash failures, not Byzantine.

Diagrams to draw in exams:

  • For linearizability vs sequential consistency: draw timelines for two processes P1 and P2 with writes and reads marked, draw real-time axes for each process; show a history that is sequentially consistent but not linearizable (example: overlapping reads/writes with real-time violation). Label invocations and responses and indicate ordering that satisfies sequential but violates real-time.
  • For quorum: draw N replicas in a circle, highlight a read quorum R and a write quorum W with at least one overlapping node; annotate R + W > N.

Formulas

R + W > N — R is the number of replicas contacted for a read (unit: count), W is the number of replicas contacted for a write (count), N is total replicas (count). Applies to quorum-based replication to guarantee reads see the latest completed writes (read/write quorums intersect). This guarantee assumes reads select the value with the highest version/timestamp among the R replies; for multi-writer atomic registers, reads typically write back the chosen version to R or W replicas (e.g., ABD) to ensure linearizability.

W > N/2 — W is write quorum size, N is number of replicas. Ensures any two write quorums intersect (prevents write-write conflicts without further coordination).

(No SI units other than counts apply; these are integer parameters.)

Worked examples

Example 1 — quorum numeric check (stepwise): Problem: N = 5 replicas. Choose R and W so that reads see latest writes and writes conflict-safe. Show two valid choices and one invalid choice. Solution steps:

  1. Condition 1: R + W > N = 5. Condition 2 (optional): W > N/2 = 2.5, so W ≥ 3.
  2. Choose W = 3 (satisfies W > 2.5). Then R must satisfy R + 3 > 5 → R > 2 → R ≥ 3. So (R, W) = (3,3) works.
  3. Choose W = 4. Then R + 4 > 5 → R > 1 → R ≥ 2. So (R, W) = (2,4) works.
  4. Invalid choice: (R, W) = (1,2). Check R + W = 3 ≤ 5 — fails. Also W = 2 ≤ 2.5 so two writes may not intersect. Answer: Valid examples: (3,3) and (2,4). Invalid: (1,2).

Example 2 — protocol trace: primary-backup write with synchronous replication. Problem: Primary P and backups B1, B2. Client C issues write X=5. Primary must replicate to both backups synchronously before acknowledging. Trace:

  1. C -> P: write X=5 (invoke).
  2. P assigns a new sequence number (log entry) and applies to local copy.
  3. P -> B1: replicate (X=5, seq). P -> B2: replicate (X=5, seq).
  4. B1, B2 apply and reply ACK to P.
  5. After P receives ACKs from both backups, P sends response to C (acknowledge success). If P crashes after step 2 before replication, a new primary election or recovery must detect incomplete commit (either discard unreplicated entry or reapply). This design can give linearizable writes if the primary synchronously replicates to enough backups before acknowledging and if failover preserves ordering.

Example 3 — causal ordering with vector clocks (theory question model answer): Question: Explain how vector clocks enforce causal consistency for two processes P1 and P2 sharing object O. Model answer (concise): Each process maintains a vector clock VC of size number of processes. On a local event (write) P1 increments VC1[1] and attaches a copy of VC to the write. When P2 receives or reads an update with VCu, P2 ensures for all i ≠ sender that VCu[i] ≤ VC2[i] and that VCu[sender] = VC2[sender] + 1; otherwise, the update is buffered until dependencies arrive. This ensures that if event A causally happened-before B, B's attached vector clock reflects A and cannot be applied before A, preserving causal order. (Include explicit small trace in exam if asked.)

Common mistakes

  • Using R + W ≥ N instead of R + W > N (off-by-one confusion). For integer N, R + W > N ensures at least one common replica. If you write ≥ you must reason with equality cases: R + W = N can allow disjoint quorums when replicas chosen adversarially.
  • Assuming eventual consistency gives any ordering guarantees — it only guarantees convergence, not order.
  • Failing to state the failure model (crash vs Byzantine) when describing a protocol.
  • Forgetting to mention how concurrent writes are resolved (timestamps, vector clocks, CRDTs) when describing eventual or causal systems.

For your exam

Typical question types:

  • Definitions and comparisons (e.g., linearizability vs sequential consistency). Full marks: precise definitions, assumptions, one small example distinguishing them, and implications.
  • Derivations (e.g., derive quorum condition). Full marks: show intersection requirement, algebraic step to R + W > N, and explain integer rounding.
  • Numerical quorum design: choose R and W for given N and latency/availability targets. Full marks: show constraints, list valid pairs, explain tradeoffs.
  • Protocol trace/algorithm explanation (e.g., primary-backup, quorum read/write). Full marks: list message sequence, specify failure handling and correctness argument.
  • Short essay on trade-offs (CAP, latency vs consistency). Full marks: mention partitions, availability, linearizability cost, and example systems.
  • Draw and explain diagrams (timelines showing real-time ordering). Full marks: correct labeling of invocations/responses, shown illegal/allowed orders.

Quick check

  1. What condition on R and W ensures that a read sees the latest completed write in an N-replica quorum system?
  2. Does linearizability require respecting real-time order between operations? (yes/no)
  3. Name one method to resolve conflicts in eventually consistent systems.

Answers: 1) R + W > N. 2) Yes. 3) Last-writer-wins (timestamp), CRDT merge, or application-specific deterministic merge (any one).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?