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
| Node | One machine running Cassandra. |
|---|---|
| Rack | A group of nodes that share power and network. They tend to fail together. |
| Data center | A 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
| Strategy | What it does | Use it for |
|---|---|---|
| SimpleStrategy | Picks the next N nodes on the ring. Ignores racks and data centers. | Local dev only. Never production. |
| NetworkTopologyStrategy | RF 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
| N | Total copies (same as RF). |
|---|---|
| W | How many copies must confirm a write. |
| R | How 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.
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:
| Level | Copies needed | Notes |
|---|---|---|
ONE | 1 | Fast. Only fresh if the other side uses ALL. |
QUORUM | 2 (a majority) | Pair with QUORUM: 2 + 2 > 3 ✓ |
LOCAL_QUORUM | 2, in your own data center | The usual production choice. Overlap holds inside one data center only. |
ALL | 3 | Most consistent. One node down and the query fails. |
QUORUM is not free
ONE | QUORUM (RF=3) | |
|---|---|---|
| Copies asked | 1 | 2 |
| Speed | As fast as the fastest copy | As slow as the slower of the two |
| Nodes you can lose | 2 | 1 |
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 QUORUM | Sent to all 3 copies. Returns when 2 confirm. |
|---|---|
| Read at QUORUM | Sent 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:
- The fastest copy sends the full data.
- The other copy sends only a hash of its answer (a few bytes).
- Hashes match? Return the data. This is almost always the case.
- 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.
| Situation | What happens |
|---|---|
| Dead copy wasn't asked | Nothing. Normal read. |
| Dead copy was asked, a spare exists | Third 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.
- The client sends the read to a node. That node is the coordinator.
- The coordinator works out which nodes hold the row: A, B, C.
- It skips nodes it thinks are down. If fewer than 2 are left, it fails at once with
UnavailableException. - It picks the 2 fastest, say A and B. C is not asked.
- A sends the data. B sends a hash.
- If A or B is too slow, it asks C (speculative retry).
- If the answers match, it returns the data.
- 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.
- A write goes out to A, B and C.
- C is down. A and B save it.
- 2 out of 3 confirmed, so the client is told "success".
- 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:
- 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
- C is down when a write arrives.
- The coordinator saves a hint: "C missed this write."
- 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 |
|---|---|
DOWN | Doesn't send to C. Saves a hint right away. |
UP, but C is really dead | Sends, gets no reply, times out, then saves a hint. |
| Too few copies alive | Fails with UnavailableException. Nothing written. |
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
- A QUORUM read asks 2 copies.
- They disagree.
- Cassandra picks the value with the newest timestamp and returns it.
- 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.
| Tool | What it fixes | Works for a LOCAL_ONE-only app on 4.0+? |
|---|---|---|
| Hinted handoff (§4) | Writes missed during a short outage | Yes, within 3 hours |
| Read repair (§5) | Rows that get read | No |
nodetool repair | Everything, even data nobody reads | Yes, 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 assume | What'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
| Topic | Remember this |
|---|---|
| RF | Number of copies, set per keyspace and per data center. |
| Rack labels | Wrong labels = fake fault tolerance. |
| R + W > N | Fresh reads. Not safe writes. |
| Default CL | ONE / LOCAL_ONE. Set LOCAL_QUORUM yourself. |
| Writes vs reads | Writes go to all copies. Reads go to just enough. |
| Speculative retry | A dead node shows up as a latency spike, not errors. |
| Concurrent writes | Both succeed, newest timestamp wins, the loser is never told. |
| Hinted handoff | 3-hour window. After that, run nodetool repair. |
| Read repair | Only fixes rows that are read, and only at QUORUM+ on 4.0+. |
nodetool repair | The only thing that fixes everything. Schedule it. |
| Eventual consistency | No deadline, no correctness promise. |