Replication, Repair, Hints and Node Replacement

Article · 8:00

Replication factor and topology, racks and datacentres, hinted handoff and read repair, anti-entropy repair, snapshots and node replacement.

Replication is per keyspace, per datacentre. NetworkTopologyStrategy with {'dc1': 3, 'dc2': 3} places three replicas in each datacentre. The snitch tells Cassandra the topology, and GossipingPropertyFileSnitch is the standard choice.

Racks matter, and getting them wrong silently removes your redundancy. Cassandra places replicas in distinct racks where it can, so RF=3 across three racks survives a rack failure. If all your nodes report rack1, replicas land on arbitrary nodes and a rack-level outage can take all three. Conversely, an unbalanced rack configuration - two racks for RF=3 - produces uneven replica distribution. Either use one rack consistently or use exactly RF racks evenly; anything else is worse than both.

Three repair mechanisms, and they do different jobs:

Hinted handoff is short-term. When a replica is down, the coordinator stores a hint and replays it when the node returns. Hints are kept for max_hint_window_in_ms (three hours by default) and then discarded - so a node down for longer than the hint window has permanently missed those writes until a repair, and that is the sentence that makes repair non-optional.

Read repair is opportunistic: when a read at a consistency level above ONE finds replicas disagreeing, the coordinator pushes the newest value to the stale ones. It only fixes data you happen to read.

Anti-entropy repair (nodetool repair) is the real mechanism: it builds Merkle trees of the data on each replica, compares them and streams the differences. It is not optional - it is what makes gc_grace_seconds safe, and it must complete on every node more often than gc_grace_seconds. A full repair is expensive; incremental repair marks repaired SSTables so subsequent runs do less work, and has had a troubled history, so many operators run full repairs with a scheduler such as Reaper. Note the modes: -pr (primary range) is how you repair a whole cluster without doing the same work RF times, and -full against incremental matters for which SSTables are considered.

Snapshots are hard links to the current SSTables - instant, nearly free in space until compaction diverges, and the basis of backup. The procedure is snapshot, then copy the files off the node, because a snapshot on the node protects against nothing that destroys the node. Incremental backups copy each new SSTable as it is flushed.

Node replacement has a specific procedure and getting it wrong is how clusters break. For a dead node: start the replacement with -Dcassandra.replace_address_first_boot so it takes over the dead node's token ranges and streams data from the other replicas. For a planned removal: nodetool decommission on the node, which streams its data away before leaving. nodetool removenode is for a node that is already gone and cannot be replaced. Never just start a new node with the same address and no replace flag - it will join as a new node, and the ring will be wrong.

Laboratory exercise 5 - lose a node, then lose everything.

Configure NetworkTopologyStrategy with three racks and RF=3, and prove from nodetool getendpoints that replicas land in distinct racks. Then misconfigure all nodes to one rack and show the redundancy gone.

Kill a node under write load at LOCAL_QUORUM and measure: writes continuing, latency change, and the hints accumulating. Bring it back and show the hints replaying. Then kill a node for longer than the hint window, bring it back, and show it missing data - then repair and show it consistent.

Kill two of three and show writes failing at QUORUM while succeeding at ONE, and explain what that means for your consistency arithmetic.

Run a full repair and an incremental repair, and record the duration, the data streamed and the impact on read latency during each.

Take a snapshot, destroy the node entirely, and replace it properly with the replace-address procedure. Time it, record RPO and RTO, and verify with assertions rather than with nodetool status.

Then do the same wrongly - a new node with no replace flag - and show the ring broken, so you have seen both.

Deliverable: the rack configuration with endpoint proof and the misconfigured comparison, the node loss measurements with hint counts, the beyond-hint-window demonstration and its repair, the two-of-three failure, the repair timings, the correct replacement with measured RPO and RTO, the incorrect replacement's symptom, and verification by assertions.

-- ==================================================================
-- REPLICATION, RACKS AND DATACENTRES.
-- ==================================================================
ALTER KEYSPACE feed WITH replication = {
  'class': 'NetworkTopologyStrategy', 'dc1': 3, 'dc2': 3 };

SELECT keyspace_name, replication FROM system_schema.keyspaces;
--   nodetool status feed          -> effective ownership per node
--   nodetool getendpoints feed events_by_user_day 7f3c...
--     172.18.0.2   (rack1)
--     172.18.0.3   (rack2)
--     172.18.0.4   (rack3)        <- DISTINCT RACKS. Good.

-- *** RACKS MATTER, AND GETTING THEM WRONG SILENTLY REMOVES YOUR
-- *** REDUNDANCY.
--   cassandra-rackdc.properties:
--     dc=dc1
--     rack=rack1
--   endpoint_snitch: GossipingPropertyFileSnitch
--
--   ALL NODES SAY rack1  -> replicas land on arbitrary nodes, and a
--                           rack-level outage can take ALL THREE.
--                           Demonstrate it with getendpoints.
--   TWO racks for RF=3   -> UNEVEN distribution; one rack holds two
--                           replicas of many partitions.
--
--   SO: use ONE rack consistently, OR EXACTLY RF racks EVENLY.
--   Anything else is worse than both.

-- ==================================================================
-- THREE REPAIR MECHANISMS. They do DIFFERENT JOBS.
-- ==================================================================
-- 1. HINTED HANDOFF - short-term only.
--   A replica is down -> the coordinator stores a HINT and replays
--   it when the node returns.
--     nodetool statushandoff
--     nodetool handoffwindow
--   cassandra.yaml: max_hint_window_in_ms: 10800000   (3 HOURS)
--
--   *** A NODE DOWN LONGER THAN THE HINT WINDOW HAS PERMANENTLY
--   *** MISSED THOSE WRITES, until a repair. ***
--   That sentence is why repair is not optional. Demonstrate it:
--   stop a node for longer than the window, bring it back, read at
--   CONSISTENCY ONE against it, and show the missing data.

-- 2. READ REPAIR - opportunistic.
ALTER TABLE events_by_user_day WITH read_repair = 'BLOCKING';
--   At a consistency level above ONE, when replicas disagree, the
--   coordinator pushes the newest value to the stale ones.
--   IT ONLY FIXES DATA YOU HAPPEN TO READ. Not a strategy.

-- 3. ANTI-ENTROPY REPAIR - the real mechanism.
--   Builds MERKLE TREES per replica, compares, streams differences.
--
--     nodetool repair -pr feed              # PRIMARY RANGE ONLY
--     nodetool repair -full feed
--     nodetool repair -pr -full --job-threads 2 feed events_by_user_day
--
--   -pr is how you repair a WHOLE CLUSTER without doing the same
--   work RF times: run it on EVERY node, and together they cover
--   everything exactly once.
--
--   *** IT MUST COMPLETE ON EVERY NODE MORE OFTEN THAN
--   *** gc_grace_seconds. *** That is the whole contract that makes
--   deletes safe (Module 4).
--
--   INCREMENTAL repair marks repaired SSTables so later runs do less
--   work. It has had a TROUBLED HISTORY, so many operators run full
--   -pr repairs on a schedule with Cassandra Reaper instead. Know
--   both; pick deliberately.
--     nodetool repair_admin list
--     nodetool tpstats | grep -i repair

-- ==================================================================
-- SNAPSHOTS AND BACKUP.
-- ==================================================================
--   nodetool snapshot -t before-upgrade feed
--     -> HARD LINKS to the current SSTables. Instant, and nearly
--        free in space UNTIL compaction diverges from them.
--   nodetool listsnapshots
--   nodetool clearsnapshot -t before-upgrade feed
--
--   *** THE PROCEDURE: snapshot, THEN COPY THE FILES OFF THE NODE.
--   A snapshot ON the node protects against NOTHING that destroys
--   the node. ***
--     tar/rsync /var/lib/cassandra/data/feed/*/snapshots/before-upgrade
--     -> object storage
--
--   Incremental backups copy each new SSTable as it is flushed:
--     cassandra.yaml: incremental_backups: true
--
--   Restore: place the SSTables in the table directory and
--     nodetool refresh feed events_by_user_day
--   or use sstableloader to stream them into a cluster with a
--   different topology - which is also how you move between
--   clusters of different sizes.

-- ==================================================================
-- NODE REPLACEMENT. Getting this wrong is how clusters break.
-- ==================================================================
-- A DEAD node -> REPLACE its token ranges:
--   on the replacement, in jvm.options:
--     -Dcassandra.replace_address_first_boot=172.18.0.4
--   then start it. It TAKES OVER the dead node's ranges and streams
--   data from the other replicas.
--     nodetool netstats          # watch the streaming
--     nodetool status            # UJ (joining) -> UN
--   Afterwards: nodetool repair on the new node.

-- A PLANNED removal -> DECOMMISSION, which streams its data AWAY
-- before leaving, so the cluster is never under-replicated:
--   nodetool decommission
--     nodetool netstats
--
-- A node ALREADY GONE and not being replaced:
--   nodetool removenode <host-id>
--   (the surviving replicas stream to cover its ranges)
--
-- *** NEVER just start a new node with the same address and NO
-- *** replace flag. *** It joins as a NEW node, takes new token
-- ranges, and the ring is wrong - you now have a cluster that
-- believes data exists where it does not. Demonstrate this in the
-- lab, in a disposable cluster, so you recognise the symptom:
--   nodetool status shows the right node count and
--   nodetool getendpoints returns replicas that hold nothing.

-- ==================================================================
-- VERIFY WITH ASSERTIONS, NOT WITH nodetool status.
-- ==================================================================
--   "All nodes UN" says the processes are up. It says nothing about
--   whether the data is right.
CONSISTENCY ALL;
SELECT COUNT(*) FROM events_by_user_day
WHERE user_id = 7f3c... AND day = '2026-11-30';
--   CONSISTENCY ALL on a verification read makes every replica
--   answer, so a missing or divergent replica shows up as an error
--   or a different count rather than being hidden.
--   ... then run the capstone invariants.

Discussion

No questions on this topic yet. Sign in to ask the first one.