Replication and Consistency, in Plain English

Cassandra keeps several copies of every row on different machines. That way, one machine dying is no big deal. The catch is that the copies can get out of sync. This part covers how they drift apart, and the tools Cassandra uses to bring them back together.

How Many Copies, and Where

Replication factor (RF)

  • RF is the number of copies of each row. RF=3 means 3 nodes hold every row.
  • You set RF per keyspace (Cassandra's word for a database), not per table.
  • You can set a different RF for each data center:
CREATE KEYSPACE my_app
  WITH replication = {
    'class': 'NetworkTopologyStrategy',
    'us-east': 3,   -- 3 copies in us-east
    'eu-west': 2    -- 2 copies in eu-west
  };

The three levels of layout

NodeOne machine running Cassandra.
RackA group of nodes that share power and network. They tend to fail together.
Data centerA group of racks, usually one building or one cloud region.

Cassandra puts each copy on a different rack. So losing one node, one rack, or even one data center never wipes out every copy.

Two placement strategies

StrategyWhat it doesUse it for
SimpleStrategyPicks the next N nodes on the ring. Ignores racks and data centers.Local dev only. Never production.
NetworkTopologyStrategyRF per data center, and copies spread across racks.Always use this in production.
  • You get: RF copies on different nodes, spread out according to your labels.
  • You don't get: Any check that your labels match the real hardware.

The R + W > N Rule

The three numbers

NTotal copies (same as RF).
WHow many copies must confirm a write.
RHow many copies you ask during a read.

R + W > N

Why it works: picture 3 boxes. The write touches 2 of them. The read touches 2 of them. With only 3 boxes, two groups of 2 have to share a box. That shared box has the new value, so the read sees it.

Two panels, N equals 3 in both. Left: W 2 and R 2. The write touches A and B, the read touches B and C; B is shared, so the read sees the write. Right: W 1 and R 1. The write touches A and the read touches C; nothing is shared, so the read can miss the write.
Overlap means the read can't miss the write

Consistency levels

You don't type R and W as numbers. You pick a consistency level (CL) for each query. Here is what each one means when RF=3:

LevelCopies neededNotes
ONE1Fast. Only fresh if the other side uses ALL.
QUORUM2 (a majority)Pair with QUORUM: 2 + 2 > 3 ✓
LOCAL_QUORUM2, in your own data centerThe usual production choice. Overlap holds inside one data center only.
ALL3Most consistent. One node down and the query fails.

QUORUM is not free

ONEQUORUM (RF=3)
Copies asked12
SpeedAs fast as the fastest copyAs slow as the slower of the two
Nodes you can lose21

Using ONE is fine when a few seconds of old data doesn't matter (analytics, caches). The mistake is using ONE and still expecting fresh reads.

Reads and writes behave differently

Write at QUORUMSent to all 3 copies. Returns when 2 confirm.
Read at QUORUMSent to only 2 copies. Needs both to answer.

Why? Writes are small, so sending an extra copy is cheap. Read results can be large, so Cassandra only asks the copies it needs.

Digest reads: saving bandwidth

Of the 2 copies a QUORUM read asks, only one sends the real data:

  1. The fastest copy sends the full data.
  2. The other copy sends only a hash of its answer (a few bytes).
  3. Hashes match? Return the data. This is almost always the case.
  4. Hashes differ? The copies disagree. Fetch full data from both, keep the newest, and fix the stale copy (read repair, §5).

Speculative retry: when a copy goes quiet

A QUORUM read asks exactly 2 copies, so there is no spare. If one is slow or dead, the read would just wait.

Speculative retry fixes this. If a copy hasn't answered by the table's usual slow time (p99), Cassandra asks the third copy too.

The result: a dead node usually shows up as a small latency spike, not as errors.

SituationWhat happens
Dead copy wasn't askedNothing. Normal read.
Dead copy was asked, a spare existsThird copy is asked. Read succeeds, a bit slower.
Dead copy was asked, no spare (e.g. ALL)Waits 5 seconds, then ReadTimeoutException.

A ReadTimeoutException means "not enough copies answered." Cassandra refuses to guess rather than give you possibly stale data.

One QUORUM read, step by step

Example: RF=3, copies on A, B and C.

  1. The client sends the read to a node. That node is the coordinator.
  2. The coordinator works out which nodes hold the row: A, B, C.
  3. It skips nodes it thinks are down. If fewer than 2 are left, it fails at once with UnavailableException.
  4. It picks the 2 fastest, say A and B. C is not asked.
  5. A sends the data. B sends a hash.
  6. If A or B is too slow, it asks C (speculative retry).
  7. If the answers match, it returns the data.
  8. If they don't, it keeps the newest value, fixes the stale copy, and returns the newest value.

What the rule does NOT protect you from

  • Two writes at the same time. Both can succeed. The rule doesn't decide which one should win.
  • Wrong clocks. The "latest" write is the one with the highest timestamp. If a clock is off, an older write can win.
  • Transactions. You get "reads are probably fresh", not "everything happens in order."
  • You get: A read always sees any write that already finished successfully.
  • You don't get: Protection for writes still in progress, a fair winner between two writes, or honest timestamps.

When Copies Disagree

Case 1: one copy misses a write

RF=3, writes and reads both at QUORUM.

  1. A write goes out to A, B and C.
  2. C is down. A and B save it.
  3. 2 out of 3 confirmed, so the client is told "success".
  4. A later read asks any 2 copies. At least one of them is A or B, so it sees the new value.

Even if the read hits A and C, Cassandra returns A's value because its timestamp is newer. ✓

Case 2: two writes race each other

Two clients update the same row at almost the same moment:

Client 1 writes online at time 100 to replicas A and B, and is told success. Client 2 writes offline at time 102 to replicas B and C, and is told success. In the end A holds online, B and C hold offline. Every QUORUM read returns offline. Client 1's write is lost and Client 1 is never told.
Both writes succeed. One of them is gone.
  • You get: Every QUORUM read returns the newest-timestamp value. Readers never see the disagreement.
  • You don't get: Any promise that a successful write survives, or any alert when it doesn't.

Hinted Handoff

How it works

  1. C is down when a write arrives.
  2. The coordinator saves a hint: "C missed this write."
  3. When C comes back, the coordinator sends it the missed writes. C catches up.

How does the coordinator know C is down?

Nodes constantly share heartbeats (gossip). Each node watches for gaps. After about 10 seconds of silence, it marks C as DOWN in its own view. Different nodes can briefly disagree.

Coordinator thinks C is…What happens
DOWNDoesn't send to C. Saves a hint right away.
UP, but C is really deadSends, gets no reply, times out, then saves a hint.
Too few copies aliveFails with UnavailableException. Nothing written.
A write goes to replicas A, B and C. C is down so the coordinator stores a hint. If C returns within 3 hours, the hint is replayed and C catches up. After 3 hours hints are thrown away; if C returns later it looks healthy but is missing the write, and only nodetool repair fixes it.
Inside 3 hours, the hint heals C. After that, C is silently missing data.

When hints don't save you

  • The coordinator crashes. Hints live only on its disk. They're lost with it.
  • C is down longer than 3 hours. New hints stop being saved, and old ones are dropped.
  • Hints are turned off or the disk is full.
  • C is back but still catching up. It serves reads while behind.
  • You get: A copy that's back within 3 hours catches up automatically.
  • You don't get: Anything after 3 hours, or if the coordinator holding the hints dies. Hints are a convenience, not a safety net.

Read Repair

How it works

  1. A QUORUM read asks 2 copies.
  2. They disagree.
  3. Cassandra picks the value with the newest timestamp and returns it.
  4. It writes that value back to the stale copy.

Foreground read repair

Happens on reads at QUORUM or higher. The coordinator already has both answers, so it compares them right away.

Background read repair (old versions)

On a random ~10% of ONE reads, Cassandra quietly checked the other copies too. Removed in Cassandra 4.0.

The 4.0 change matters

Before 4.0, cheap LOCAL_ONE reads still helped fix copies now and then. From 4.0 on, they fix nothing. Read repair only runs at QUORUM or higher.

ToolWhat it fixesWorks for a LOCAL_ONE-only app on 4.0+?
Hinted handoff (§4)Writes missed during a short outageYes, within 3 hours
Read repair (§5)Rows that get readNo
nodetool repairEverything, even data nobody readsYes, but only when someone runs it
  • You get: Stale copies get fixed as a side effect of reading them.
  • You don't get: Full coverage. Data that's rarely read, usually the oldest and most out of sync, gets fixed last or never.

What "Eventual Consistency" Really Means

What people assumeWhat's actually true
"My write will be everywhere within milliseconds."Usually it is. But Cassandra promises no time limit at all.

"Eventually" can mean "never" when:

  • A node was down more than 3 hours and nobody ran repair (§4).
  • A row is never read, so read repair never touches it (§5).
  • Two writes clash and a wrong clock lets the older one win for good (§3).

Liveness = something good eventually happens. Safety = nothing bad ever happens. Cassandra gives you liveness, not safety. That's fine, as long as your app doesn't assume safety.

  • You get: Copies eventually match, for data that gets read or repaired, once writes stop.
  • You don't get: A deadline, correct results when writes clash, or any report that a clash happened.

Cheat Sheet

TopicRemember this
RFNumber of copies, set per keyspace and per data center.
Rack labelsWrong labels = fake fault tolerance.
R + W > NFresh reads. Not safe writes.
Default CLONE / LOCAL_ONE. Set LOCAL_QUORUM yourself.
Writes vs readsWrites go to all copies. Reads go to just enough.
Speculative retryA dead node shows up as a latency spike, not errors.
Concurrent writesBoth succeed, newest timestamp wins, the loser is never told.
Hinted handoff3-hour window. After that, run nodetool repair.
Read repairOnly fixes rows that are read, and only at QUORUM+ on 4.0+.
nodetool repairThe only thing that fixes everything. Schedule it.
Eventual consistencyNo deadline, no correctness promise.