Multi-Raft, Sharding & CCR

Hash/range sharding, online split/rebalance, 2PC, Cross-Cluster Search (CCS), CCR, and DR workflows.

2 min read Clustering & Operations

Scale SpydDB horizontally with Multi-Raft consensus groups, hash/range sharding, online shard splitting & rebalancing, distributed two-phase commit (2PC), Cross-Cluster Search (CCS), Cross-Cluster Replication (CCR), and cross-site Disaster Recovery workflows.

1. Configuring a Multi-Node Raft Cluster#

Configure the raft block in server.json on each voter node using mutual-TLS peer transport and optional SHA-256 certificate pinning:

json
{
  "raft": {
    "id": 1,
    "cluster_id": "prod-us-east-1",
    "voters": [1, 2, 3],
    "peer_bind": "10.0.1.11:7000",
    "certificate": "/etc/spyd/tls/node1.crt",
    "private_key": "/etc/spyd/tls/node1.key",
    "client_ca": "/etc/spyd/tls/ca.crt",
    "tick_millis": 100,
    "request_timeout_millis": 5000,
    "peers": [
      { "id": 1, "address": "10.0.1.11:7000", "server_name": "node1.spyd.internal", "certificate_pin": "sha256:..." },
      { "id": 2, "address": "10.0.1.12:7000", "server_name": "node2.spyd.internal", "certificate_pin": "sha256:..." },
      { "id": 3, "address": "10.0.1.13:7000", "server_name": "node3.spyd.internal", "certificate_pin": "sha256:..." }
    ]
  }
}

Managing Dynamic Raft Groups (GET/POST /v1/cluster/raft/groups)#

sh
# List per-shard Multi-Raft groups, terms, leaders, and committed indices
curl -fsS --cacert tls/ca.crt https://localhost:8080/v1/cluster/raft/groups \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN"

# Transfer leadership cleanly before node maintenance
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/cluster/raft/groups \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"action": "transfer_leadership", "group_id": 1, "node_id": 2}'

Supported Raft group actions: "add_voter", "remove_voter", "transfer_leadership", "campaign".

2. Cluster Nodes & Hash/Range Sharding#

sh
# 1. Register nodes in the cluster topology
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/cluster/nodes \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "node_id": "node-2",
    "http_url": "https://node2.spyd.internal:8080",
    "wire_addr": "node2.spyd.internal:5433",
    "role": "voter",
    "zone": "us-east-1b"
  }'

# 2. Configure hash or range sharding on a collection
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/cluster/shards \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "collection": "orders",
    "shard_key": "customer_id",
    "strategy": "hash",
    "shard_count": 8,
    "replication_factor": 3
  }'

# 3. Online zero-downtime shard split or rebalance
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/cluster/shards/rebalance \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"collection": "orders", "shard_id": 3, "to_node": "node-2"}'

Inspect cluster generation, coordinator leader, active migrations, nodes, shards, and remote links via GET /v1/cluster/topology.

3. Distributed Reads, Writes, Raft Snapshots & Cross-Shard Coordination#

  • Distributed Inserts (POST /v1/cluster/insert): Routes each row automatically by shard_key, supports configurable write_concern ("one", "quorum", "all"), honors Idempotency-Key deduplication, and executes cross-shard atomic batches using two-phase commit when "atomic": true.
  • Scatter/Gather Queries (POST /v1/cluster/query): Prunes candidate shards via predicate analysis (==, in, <, <=, >, >=, between), executes partial aggregates (count, sum, min, max, weighted avg) in parallel across shards, and performs global sort / skip / take.
  • Interactive Distributed 2PC (POST /v1/cluster/tx/{begin,query,stage,end} & {prepare,commit,abort,forget,recover}): Durable replicated coordinator log enabling interactive multi-statement cross-shard transactions, atomic participant commits, and deterministic standby recovery.
  • Regional Leases, Distributed Row Locks & HLC: Manage authoritative regional leases (/v1/cluster/leases/*), distributed row locks (/v1/cluster/locks/*), and Hybrid Logical Clock synchronization (/v1/cluster/hlc/*).
  • Raft Chunked Snapshots (SDBSNAP1) & Automatic Log Compaction: Multi-Raft followers that fall behind the compacted WAL window automatically receive checksummed chunked snapshots over the Raft wire transport while leaders compact applied Raft logs on bounded budgets.
  • Cross-Cluster POST Leader Chasing: Mutating requests forwarded to a follower or remote cluster link automatically chase the current leader hint (isNotLeader) instead of failing immediately on transient leadership changes.

4. Cross-Cluster Search (CCS) & Cross-Cluster Replication (CCR)#

Link remote clusters (with up to 16 HA seed URLs, TLS/CA verification or leaf certificate pinning, auth modes, and optional security synchronization) for global search and asynchronous/continuous bidirectional replication:

sh
# 1. Define a remote cluster link (optional: telemetry_token, auth_mode, environment, ca_cert_pem, certificate_pin, pin_only, sync_security, inbound_user)
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/cluster/links \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "eu-west",
    "seed_urls": ["https://eu-1.spyd.internal:8080", "https://eu-2.spyd.internal:8080"],
    "auth_mode": "caller_token",
    "environment": "production",
    "telemetry_token": "'"$EU_MONITOR_TOKEN"'",
    "sync_security": true
  }'

# 2. Query remote cluster collections (inline cluster prefix or `clusters` array)
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/cluster/query \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "sql": "orders | where status == \"paid\" | agg count() as n, sum(total) as rev",
    "clusters": ["local", "eu-west"]
  }'

# 3. Create Continuous Cross-Cluster Replication (CCR) subscription
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/cluster/ccr \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "replicate-orders-from-eu",
    "link_name": "eu-west",
    "remote_collection": "orders",
    "local_collection": "orders_eu_replica",
    "bootstrap_existing": true,
    "auto_create_schema": true,
    "conflict_policy": "lww"
  }'

Inspect conflict logs via GET /v1/cluster/ccr/conflicts, manually sync with POST /v1/cluster/ccr/sync, and execute non-disruptive Disaster Recovery validation drills via POST /v1/cluster/dr/drill, failover, and failback.

5. Multi-Cluster Fleet Monitoring & Security Sync#

Once clusters are linked, a hub node can monitor up to 10,000 outbound links and 4,096 reverse-dial NAT/firewalled clusters (fleet_publish), browse any dial-out link safely through X-Spyd-Cluster, and prevent RBAC/policy divergence across shards and linked clusters via X-Spyd-Security and opt-in security sync (GET/PUT /v1/cluster/security/sync).

Read the full operational guide in Multi-Cluster Fleet & Security Sync.