Query icon
If icon
Fail icon
Log icon
SlackIncomingWebhook icon
Pause icon
Loop icon
Sleep icon
Schedule icon

CockroachDB Range Under-Replication Guard

Audit CockroachDB under-replicated and unavailable ranges, name the tables, alert Slack, fix impossible zone configs after approval and verify.

Categories
DataInfrastructure

CockroachDB splits a table into ranges and keeps each one replicated, three times by default. Lose a node and the ranges that had a replica on it carry one fewer copy until the cluster rebuilds them somewhere else. Lose enough copies and a range has no quorum, so reads and writes against that key span hang or error while the rest of the cluster serves normally. That is what makes it hard to notice. The cluster looks healthy and one table stops answering.

There is a worse version that never clears. A zone config asking for num_replicas = 5 on a three node cluster leaves its ranges under-replicated for as long as that config stands. The allocator has nowhere to put the fourth and fifth copy. Waiting does nothing. A dashboard that shows only a count of under-replicated ranges cannot tell that apart from a node that is three minutes into catching up.

This blueprint reads CockroachDB's own replication report and joins it to the zone configs and to node liveness. Then it says which of those two cases it is. It names the databases and tables behind the affected ranges. With approval, it lowers an impossible zone's num_replicas to the live node count, then reads a newer report and fails the run if the count did not fall.

This blueprint was created by zkasuran.

Findings

  • The replication report lives in system.replication_stats, keyed by zone_id and subzone_id. It is rebuilt every kv.replication_reports.interval, one minute by default. system.reports_meta carries the build time, so a run can tell how old the numbers it is reading are. There is no crdb_internal.replication_stats.
  • A table with no zone config of its own is counted under RANGE default, so the report alone never names it. The table names come from SHOW CLUSTER RANGES WITH TABLES, with the effective target resolved from the table zone, then the database zone, then RANGE default.
  • CockroachDB asks for 5 replicas on DATABASE system, RANGE meta, RANGE system and RANGE liveness. On a three node cluster that is 64 permanently under-replicated ranges out of the box, with no user mistake behind it. They are listed every run. They are left out of the remediation unless include_internal_zones is on, because lowering them reduces the durability of cluster metadata.

Safety

  • Every read is a catalog or report query. Nothing touches user data.
  • A zone is only changed when auto_remediate is true, when dry_run is false and when a human resumes the Pause.
  • The only statement the flow ever writes is ALTER <target> CONFIGURE ZONE USING num_replicas = <live node count>, built from crdb_internal.zones.target. It never drops a zone. It never raises a replica count. It never touches constraints or lease preferences.
  • CockroachDB internal zones are out of scope by default.
  • A report that was never built fails the run. So does one older than max_report_age_seconds. A quiet report never reads as a healthy cluster.

How it works

  1. cluster_before (io.kestra.plugin.jdbc.postgresql.Query, read-only) counts live, dead and leaving nodes from crdb_internal.gossip_nodes and crdb_internal.gossip_liveness. It sums the replication report. It counts the zones asking for more replicas than there are live nodes. It returns the report age.

  2. report_freshness (io.kestra.plugin.core.flow.If) fails the run when the report was never built or is older than max_report_age_seconds.

  3. audit returns one row per zone with a problem: the zone target, its num_replicas pulled out of full_config_sql, the range counts and a state.

    State Meaning Fix
    UNAVAILABLE Ranges in the zone have lost quorum. Queries on that key span hang or error Page. Bring the nodes back
    ZONE_IMPOSSIBLE num_replicas is higher than the live node count, so the target cannot be reached Lower num_replicas or add nodes
    UNDER_REPLICATED_TRANSIENT Below target while the target is reachable, so the allocator is rebuilding copies Wait, then re-check
    OVER_REPLICATED More replicas than the zone asks for, usually mid-rebalance Wait, then re-check
  4. affected_tables names the databases and tables whose ranges hold fewer voting replicas than their effective target, with range ids to check by hand.

  5. summary logs every zone and every table.

  6. route (io.kestra.plugin.core.flow.If):

    • clean: logs it.
    • findings: alert posts the states, the zones and the named tables to Slack.
      • remediate_gate runs only when auto_remediate is true, when dry_run is false and when at least one zone has a fix.
      • approve (io.kestra.plugin.core.flow.Pause) holds for up to an hour and records the change record you give it. behavior: FAIL means an ignored request fails loudly rather than changing a durability setting on its own.
      • remediate (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) runs one ALTER ... CONFIGURE ZONE at a time.
      • settle waits and cluster_after reads the report again. verify_result fails unless three things hold. The report must be newer than the one audited. No in scope zone may still ask for more replicas than there are live nodes. The under-replicated count must have fallen.
  7. errors alerts Slack when the audit itself cannot run.

What you get

  • A count of under-replicated ranges split into the two cases that need opposite responses: the one to wait out and the one that needs a config change.
  • The databases and tables behind the ranges, which the replication report does not carry.
  • An impossible zone config lowered to a target the cluster can actually reach, behind a human approval.
  • Before and after numbers from two different reports, so a green result is a measurement rather than an assumption.

Who it's for

  • Teams running CockroachDB who want under-replication triaged before a table stops answering.
  • Platform engineers after a node loss, a rolling upgrade or a region change, who need to know whether the cluster is still catching up or has stopped trying.
  • Anyone who has set num_replicas higher than the node count and watched the figure never come down.

Why orchestrate this with Kestra

Under-replication is a state, not an event. The thing that matters is how long it lasts. Kestra runs the same audit every fifteen minutes, keeps each run with its numbers and sends the zones and tables to Slack. The fix is a durability change, so it sits behind auto_remediate, dry_run and a Pause that records the change record. The run reports success only after a newer report says the count fell.

Tested end to end

On Kestra 2.0.5 OSS against a five node CockroachDB v24.1 cluster, with a local HTTP sink standing in for Slack. Each node was given its own locality so replicas spread. server.time_until_store_dead was raised to 20 minutes so a stopped node held the cluster in the under-replicated state long enough to run the flow against it.

Run Cluster state Result
1 5 live nodes, no zone overrides Clean. 0 unavailable, 0 under-replicated of 68 ranges
2 shop.orders set to num_replicas = 7 1 finding, ZONE_IMPOSSIBLE on TABLE shop.public.orders, 10 of 10 ranges under-replicated, 7 wanted against 5 live. shop.orders named with 10 ranges at 5 voting replicas. Alert sent, no change
3 same, auto_remediate: true, dry_run: true Same report, zone still at 7 when checked with cockroach sql
4 same, auto_remediate: true, dry_run: false, approval declined Pause held, resumed with approved: false, zone still at 7
5 same, auto_remediate: true, dry_run: false, approved ALTER TABLE shop.public.orders CONFIGURE ZONE USING num_replicas = 5 applied, under-replicated ranges 10 to 0, verified against a newer report and with SHOW ZONE CONFIGURATION
6 one node stopped, a second table pinned to it with num_replicas = 1 UNAVAILABLE and UNDER_REPLICATED_TRANSIENT and ZONE_IMPOSSIBLE reported together, with the pinned table named at 0 voting replicas
7 unreachable host errors alert sent

Prerequisites

  • A CockroachDB cluster reachable over the Postgres wire protocol, which is what the PostgreSQL plugin speaks to it.
  • The audit role needs VIEWCLUSTERMETADATA to read crdb_internal.gossip_nodes and the report tables. Remediation needs ZONECONFIG on the targets (or admin rights).
  • kv.replication_reports.interval must not be zero. At zero the report is never built and the run fails on report_freshness.
  • Local testing: a three node insecure cluster is enough to see ZONE_IMPOSSIBLE, since CockroachDB's own system zones ask for 5 replicas.

Secrets

  • COCKROACHDB_USERNAME / COCKROACHDB_PASSWORD: the SQL user for every query.
  • SLACK_WEBHOOK_URL: used by alert and the errors block.
  • In Kestra OSS, provide each one as a base64-encoded environment variable prefixed SECRET_.

Inputs

Input Default Purpose
jdbc_url jdbc:postgresql://localhost:26257/defaultdb?sslmode=require Any node of the cluster
include_internal_zones false Let the remediation lower CockroachDB's own system zones too
max_report_age_seconds 900 Fail rather than audit a report older than this
settle_duration PT150S Wait before the re-verify, longer than the report interval
auto_remediate false Allow ALTER ... CONFIGURE ZONE
dry_run true Must be false before any zone is changed

Quick start

  1. Add the COCKROACHDB_USERNAME, COCKROACHDB_PASSWORD and SLACK_WEBHOOK_URL secrets.
  2. Import the flow and set jdbc_url to one of your nodes.
  3. Run it once. Read the states before anything else, since UNDER_REPLICATED_TRANSIENT wants a second run a few minutes later rather than a config change.
  4. For a ZONE_IMPOSSIBLE zone, run with auto_remediate: true and dry_run: false, then resume the Pause with approved: true and your change record.
  5. Enable the every_15_minutes trigger, which always runs in safe mode.

How to extend

  • Alert on how long a zone has been UNDER_REPLICATED_TRANSIENT by storing the first-seen time in a KV key, so a rebuild that stalls is paged rather than waited on.
  • Read system.replication_critical_localities as well, to catch a range whose replicas all sit in one locality and would be lost together.
  • Propose the next lower odd replica count instead of the live node count, which keeps the Raft quorum odd on an even sized cluster.
  • Add a branch that decommissions a node that has been dead longer than a threshold, so the allocator stops counting it.
  • Loop over several clusters by passing a list of JDBC URLs.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.