Replication and content distribution
Notes on replication and content distribution: definitions, replication strategies, quorum rules, availability formulas, caching and CDNs, worked examples, common mistakes and likely exam questions.
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
Replication and content distribution are used by engineers to make services highly available, fault-tolerant and fast for users worldwide. Examples: database replicas for read-scaling and fault tolerance, and Content Delivery Networks (CDNs) that cache website assets at edge servers to reduce latency.
Key ideas
Availability — the probability that the service can respond to requests (often modelled as a probability between 0 and 1). When replicas are independent and each has availability a, replication increases overall availability.
Partition tolerance — the system continues to operate despite network partitions. Mention the CAP trade-off: in presence of partitions, a distributed system can provide either consistency or availability but not both (refer to the CAP theorem topic for formal proof and assumptions).
Replication strategies (brief definitions):
- Passive replication (primary-backup) — one replica (primary) processes updates and forwards state changes to backups; backups apply changes and can take over on failure.
- Active replication (state-machine replication) — all replicas receive the same sequence of commands (total order) and execute deterministically so they stay in sync; requires ordering (consensus protocols such as Paxos/Raft — refer to Consensus topic).
- Quorum-based replication — clients contact subsets (quorums) of replicas to perform reads and writes. Defined below.
- Chain replication — replicas arranged in a chain; writes flow head→tail and are stable at tail; reads taken from tail for strong consistency.
Quorum rules (exact statement and assumptions):
- Suppose N replicas, a read operation contacts any R replicas and a write operation contacts any W replicas (R and W are integers 1..N). Assume crash-stop failures only (no Byzantine), reliable message delivery when network connected, and every write carries a monotonically increasing version number or timestamp. Then strong-read-after-write visibility is guaranteed if the quorums intersect: R + W > N. This ensures at least one replica participating in a read has the latest write. To realize this guarantee, the read must choose the value with the highest version/timestamp among the R replies (and may perform read-repair/write-back of that value to the contacted replicas).
- To ensure two writes do not both succeed without overlapping (i.e., to avoid lost updates), require W > N/2. This guarantees any two write quorums overlap.
Derivation of R + W > N (step-by-step):
- Let set Wset be the set of replicas that acknowledged a write (|Wset| = W). Let set Rset be the set of replicas read by a later read (|Rset| = R).
- If R + W > N, by the pigeonhole principle Rset ∩ Wset ≠ ∅ (they must share at least one replica).
- That intersection replica has at least the written version (writes update their Wset members with the write's version). If the read selects the highest version/timestamp among its R replies, it returns the newest value known to Rset (which includes the write).
- Assumes the write's update reached its Wset members before read; if a read overlaps an in-progress write, versions or timestamps are used to choose latest.
Caching and content distribution terms:
- Content Delivery Network (CDN) — a distributed set of edge caches placed near clients; the origin server is the authoritative source.
- Cache hit — requested object found in cache; cache miss — not found and must be fetched from origin.
- TTL (time-to-live) — how long a cached item is considered fresh.
- Cache consistency methods: invalidation/purge (origin tells caches to invalidate or purge an object), TTL and HTTP revalidation (caches respect TTL and use conditional requests such as If-Modified-Since/ETag to check freshness), leases (cache holds a temporary guarantee), and pull-based refresh (cache refreshes on miss or expiry).
Trade-offs engineers consider: latency vs freshness (short TTL gives fresh data but more origin load), availability vs consistency (shorter write quorums improve availability but reduce consistency), and throughput vs coordination (active replication needs consensus which limits write throughput).
Assumptions listed where relevant: crash-stop model, no Byzantine faults, independent replica failures in simple availability formulas, and existence of version numbers for detecting latest writes.
Drawings to make in an exam (describe exactly):
- Replica-and-quorum diagram: draw N circles labelled R1..RN. Shade any W circles for a write quorum and any R circles for a read quorum such that at least one overlaps. Label N, R, W and annotate "R + W > N ⇒ overlap".
- CDN diagram: draw origin server on left, several edge caches at the edge, and clients connected to nearest edge; label cache hit and cache miss path to origin.
Formulas
Availability_total = 1 - Π_{i=1..N} (1 - a_i) — a_i is availability (probability of being up) of replica i (unitless probability). Applies when failures independent.
Availability_identical = 1 - (1 - a)^N — a is per-replica availability, N replicas identical. Unitless. Applies under independent failures.
Quorum_write_availability_identical = Σ_{k=W}^{N} C(N,k) a^k (1 - a)^{N - k} — probability that at least W replicas are up (independent failures).
Quorum_read_availability_identical = Σ_{k=R}^{N} C(N,k) a^k (1 - a)^{N - k} — probability that at least R replicas are up (independent failures).
Quorum_condition_for_read_write = R + W > N — integers R, W, N; ensures read sees latest write under assumptions listed.
Quorum_condition_for_write_overlap = W > N/2 — ensures write quorums always overlap (prevents concurrent writes from being lost).
Needed_replicas_for_target = ceil( log(1 - A_target) / log(1 - a) ) — solve Availability_identical ≥ A_target for N; a and A_target in (0,1). Unitless.
Worked examples
- Availability with identical replicas
- Problem: Each replica has availability a = 0.98. You deploy N = 3 replicas. Compute total service availability assuming independent failures.
- Formula:
Availability_identical = 1 - (1 - a)^N. - Numbers:
1 - (1 - 0.98)^3 = 1 - (0.02)^3 = 1 - 0.000008 = 0.999992. - Answer: 0.999992 (≈ 99.9992% availability).
- Choosing R and W to guarantee strong reads, and trade-offs (N = 5)
- Problem: N = 5 replicas. What choices of R and W satisfy R + W > 5? What choices satisfy W > 5/2?
- Options meeting R + W > 5: (W=3, R=3) because 3+3=6>5; (W=4,R=2) etc. Show table quickly (students draw):
- W=3, R=3 ⇒ R+W>5 true; W>5/2 true (3>2.5) so writes overlap.
- W=4, R=1 ⇒ 4+1=5 (NOT >5), so fails; must be strictly >5.
- W=4, R=2 ⇒ 6>5 OK.
- Trade-offs: W small (e.g., W=3) reduces write latency (need acknowledgements from fewer machines) but R must be ≥3 for strong reads, so read latency increases. For read-heavy workloads choose small W and larger R if reads can accept contacting more replicas.
- Answer style: list legal pairs and note that W must be at least 3 for write-overlap.
- Primary-backup failover trace (small-step)
- Setup: replicas R1(primary), R2(backup), R3(backup). Client issues write W1 to primary.
- Steps:
- Client → R1: W1 with version v=1.
- R1 applies W1, forwards state or log to R2 and R3 synchronously (write-ahead) and waits for acknowledgements (synchronous replication assumed).
- R1 replies to client after R2 and R3 ack.
- R1 crashes. R2 detects missing heartbeats and runs election or uses preconfigured priority to become primary.
- New primary R2 has applied W1, so client can continue with reads/writes without losing W1.
- Important to state assumptions: backups applied updates before ack; otherwise data loss may occur.
Common mistakes
- Treating availability multiplication rules as valid when failures are correlated (they require independence).
- Confusing linearizability with sequential consistency. Be precise with definitions.
- Assuming replication always increases write throughput — coordination (consensus, synchronous replication) can make writes slower.
- Using W > N/2 without noting it's sufficient for write-write overlap but you still need R + W > N for read-after-write visibility.
For your exam
Types of questions you will get: definitions (replication, consistency types), short notes (quorum protocols, CDN caching strategies), derivations (prove R + W > N implies read/write overlap), numericals (compute availability with N replicas, choose R/W for given N), algorithm traces (primary-backup failover, quorum read/write steps), and diagrams (draw quorum overlap or CDN).
What a full-marks answer includes:
- Precise definitions with bolded terms (replication, consistency, availability).
- Clear list of assumptions (failure model, independence, versioning).
- Exact statements of quorum conditions with step-by-step proof using the pigeonhole principle.
- Correct formulas, units (probabilities unitless), and clear numerical substitution with unitless results.
- A diagram when asked: label N, R, W and explicitly show overlap.
- Mention trade-offs (latency vs freshness, availability vs consistency).
Quick check
- State the quorum condition that guarantees a read sees the latest write.
- If per-replica availability is 0.95 and replicas are independent, what is availability with N=2?
- Name two cache consistency strategies used in CDNs. Answers: 1) R + W > N. 2) 1 - (1 - 0.95)^2 = 1 - 0.05^2 = 1 - 0.0025 = 0.9975 (99.75%). 3) Invalidation/purge and TTL with HTTP revalidation (also leases, pull-based refresh).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?