SkillSprout
Chapter 20 · Lesson 2
Advanced
5 min read

Tuning N, W and R, and Repairing Replicas

Overview

Databases such as Cassandra and DynamoDB-style systems let you choose how many replicas must answer each read and write. That choice is a dial between freshness, speed and availability.

You will learn the N, W, R rule, and how replicas that fall behind are repaired.

Detailed Notes

What are N, W and R

  • N: number of replicas that store each item (for example 3).
  • W: how many replicas must acknowledge a write before it is reported successful.
  • R: how many replicas must respond to a read before it returns.

The node that receives the client's request and talks to the replicas is called the coordinator.

The problem it solves

Different data has different needs. A shopping cart should always accept an item, even during a partial outage. A user's password hash should always read the latest value. One database can serve both if each use sets W and R differently.

How it works

The overlap rule: W + R > N. If the number of write acknowledgements plus read responses exceeds N, the nodes that took the write and the nodes that answer a read must share at least one replica. That replica has the latest value, and the coordinator returns the version with the newest timestamp or version number.

Examples with N = 3:

WRW + RBehaviour
224 > 3Reads see the latest write; tolerates 1 down node for reads and writes
314 > 3Fast reads, but one down node blocks writes
134 > 3Fast writes, but one down node blocks reads
112 < 3Fastest and most available, but reads may be stale (replicas only catch up eventually, called eventual consistency)

Lower W and R give lower latency and higher availability but weaker freshness. Higher values give fresher reads but slower and less available operations. In Cassandra these appear as levels: ONE, QUORUM (a majority of all replicas), LOCAL_QUORUM (a majority inside the local data centre, avoiding slow cross-region trips) and ALL.

Overlap alone is not a perfect guarantee. Two clients can write the same key at once and leave conflicting values, which needs a rule such as last write wins (which can drop a write if clocks are skewed).

Sloppy quorums and hinted handoff. A strict quorum uses only the N designated replicas. A sloppy quorum lets the write go to other healthy nodes if designated ones are down, so writes keep succeeding. The stand-in stores a hint naming the real owner and hands the data over when the owner returns (hinted handoff). The price: the overlap guarantee no longer holds, so reads may be stale.

Read repair. During a read, the coordinator compares replies. If some replicas have an older version, it returns the newest to the client and writes it back to the stale replicas. This heals commonly read data cheaply.

Anti-entropy repair. A background process compares replicas and syncs differences, often using Merkle trees (trees of hashes that let two nodes find differing ranges by comparing a few hashes). It fixes data that is rarely read, which read repair would never touch.

A simple example

Three friends each keep a copy of a shared shopping list. To add an item, you must tell 2 of them (W = 2). To check the list, you call 2 of them (R = 2). Since 2 + 2 > 3, at least one friend you call heard the latest change. If one friend was away and missed a change, you notice they differ and tell them the newest version (read repair).

System design example

A shopping cart uses a low W with sloppy quorums so adds always succeed, accepting that a removed item might occasionally reappear. A payment profile uses QUORUM for both reads and writes so it always sees the latest value. In a multi-region deployment, LOCAL_QUORUM keeps each region fast and independent. When one node has a short outage, hinted handoff buffers the writes it misses, and once it returns, hints are replayed and read repair fixes any leftovers. Users notice nothing. The right settings are a business decision: how stale may this data be, and how much downtime is acceptable?

Summary

  • N replicas, W write acks, R read responses; W + R > N makes reads overlap the latest write.
  • Lower W/R means faster and more available but possibly stale; higher means fresher but slower.
  • Sloppy quorums with hinted handoff keep writes available, trading away the overlap guarantee.
  • Read repair and background anti-entropy keep replicas converging.

Architecture Diagram

Quorum read and write with repair
Architecture
Client
Coordinator

N = 3 replicas

Replica Av2
Replica Bv2
Replica Cv1 (stale)

W and R overlap; stale replicas are repaired.

  1. 1A write needs W acknowledgements, here 2 of 3.
  2. 2A read asks R replicas, here 2 of 3, and sees different versions.
  3. 3The coordinator returns the newest version.
  4. 4It writes the newest version to the stale replica (read repair).

Common Interview Questions

Chapter quiz

Quorum · 5 questions

  1. 1.What is a majority of 7 nodes?

  2. 2.Why do two majorities always overlap?

  3. 3.How many failures can a 5-node quorum cluster tolerate?

  4. 4.With N = 3, which pair gives overlapping read and write quorums?

  5. 5.What is read repair?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course