Lecturas y Escrituras por Quórum

R + W > N

Escrito por Darjus Kazakevic · revisado el

Choose R and W so that every read meets the latest write.

In a leaderless store, each key lives on N replicas and any of them can take a write. The client, or a coordinator node, sends each write to all N and waits for W confirmations. A read asks R replicas and keeps the newest value. Amazon's Dynamo paper made the idea famous; Cassandra and ScyllaDB use it today.

The rule is R + W > N. Then the replicas you read from and the replicas that took the write share at least one node. You tune R and W to make reads or writes cheaper. The cost: overlap does not order two writes that happen at the same time, so you still need timestamps or version vectors to pick a winner.

En esencia — Store each key on N replicas, write to W of them and read from R. When R + W > N the two groups overlap, so a read always reaches a fresh copy.

Cuándo recurrir a él

Recurre a él cuando

  • Data must stay writable when a replica or a whole zone is down
  • You want to trade freshness against latency, query by query
  • High write volume spread over many nodes, as in Cassandra or ScyllaDB

Reconsidéralo cuando

  • You need one order of writes, a unique constraint or a lock
  • Multi-row transactions: quorums protect single keys, not groups
  • One primary database already handles the load

Conceptos clave

N, W and R
N is the replication factor: how many nodes store each key. W is how many must confirm a write, and R how many must answer a read. The other replicas catch up in the background.
Tuning for the load
A lower W makes writes faster and keeps them available when nodes fail. A lower R does the same for reads. Keep R + W > N when reads must be fresh.
Sloppy quorum and hinted handoff
When a key's home replicas are down, another node can take the write and keep a hint. When the home node returns, the hint is handed back. Writes stay available, but R + W > N no longer guarantees overlap.
Read repair and anti-entropy
When a read sees different versions, the coordinator writes the newest one back to the stale replicas. That is read repair. A background repair compares replicas with Merkle trees (hash trees) to fix keys that nobody reads.
Consistency levels in real products
Cassandra lets you choose per query: ONE, QUORUM, LOCAL_QUORUM (a majority in the local data center) or ALL. DynamoDB hides N and offers eventually consistent reads at half the cost, or strongly consistent ones.

Reglas de diseño

Keep R + W > N when reads must be fresh
With N = 3, use W = 2 and R = 2. Each read then shares at least one replica with the last successful write. With W = 1 and R = 1 you get speed and stale reads.
Choose the side that must stay fast
Read-heavy data can use W = N and R = 1. Write-heavy data such as event logs can use W = 1. Set the level per query, not once for the whole cluster.
Decide how conflicts are resolved
Two clients can write the same key at once on different replicas. Last-write-wins by timestamp drops one write and trusts the clocks. Use version vectors or CRDTs when both writes matter.
Run repair on a schedule
Read repair only fixes keys someone reads. Run a full repair regularly, or a replica that missed a delete can bring the deleted data back.

Errores habituales

  • Assuming QUORUM behaves like one database. Concurrent writes, a write that failed after reaching one replica, and sloppy quorums can still return surprising values.
  • Using LOCAL_QUORUM in two data centers and expecting fresh reads across regions. Each region only overlaps with itself.
  • Last-write-wins with skewed clocks. A node whose clock runs 2 seconds fast wins every conflict and silently drops newer writes.
  • Skipping repair. In Cassandra, a replica that missed a delete for longer than gc_grace_seconds brings the deleted row back.