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 / CONFIGURATION

Configuration

Every setting of the gateway, every flag that makes a server a shard, and the session settings clients can change through the gateway.

How the gateway reads its settings

sh
rasterizeddb-gateway --config /etc/rasterizeddb/gateway.conf
rasterizeddb-gateway --map cluster.map --listen 0.0.0.0:6432 --unrouted fan_out
rasterizeddb-gateway --config gateway.conf --workers=4
  • The configuration file has one key = value per line. # starts a comment; blank lines are ignored.
  • On the command line every key is a flag: --key value or --key=value. A flag overrides the same key in the file, wherever it stands on the line.
  • A dash and an underscore mean the same in a key: --home-shard 0 is home_shard = 0.
  • For a setting that takes an address or a file, off, none or an empty value turns it off.
  • An unknown key, or a value that does not parse, stops the gateway at start with a message naming the key.
  • --help prints the usage line.
  • Settings stored through the configuration API are applied at start over both the file and the flags.

The gateway logs to standard error. Set RUST_LOG to change the level; the default is info.

A minimal file:

text
listen = 0.0.0.0:6432
map = /etc/rasterizeddb/cluster.map
shard_trust = ca:/etc/rasterizeddb/tls/ca.pem
shard_cert = /etc/rasterizeddb/tls/gateway.pem
shard_key = /etc/rasterizeddb/tls/gateway.key

The gateway refuses to start when:

  • neither map nor map_from is set (unless relay is);
  • only one of tls_cert and tls_key, or only one of shard_cert and shard_key, is set;
  • native_listen is set without auth = trust and a native_role;
  • max_message_bytes is below 1024, or max_batch_bytes is below the smaller of max_message_bytes and 1 MiB;
  • api_listen is set without config_dir and api_token_file;
  • user_routing is not off in a build without per-user shards;
  • the map's epoch is lower than the epoch it has already routed with (see map).

Gateway settings

Listening and the map

KeyDefaultWhat it does
listen0.0.0.0:6432Comma-separated host:port addresses for PostgreSQL clients. Every worker binds each one (with SO_REUSEPORT on Linux).
workers0Worker threads. 0 means one per CPU core. A client stays on one worker for its whole connection.
mapnoneThe shard map file, in text or binary form. The highest map epoch the gateway has seen is kept in <map>.epoch beside it; a map older than that is refused at start. map_file is the same key.
map_fromnoneFetch the map from a shard at start instead of reading a file: the shard's rshard address, host:port. The highest epoch is then kept in <gateway_name>.epoch in the working directory.
home_shardthe lowest shard id in the mapWhere plain (unsharded) tables live, where logins are checked, where statements are classified, where session SETs go first, and where cross-shard aggregates are combined. Also the shard that records two-phase commit decisions.
gateway_namerasterizeddb_gatewayThe name the gateway gives the shards, also part of every transaction id it makes. Give each gateway its own name.

The gateway also learns newer maps by itself: when a shard reports a higher epoch, or refuses a statement because its range has moved, the gateway fetches the shard's map and uses it.

Client connections and authentication

KeyDefaultWhat it does
authscramscram: the gateway checks SCRAM-SHA-256 passwords against the role keys it reads from the home shard. trust: any role logs in without a password. Use trust only on a loopback address for tests.
auth_cache_secs60How long role keys read from the home shard are cached. CREATE/ALTER/DROP ROLE through the gateway clears the cache at once.
tls_cert, tls_keyoffPEM certificate chain and private key for client TLS. With them the gateway also offers SCRAM-SHA-256-PLUS. A client that does not ask for TLS still connects without it.
max_sessions100000Client connections at once. Past it a new connection gets 53300 sorry, too many clients already.
max_message_bytes67108864 (64 MiB)The largest single message a client may send. A larger one ends the connection with 08P01.
max_batch_bytes16777216 (16 MiB)The most a client may pipeline in extended-protocol messages before a Sync. A larger batch ends the connection with 54000.
server_version17.0The server_version clients are told.
drain_secs30On SIGTERM or SIGINT: how long to wait for open sessions before exiting.
KeyDefaultWhat it does
shard_trustaccept_anyHow shard certificates are checked: accept_any (encrypted, not verified), ca:<file> (signed by this CA, for the host in the map address) or pinned:<sha256>[,<sha256>...] (these exact certificates).
shard_cert, shard_keyoffThe certificate and key the gateway presents to the shards. Shards admit gateways by certificate only, so these are needed in practice.
shard_timeout_ms0A timeout for each request to a shard, in milliseconds. 0 means none: a statement may run as long as it needs.
max_in_flight4096Requests in flight on each worker's connection to one shard. At least 1.
retries3How many times a statement refused before it had any effect (its range moved, was fenced for a move, or the shard lost its session) is sent again without the client seeing it. At most 10. Only in autocommit, or for a transaction's first statement.
shard_compressionoffzstd (level 3) or zstd:LEVEL (-7 to 22, not 0): compress the links to the shards. Frames are compressed only when that makes them smaller. Worth it between machines; on one machine it only costs CPU. A shard too old to know the request leaves its link uncompressed and the gateway logs it.
shard_compression_min_bytes1024Frames shorter than this are sent uncompressed. At least 64.
session_pool0Idle shard sessions each worker keeps for its next clients. 0 keeps none. For applications that connect once per request (see below).
session_pool_idle_secs30How long an idle shard session is kept before it is closed. At least 1.

Session pool. Each client connection opens one session on each shard it uses, and closes them when it disconnects. With session_pool = N, a client's shard sessions are reset (RESET ROLE, DISCARD ALL) and kept for the next client of the same database, role and startup parameters on the same worker, which then logs in to no shard at all. Only a session left between statements and outside a transaction is kept. SHOW GATEWAY counts reuses in virtual_sessions_reused.

Statements without a shard key

KeyDefaultWhat it does
unroutedrefuseWhat a read without a shard key does: refuse (0A000 statement needs a route key in cluster mode) or fan_out (run on every shard, results combined). A session can change it with SET rdb.unrouted.
unrouted_writesrefuseOnly refuse is accepted here. A session turns keyless writes on with SET rdb.unrouted_writes = each_shard. Under two_phase = on with unrouted = fan_out, keyless writes run without it.
gatheronWhether a read that no single shard can answer (a join of sharded tables that names no key value, a subquery or CTE over a sharded table, a sharded table joined with a plain one) is gathered onto the home shard. on, true or 1 turn it on; any other value turns it off, and such reads are refused.
gather_max_bytes268435456 (256 MiB)The most a fan-out or a gather may collect. Past it the statement fails with 54000; results are never cut short.
broadcast_max_bytes16777216 (16 MiB)The largest plain table that is copied to every shard so a join with one sharded table can run on every shard. A larger table is gathered instead. 0 never copies.
template_cache_per_core4096Statement shapes each worker remembers the routing of. At least 16.
template_cache_shared65536The shared cache behind the per-worker ones. At least 16.

Transactions across shards

KeyDefaultWhat it does
two_phaseoffon: a transaction may write on several shards, and commits on all of them with two-phase commit. Multi-row INSERTs and COPYs that span shards are split and committed atomically. off: a transaction writes on one shard only. Needs shards built with the sharding_2pc feature.
two_phase_lock_timeout_ms5000The lock_timeout every part of a transaction runs under when the gateway coordinates several shards, so a lock wait across shards ends in an error instead of hanging.
deadlock_check_ms1000How often the gateway looks for deadlocks across shards between two-phase transactions, and ends the youngest transaction in a cycle with 40P01. 0 turns the check off. Only with two_phase = on.

Other front ends

KeyDefaultWhat it does
native_listenoffhost:port for native-protocol clients, routed by a route= option (see Routing and queries). Needs auth = trust and native_role. Uses tls_cert/tls_key when set.
native_rolenoneThe role every native request runs as.
native_databasenoneThe database every native request runs in.
kv_listenoffhost:port for Redis-protocol clients, each command sent to the shard its key hashes to.
kv_shardsnoneEach shard's Redis address, ID=HOST:PORT[,ID=HOST:PORT...]: the port each shard opens with --kv-port.

Administration

KeyDefaultWhat it does
adminoffhost:port of the metrics endpoint: GET /metrics in Prometheus format, any other path the SHOW GATEWAY rows as text. No authentication: bind it to a private address.
api_listenoffhost:port of the HTTP configuration API. Needs config_dir and api_token_file. No TLS of its own.
api_token_filenoneA file whose first line is the API's bearer token, at least 16 characters.
config_dirnoneA directory for the API's local store of settings and its audit log (a small embedded database). Also where rebalance runs keep their state.
rebalance_toolnonePath to the rdb-shard binary. Turns on REBALANCE CLUSTER (see Moving data).
rebalance_rolesnoneRoles allowed to run the rebalance statements, comma- or space-separated.
rebalance_usernoneThe role rdb-shard logs in to the shards as.
rebalance_password_envRDB_PASSWORDThe environment variable of the gateway's process that holds rebalance_user's password.
rebalance_nativenoneEach shard's native address, ID=HOST:PORT[, ...], when the map's addresses are rshard listeners.
rebalance_extranoneMore rdb-shard flags, space-separated, for example --database postgres --ca /etc/rasterizeddb/tls/ca.pem.
rebalance_tolerance0.10How far from the mean a shard's load may be before the planner moves ranges: a fraction between 0 and 1.
rebalance_max_moves4The most range moves one run makes. At least 1.

Testing and special modes

KeyDefaultWhat it does
user_routingoffPer-user shards: off, prefer or require. Needs the enterprise gateway; see Per-user shards.
fixed_shardoffSend every session to this one shard id, with no routing. For measuring the gateway's own cost.
relayofftcp:<host:port>: relay bytes to one address with no parsing at all, and no map. For measuring a network hop.

Shard flags

These are server (rasterizeddb) flags. Any of --role, --cluster-map, --shard-id, --cluster-id or --shard-listen needs --role shard.

FlagDefaultWhat it does
--role shardoffRun as a shard: serve only the ranges the map gives --shard-id, refusing reads and writes anywhere else. shard is the only role.
--cluster-map FILEnoneThe shard map, text or binary. Required with --role shard. A signature is read from FILE.sig when it exists.
--shard-id NnoneThis shard's id in the map. Required. An id the map does not list starts as a joining shard that owns nothing until a map naming it is installed.
--cluster-id HEXnoneWhen given, the map must name this cluster (32 hex digits).
--shard-listen ADDR:PORTclosedWhere gateways connect (rshard, mutual TLS). Needs --shard-cert, --shard-key and --gateway-ca or --gateway-pin. A shard whose listener fails to open exits.
--shard-cert PEMnoneThe certificate chain this shard presents to gateways and to other shards.
--shard-key PEMnoneIts private key.
--gateway-ca PEMnoneAdmit gateways whose certificate this CA signed.
--gateway-pin SHA256noneAdmit the gateway certificate with this SHA-256 fingerprint. Repeat it for each gateway.
--peer-ca PEM--gateway-caThe CA other shards' certificates are signed by, for the links a shard opens to its peers to combine aggregates, DISTINCT and sorted results.
--shard-peer-compression MODEoffCompress this shard's links to other shards: off, zstd or zstd:LEVEL.
--authority-key HEXnoneThe Ed25519 public key (64 hex digits) maps must be signed with. See Signed maps.
--raft-id NnoneThis replica's id in its shard's Raft group. Needs a build with sharding_raft. See High availability.
--raft-listen ADDR:PORTnoneWhere the group's other replicas reach this one.
--raft-peer ID=ADDR:PORTnoneAnother replica of the group. Repeatable.

A shard also needs:

  • a data directory (--location): it cannot run --in-memory;
  • the write-ahead log, which is on by default;
  • --json-shadow-tables left off: a shard refuses it.

A shard's data directory is marked as a shard's. A server built without the sharding feature refuses to open it, and refuses the shard flags with the reason.

Every other server flag works on a shard as on any server: ports, TLS for its own listeners, backups (--s3-*), memory and storage settings. See the server configuration.

Settings a shard keeps at run time

SettingSet withWhat it does
shard.schema_epochSET CONFIG shard.schema_epoch = NThe schema version the shard classifies statements under. It never goes down. rdb-shard ddl raises it on every shard after a schema change.
shard.ddl_fenceSET CONFIG shard.ddl_fence = 'orders,items'Tables a schema change is working on. Statements touching them are refused with a retryable 40001 until it is set back to ''. rdb-shard ddl sets and clears it.

Both are run on a shard directly, not through the gateway.

Session settings through the gateway

A client changes these with SET on its gateway connection. The gateway answers them itself.

SettingValuesWhat it does
rdb.route_keyan integer, 'i:42', 'u:<uuid>', 't:<text>', or any quoted textSend every following statement to the shard that owns this key, without looking at the SQL. SET LOCAL sets it for the current transaction only. RESET rdb.route_key or SET rdb.route_key TO DEFAULT clears it.
rdb.unroutedrefuse, fan_outThis session's unrouted.
rdb.unrouted_writesrefuse, each_shardLets a write without a key run on every shard. Without two-phase commit each shard's part commits on its own.
rdb.consistent_fanouton, off (default)Fan-out reads take one snapshot across the shards, so a transaction that committed on several shards is seen wholly or not at all.
rdb.split_bulk_writeson, off (default)Without two_phase, lets a COPY into a sharded table be split by shard in autocommit, each shard's part committed on its own.

SHOW rdb.route_key, SHOW rdb.unrouted and SHOW rdb.unrouted_writes show the current values. SHOW GATEWAY lists the gateway's state and counters (see Administration).

Any other SET at session level runs on the home shard and on every shard the session has open, and is replayed on each shard the session reaches later.

Limits

  • Settings are read once, at start. Change the file (or the API's store) and restart the gateway.
  • The metrics endpoint has no authentication, and neither it nor the configuration API has TLS.
  • native_listen needs auth = trust, which then applies to PostgreSQL clients too.
✓ page rendered in 155 µs axum + tera + htmx · no other javascript