Raft, Node Failure, Backup and Survival Goals

Article · 7:00

Quorum and what losing it means, leaseholder transfer, node failure and decommissioning, BACKUP and RESTORE, and zone against region survival.

Quorum is the whole story of availability here. With three replicas, losing one node leaves two - a majority - so the range stays available and the lease moves within seconds. Losing two of three leaves no majority, and that range becomes unavailable for both reads and writes until a replica returns. Not inconsistent, not lost: unavailable. CockroachDB chooses consistency over availability when it must, and understanding that is understanding the database.

The arithmetic generalises: 2n+1 replicas survive n failures. Three survives one, five survives two. Replication factor is set per zone configuration, so important tables can be more replicated than the rest.

Node failure, in order of what happens. The lease transfers to a surviving replica, usually within a few seconds. Clients connected to the dead node get connection errors and must reconnect - a connection pool that does not retry its connections will report an outage the cluster did not have. After server.time_until_store_dead (five minutes by default) the cluster declares the node dead and begins re-replicating its ranges elsewhere to restore the replication factor.

Decommissioning is different from failure and is what you should do for planned work: cockroach node decommission moves every replica off a node before it leaves, so the cluster is never under-replicated. Draining is the gentler version for a restart.

Backup and restore. BACKUP writes a consistent snapshot to object storage, and incremental backups capture changes since the last one. RESTORE can bring back the whole cluster, one database or one table. Restoring a single table from a cluster backup is genuinely useful and worth practising. Scheduled backups (CREATE SCHEDULE) are the production mechanism, and SHOW BACKUP inspects what you have - which you should do before you need it.

Note the licensing boundary: cluster-level and incremental backups require an enterprise licence; full database and table backups to object storage are available in the core offering. CockroachDB Cloud includes managed backups.

Survival goals are the multi-region concept that matters most. ZONE survival (the default) keeps the database available through the loss of one availability zone, and needs three replicas. REGION survival keeps it available through the loss of an entire region - which requires replicas in at least three regions, and therefore means every write must reach a quorum spanning regions. The cost is explicit: region survival makes writes slower, permanently, in exchange for surviving a regional outage. That is the trade, it cannot be avoided, and the capstone must state which side of it the design sits on.

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

On a three-node cluster, kill one node while a write workload runs. Measure: how long writes failed, how long the lease took to move, and whether any committed write was lost. Then kill a second node and show the ranges becoming unavailable rather than inconsistent - and bring a node back and show recovery.

Decommission a node properly while under load and show the cluster never went under-replicated.

Take a full backup and an incremental, inspect them with SHOW BACKUP, then restore a single table into a new database and verify it. Record RPO and RTO for the restore, measured.

For multi-region, on a nine-node demo cluster set SURVIVE REGION FAILURE, measure write latency before and after, then kill an entire region and show the database still serving. If a nine-node cluster is impractical, produce a design exercise: topology, survival goal, the measured or estimated write latency cost, and the runbook.

Deliverable: the recovery runbook, measured lease transfer time, the unavailability demonstration with the exact error, the decommission evidence, the single-table restore with measured RPO and RTO, and the survival goal comparison with write latency on both settings.

-- ------------------------------------------------------------------
-- Quorum. The whole story of availability.
-- ------------------------------------------------------------------
--   3 replicas: survives 1 failure   (2 of 3 is a majority)
--   5 replicas: survives 2 failures
--   2n+1 replicas survive n failures
--
-- Lose 2 of 3 and the range is UNAVAILABLE - for reads AND writes -
-- until a replica returns. Not inconsistent. Not lost. Unavailable.
-- CockroachDB chooses consistency over availability when it must.

ALTER TABLE orders CONFIGURE ZONE USING num_replicas = 5;
-- Important tables can be more replicated than the rest.

SELECT range_id, table_name, replicas, lease_holder,
       array_length(replicas, 1) AS replica_count
FROM crdb_internal.ranges WHERE table_name = 'orders' LIMIT 5;

-- Health. Run this during the failure lab:
SELECT node_id, is_live, is_decommissioning, is_draining,
       ranges, ranges_underreplicated, ranges_unavailable
FROM crdb_internal.kv_node_status;
--                  ^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^
-- under-replicated: a replica is missing. Still serving.
-- unavailable:      no quorum. NOT serving. Page someone.

-- ------------------------------------------------------------------
-- Node failure, step by step.
-- ------------------------------------------------------------------
-- 1. Kill node 3.             (kill the process, or docker stop)
-- 2. Lease transfers to a surviving replica - a few seconds. Watch:
SELECT count(*) FILTER (WHERE lease_holder = 3) AS still_on_node_3
FROM crdb_internal.ranges;

-- 3. Clients connected to node 3 get connection errors and MUST
--    reconnect. A connection pool that does not retry its
--    CONNECTIONS will report an outage the cluster did not have.
--    This is the commonest false alarm in CockroachDB operations.

-- 4. After server.time_until_store_dead the cluster declares the node
--    dead and re-replicates its ranges to restore the factor:
SHOW CLUSTER SETTING server.time_until_store_dead;   -- 5m0s default

-- DECOMMISSION is what to do for PLANNED work. It moves every replica
-- off the node BEFORE it leaves, so the cluster is never
-- under-replicated:
--   cockroach node decommission 3 --certs-dir=certs
--   cockroach node drain 3        # gentler, for a restart
SELECT node_id, is_decommissioning, membership FROM crdb_internal.gossip_liveness;

-- ------------------------------------------------------------------
-- BACKUP and RESTORE.
-- ------------------------------------------------------------------
BACKUP DATABASE shop INTO 's3://my-bucket/backups?AWS_ACCESS_KEY_ID=...';

BACKUP DATABASE shop INTO LATEST IN 's3://my-bucket/backups?...';
--                         ^^^^^^ incremental: changes since the last.
--                         Enterprise licence; full database and table
--                         backups are in the core offering.

-- Production: a schedule, not a cron job calling BACKUP.
CREATE SCHEDULE shop_backup
    FOR BACKUP DATABASE shop INTO 's3://my-bucket/backups?...'
    RECURRING '@daily'
    FULL BACKUP '@weekly'
    WITH SCHEDULE OPTIONS first_run = 'now';

SHOW SCHEDULES;
SHOW BACKUPS IN 's3://my-bucket/backups?...';
SHOW BACKUP LATEST IN 's3://my-bucket/backups?...';
-- Inspect your backups BEFORE you need them. A backup nobody has read
-- is a file, not a backup.

-- Restore ONE TABLE from a database backup. Genuinely useful; practise
-- it before the day you need it at 3 a.m.:
RESTORE TABLE shop.orders FROM LATEST IN 's3://my-bucket/backups?...'
    WITH into_db = 'shop_recovery';

-- Point in time, within the configured GC window:
RESTORE DATABASE shop FROM LATEST IN 's3://my-bucket/backups?...'
    AS OF SYSTEM TIME '2026-11-30 14:04:00'
    WITH new_db_name = 'shop_pitr';

SELECT job_id, status, fraction_completed, running_status
FROM [SHOW JOBS] WHERE job_type = 'RESTORE';

-- ------------------------------------------------------------------
-- SURVIVAL GOALS. The trade is explicit and cannot be avoided.
-- ------------------------------------------------------------------
ALTER DATABASE shop SURVIVE ZONE FAILURE;     -- default
--   survives one availability zone; needs 3 replicas
--   write latency: LOCAL

ALTER DATABASE shop SURVIVE REGION FAILURE;
--   survives an entire region; needs replicas in 3+ REGIONS
--   so every write must reach a quorum SPANNING REGIONS
--   write latency: permanently higher. Measure it:
--
--     SURVIVE ZONE FAILURE    insert p50 ~4 ms   p99 ~12 ms
--     SURVIVE REGION FAILURE  insert p50 ~55 ms  p99 ~140 ms
--
-- Region survival buys you a regional outage and costs you every
-- write, forever. State which side of that trade your design is on.

SELECT * FROM [SHOW SURVIVAL GOAL FROM DATABASE shop];
SELECT region, zones, primary FROM [SHOW REGIONS FROM DATABASE shop];

Discussion

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