NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / SHARDING GATEWAY / HIGH AVAILABILITY

High availability

What a sharded cluster does when a shard, the home shard or a gateway goes down, how to finish transactions a failure left half done, and what per-shard replication offers in this version.

When a shard is down

Each shard holds its rows and nobody else's, so a shard that is down takes exactly its rows out of service:

  • Statements for its keys fail at once with 08006: the gateway could not reach the shard, and nothing was sent. Statements for keys on other shards keep working.
  • Statements that need every shard (fan-outs, gathers, keyless writes, cluster-wide SHOWs) fail while any shard is down.
  • A statement in flight when the connection to its shard is lost fails with 08007: it was sent, so its outcome is unknown, and the gateway does not retry it. Inside a transaction block the client's connection is closed too, since the transaction's state is unknown.
  • The gateway keeps trying. It marks the shard down and reconnects with a back-off that starts at 100 ms and doubles up to 5 s. When the shard is back, statements for its keys work again; nothing needs to be done on the gateway.

When the shard restarts on its own data directory, its rows are all there: a shard is an ordinary RasterizedDB server with a write-ahead log.

When the home shard is down

The home shard (home_shard, by default the lowest shard id) does more than hold its own rows, so while it is down:

  • Logins fail with 08006, except for roles whose keys the gateway cached in the last auth_cache_secs (60 s).
  • New statement shapes cannot be classified and fail with 08006. Shapes the gateway has already seen keep routing to the other shards.
  • Plain tables, which live on the home shard, are unavailable.
  • Aggregates, DISTINCT and top-N across shards, combined on the home shard, fail.
  • Two-phase commits cannot record their decision, and roll back with 40001.

Choose a home shard on your most reliable machine, and keep plain tables small.

When a gateway is down

A gateway keeps no data. Its only file is <map>.epoch, the highest map epoch it has seen.

  • Run two or more gateways behind a TCP load balancer, each with the same map and its own gateway_name. When one stops, its clients' connections drop; they reconnect to another.
  • A gateway's sessions end with it. Open transactions on the shards are rolled back as their sessions close, except two-phase transactions caught between prepare and commit, which stay prepared (below).
  • A gateway restarted with an older map than it has used refuses to start. Give it the newest map, or start it with map_from to fetch the map from a shard.

Transactions left prepared

With two_phase = on, a transaction that wrote on several shards commits in two steps: every shard prepares, the decision is recorded on the home shard, then every shard commits. If a gateway or a shard fails in between, some shards hold the transaction prepared: its rows stay locked and its outcome is not yet visible there. The gateway logs:

text
transaction rdb2pc:... is decided commit but shard 1 did not finish it (...); rdb-shard resolve completes it

Run rdb-shard resolve to finish every such transaction from the recorded decision:

sh
export RDB_PASSWORD='change-me'
./rdb-shard resolve --map cluster.map --user rasterized --database postgres \
  --native 0=127.0.0.1:61180 --native 1=127.0.0.1:61181 --native 2=127.0.0.1:61182
text
shard 1: rdb2pc:rasterizeddb_gateway:18797c5f3a2b4c10:7 committed
  • A transaction the home shard recorded as committed is committed on every shard that still holds it prepared; one recorded as aborted is rolled back.
  • A transaction with no decision that is older than --grace seconds (300 by default) is recorded as aborted on the home shard first, so a late gateway cannot commit it, then rolled back. Younger ones are left for their gateway to finish.
  • Prepared transactions that no gateway made (their id does not start with rdb2pc:) are left alone.
  • --home SHARD names the shard that records decisions, when the gateways use a home_shard other than the lowest id.

Run it after any gateway or shard crash while two-phase transactions were in flight, and after restoring a cluster backup (with --grace 0).

Backups

Each shard backs itself up to S3 like any server, with its own --s3-* flags and its own prefix. rdb-shard backup takes a backup of the whole cluster at one consistent point: see Administration.

Per-shard replicas (Raft)

A server built with the sharding_raft feature can run each shard as a group of replicas that agree on every commit through Raft: a commit is visible only once a majority of the group holds it, and a new leader is elected when the leader fails.

FlagWhat it does
--raft-id NThis replica's id in its shard's group: unique in the group, not 0. Needs --role shard, --raft-listen, --shard-cert, --shard-key, and --peer-ca (or --gateway-ca).
--raft-listen ADDR:PORTWhere the group's other replicas reach this one, over mutual TLS.
--raft-peer ID=ADDR:PORTAnother replica of the same group. Repeat it for each. The group's first membership is this replica and every peer named, all voting.

Every replica of a shard runs with the same --shard-id and its own data directory; the group's log is kept in a raft directory inside it. The leader serves the group; a follower refuses what it cannot serve with 57P03 and names the leader.

Treat this as a preview, not as high availability for production:

  • The gateway connects to each shard only at the address in the map. It does not follow a change of leader, and it does not use the map's standby addresses.
  • This version has no supported procedure for moving clients to a new leader after a failover.
  • The cluster's end-to-end tests do not cover Raft groups.

Until this is finished, protect each shard as you would a single server: reliable storage, continuous S3 backups, and a tested restore.

Limits

  • One shard down makes its keys, and every statement that needs all shards, unavailable until it is back.
  • The home shard is needed for logins (after the cache expires), for new statement shapes, for plain tables, for cross-shard aggregates and for two-phase commits.
  • Two-phase transactions caught by a failure stay prepared until rdb-shard resolve runs.
  • Per-shard Raft replicas are a preview: the gateway does not fail over to a new leader.
✓ page rendered in 148 µs axum + tera + htmx · no other javascript