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 / INSTALL AND RUN A CLUSTER

Install and run a cluster

This page gets the three programs a sharded cluster needs, then starts a cluster of three shards and one gateway on one machine and uses it. The same steps, with real host names, run a cluster across machines.

What you need

ProgramWhat it isWhere it comes from
rasterizeddbThe server. Each shard is one server process started with --role shard.rasterizeddb-server-<version>-<platform> archive
rasterizeddb-gatewayThe gateway clients connect to.rasterizeddb-gateway-<version>-<platform> archive
rdb-shardThe operator tool: identity rows, schema changes, moves, backups.cargo install rasterizeddb_client --bin rdb-shard

You also need openssl to make certificates and psql to try the cluster.

Get the binaries

The server and the gateway come as release archives on GitHub. Put both programs in one working directory:

sh
VERSION=1.1.0
BASE=https://github.com/milen-denev/rasterizeddb/releases/download/v$VERSION
mkdir -p ~/rdb-cluster && cd ~/rdb-cluster
curl -LO $BASE/rasterizeddb-server-$VERSION-linux-x86_64.tar.gz
curl -LO $BASE/rasterizeddb-gateway-$VERSION-linux-x86_64.tar.gz
curl -LO $BASE/SHA256SUMS
sha256sum -c --ignore-missing SHA256SUMS
tar xzf rasterizeddb-server-$VERSION-linux-x86_64.tar.gz
tar xzf rasterizeddb-gateway-$VERSION-linux-x86_64.tar.gz
cp rasterizeddb-server-$VERSION-linux-x86_64/rasterizeddb ./rasterizeddb
cp rasterizeddb-gateway-$VERSION-linux-x86_64/rasterizeddb-gateway ./rasterizeddb-gateway

Replace linux-x86_64 with linux-arm64, linux-x86_64-musl or linux-arm64-musl for the other builds. On Windows use the windows-x86_64.zip or windows-arm64.zip archives; the files end in .exe.

The rdb-shard tool is not in a release archive. It is a binary of the rasterizeddb_client crate; install it with Cargo (you need a Rust toolchain) and copy it beside the others:

sh
cargo install --root ~/.local rasterizeddb_client --bin rdb-shard
cp ~/.local/bin/rdb-shard ./rdb-shard
  • The release server is built with sharding (--role shard and the shard flags), two-phase commit across shards (sharding_2pc, needed for the gateway's two_phase = on) and per-shard Raft replicas (sharding_raft, see High availability).
  • The gateway archive has everything: client TLS, the native front end, fan-out, compressed links to the shards, and the configuration API.

Building from source

To build the three programs yourself instead:

sh
git clone https://github.com/milen-denev/rasterizeddb
cd rasterizeddb
git checkout v1.1.0
cargo build --release -p rasterizeddb_core --features sharding_raft,sharding_2pc
cargo build --release -p rasterizeddb_gateway
cargo build --release -p rasterizeddb_client --bin rdb-shard

The binaries land in target/release/ as rasterizeddb_core, rasterizeddb_gateway and rdb-shard. Copy them into ~/rdb-cluster as rasterizeddb, rasterizeddb-gateway and rdb-shard, the names the walkthrough uses.

A three-shard cluster on one machine

The walkthrough uses these ports:

ShardData directoryPostgreSQL portNative portrshard port (gateway links)
0./shard05440611807400
1./shard15441611817401
2./shard25442611827402

The gateway listens on 6432. Run every command from ~/rdb-cluster.

1. Certificates

The gateway talks to the shards over mutual TLS only: each side proves who it is with a certificate. One certificate authority (CA) signs a certificate for every shard and one for the gateway.

sh
mkdir -p tls && cd tls
openssl req -x509 -newkey rsa:2048 -nodes -days 365 -subj /CN=rdb-cluster-ca -keyout ca.key -out ca.pem
printf 'subjectAltName=DNS:localhost,IP:127.0.0.1\nbasicConstraints=CA:FALSE\nkeyUsage=digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth,clientAuth\n' > ext.cnf
for name in shard0 shard1 shard2 gateway; do
  openssl req -newkey rsa:2048 -nodes -subj /CN=$name -keyout $name.key -out $name.csr
  openssl x509 -req -in $name.csr -CA ca.pem -CAkey ca.key -CAcreateserial -days 365 -extfile ext.cnf -out $name.pem
done
cd ..
  • Every certificate is valid for both server and client use (serverAuth,clientAuth): shards also connect to each other.
  • The gateway checks a shard's certificate against the host in the shard's map address, so subjectAltName must name that host. On several machines, give each shard a certificate with its own host name or IP address.
  • Keep ca.key somewhere safe. Only the CA certificate (ca.pem) is handed to the programs.

2. The shard map

Pick a cluster id: 32 hexadecimal digits, unique to this cluster.

sh
openssl rand -hex 16

Write the map. This one splits the key space into three equal ranges:

sh
cat > cluster.map <<'EOF'
rasterizeddb shard map
format 1
cluster 0123456789abcdef0123456789abcdef
epoch 1
route_v 1
shard 0 127.0.0.1:7400
shard 1 127.0.0.1:7401
shard 2 127.0.0.1:7402
range 0x0 0
range 0x555555555555 1
range 0xaaaaaaaaaaaa 2
EOF

The map's lines:

LineMeaning
rasterizeddb shard mapMust be the first line.
format 1The file format. Optional; 1 is the only one.
cluster HEXThe cluster id. Every shard started with --cluster-id checks it. Required.
epoch NThe map's version, at least 1. Every change to the map must raise it. Required.
route_v 1The route function's version. Optional; 1 is the only one.
shard ID ADDRESS [STANDBY ...]A shard: its id and the address of its rshard listener (--shard-listen), which is where gateways connect. Extra addresses are recorded as standbys; the gateway does not use them.
range FIRST_BLOCK SHARDThe shard that owns blocks from FIRST_BLOCK up to the next range's first block. Blocks run from 0x0 to 0xffffffffffff (2^48 blocks). The first range must start at 0x0, and ranges must be in increasing order. A shard may own several ranges.
digest HEXOptional. A checksum of the content; a map whose digest does not match is refused. Delete it when you edit a map by hand.

# starts a comment. Use the same map file for every shard and every gateway.

3. Start the shards

sh
export RASTERIZEDDB_PG_PASSWORD='change-me'
for n in 0 1 2; do
  ./rasterizeddb --location ./shard$n --protocol both \
    --pg-port 544$n --rdb-port 6118$n \
    --role shard --cluster-map cluster.map --shard-id $n \
    --cluster-id 0123456789abcdef0123456789abcdef \
    --shard-listen 127.0.0.1:740$n \
    --shard-cert tls/shard$n.pem --shard-key tls/shard$n.key \
    --gateway-ca tls/ca.pem --peer-ca tls/ca.pem \
    --log-level info > shard$n.log 2>&1 &
done

What the flags do here:

  • --protocol both opens the PostgreSQL port (for you, with psql) and the native port (for rdb-shard).
  • --role shard --cluster-map cluster.map --shard-id N makes the server shard N of the map: it serves only the ranges the map gives it.
  • --cluster-id makes the server refuse a map of another cluster.
  • --shard-listen opens the port gateways connect to. --shard-cert and --shard-key are the certificate it presents there, and --gateway-ca admits only gateways whose certificate the CA signed.
  • --peer-ca is the CA the other shards' certificates are signed by. A shard connects to the other shards to combine aggregates, DISTINCT and sorted results.
  • RASTERIZEDDB_PG_PASSWORD is the password of the rasterized role each fresh shard creates. Use the same password on every shard: the gateway checks logins against the home shard (shard 0), and the session on every other shard logs in as the same role.

Configuration lists every shard flag.

A shard needs a data directory: it cannot run with --in-memory. It records the highest map epoch it has served in its data directory and refuses to start with an older map.

4. Create the schema on every shard

Every shard holds the whole schema. A table partitioned with PARTITION BY HASH (column) is sharded by that column; a plain table lives on one shard, the home shard.

sh
sleep 2
for n in 0 1 2; do
  psql "postgresql://rasterized:change-me@127.0.0.1:544$n/postgres?sslmode=require" \
    -c "CREATE TABLE orders (id INT PRIMARY KEY, store INT NOT NULL, v INT NOT NULL) PARTITION BY HASH (id)" \
    -c "CREATE TABLE stores (id INT PRIMARY KEY, name TEXT NOT NULL)"
done

The shards' PostgreSQL ports require TLS by default (--pg-tls require), hence sslmode=require.

Creating tables shard by shard is fine before anything uses them. On a running cluster, change the schema with rdb-shard ddl instead, which fences the tables while it works and checks that every shard ends with the same schema: see Sharded tables.

5. Write the identity rows

Each shard keeps an identity row naming its cluster and shard id. rdb-shard checks it before it moves data, so a wrong server behind an address is never written to. Write them once:

sh
export RDB_PASSWORD='change-me'
./rdb-shard init --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 0 (127.0.0.1:7400): identity written at epoch 1
shard 1 (127.0.0.1:7401): identity written at epoch 1
shard 2 (127.0.0.1:7402): identity written at epoch 1

rdb-shard speaks the native protocol. The map's addresses are the rshard listeners, which it does not speak, so --native ID=HOST:PORT tells it where each shard's native port is. --user logs in with the password in RDB_PASSWORD.

6. Start the gateway

sh
cat > gateway.conf <<'EOF'
listen = 0.0.0.0:6432
map = cluster.map
home_shard = 0
shard_trust = ca:tls/ca.pem
shard_cert = tls/gateway.pem
shard_key = tls/gateway.key
auth = scram
unrouted = fan_out
two_phase = on
admin = 127.0.0.1:9187
EOF
./rasterizeddb-gateway --config gateway.conf > gateway.log 2>&1 &
  • map is the same map file the shards read.
  • shard_trust = ca:tls/ca.pem checks every shard's certificate against the CA, and shard_cert/shard_key are the certificate the gateway presents to the shards.
  • unrouted = fan_out lets a read without a shard key run on every shard, with the results combined. The default, refuse, refuses it.
  • two_phase = on lets one transaction write on several shards, committed with two-phase commit.
  • admin opens the metrics endpoint on the loopback address.

The gateway's log starts with a line naming the cluster:

text
cluster 0123456789abcdef0123456789abcdef, map epoch 1, 3 shards, digest ...

Configuration lists every gateway setting.

7. Use it as one database

sh
psql "postgresql://rasterized:change-me@127.0.0.1:6432/postgres?sslmode=disable" \
  -c "INSERT INTO orders (id, store, v) VALUES (37, 2, 370)" \
  -c "INSERT INTO orders (id, store, v) VALUES (38, 1, 380), (39, 2, 390)" \
  -c "INSERT INTO stores (id, name) VALUES (1, 'north'), (2, 'south')" \
  -c "SELECT v FROM orders WHERE id = 37" \
  -c "SELECT count(*), sum(v) FROM orders" \
  -c "SELECT s.name, sum(o.v) FROM orders o JOIN stores s ON s.id = o.store GROUP BY s.name ORDER BY s.name"
text
INSERT 0 1
INSERT 0 2
INSERT 0 2
  v
-----
 370
(1 row)

 count | sum
-------+------
     3 | 1140
(1 row)

 name  | sum
-------+-----
 north | 380
 south | 760
(2 rows)
  • The first INSERT and the keyed SELECT go to the one shard that owns id = 37.
  • The two-row INSERT may put its rows on two shards; with two_phase = on the gateway splits it and commits both parts together.
  • stores is a plain table, so it lives on the home shard.
  • The count, sum and the join run across every shard.

The gateway has no TLS for clients in this file, hence sslmode=disable. To give clients TLS, see TLS between the pieces.

To see where rows went, ask a shard directly for the rows it owns. SET rdb.shard_part = on makes a keyless read on a shard count only its own ranges:

sh
for n in 0 1 2; do
  psql -At "postgresql://rasterized:change-me@127.0.0.1:544$n/postgres?sslmode=require" \
    -c "SET rdb.shard_part = on" -c "SELECT count(*) FROM orders"
done

8. Stop the cluster

Stop the gateway first, then the shards:

sh
kill $(pgrep -f '[r]asterizeddb-gateway --config gateway.conf')
kill $(pgrep -f '[r]asterizeddb --location ./shard')

On SIGTERM the gateway stops accepting connections, closes idle sessions at once and waits up to drain_secs (30 s) for open transactions to end.

Running across machines

The steps are the same. What changes:

  • Map addresses. Each shard line names the address gateways reach that shard's --shard-listen on, for example shard 0 db0.internal:7400. Start each shard with --shard-listen 0.0.0.0:7400 (or its own interface address).
  • Certificates. Each shard's certificate must name the host in its map address (subjectAltName=DNS:db0.internal).
  • Client ports. The shards' own PostgreSQL and native ports listen on 127.0.0.1 by default (--pg-addr, --rdb-addr). Applications only need the gateway. Open a shard's native port to the machine that runs rdb-shard, and its PostgreSQL port only to administrators.
  • The same map everywhere. Copy the map file to every shard and gateway. After a move, the new map must reach every machine before any shard or gateway restarts: see Moving data.
  • Several gateways. Gateways keep no data, so run as many as you like behind a TCP load balancer, each with the same map and its own gateway_name.

TLS between the pieces

LinkRequiredSet on the server sideSet on the client side
Client to gatewayNotls_cert, tls_key in the gatewaythe client's sslmode
Gateway to shard (rshard)Yes, mutual--shard-listen, --shard-cert, --shard-key, and --gateway-ca or --gateway-pin on the shardshard_cert, shard_key, shard_trust in the gateway
Shard to shardYes, mutualthe same --shard-cert/--shard-key--peer-ca on each shard (falls back to --gateway-ca)
rdb-shard to shardNative protocol TLSthe shard's --rdb-cert/--rdb-key (self-signed when not given)--ca, --pin or --accept-any (default) on rdb-shard

Client TLS on the gateway

text
tls_cert = /etc/rasterizeddb/gateway-public.pem
tls_key = /etc/rasterizeddb/gateway-public.key

With TLS set, the gateway answers a client's TLS request and offers SCRAM-SHA-256-PLUS (channel binding) as well as SCRAM-SHA-256. A client that does not ask for TLS can still connect without it: the gateway has no setting that requires TLS, so block plain connections at the network if you need to.

Pinning certificates instead of a CA

Instead of a CA, each side can accept exact certificates by their SHA-256 fingerprint:

sh
openssl x509 -in tls/gateway.pem -outform der | sha256sum | cut -d' ' -f1
  • On each shard: --gateway-pin <fingerprint>, once per gateway certificate.
  • In the gateway: shard_trust = pinned:<fingerprint>,<fingerprint>,..., one per shard certificate.

shard_trust = accept_any, the default, encrypts the gateway's links but does not check which server answers. Use ca: or pinned: in production.

Signed maps

A shard started with --authority-key accepts only maps signed by that key, so a map edited by anyone without the key is refused.

sh
./rdb-shard keygen --key authority.key
text
authority key written to authority.key; start every shard with --authority-key 3b6a...
sh
./rdb-shard sign --map cluster.map --key authority.key

sign writes cluster.map.sig beside the map. Start every shard with --authority-key <the printed hex>; a shard reads the signature from <map file>.sig. Every rdb-shard command that writes a map signs it when given --sign-key authority.key. Keep authority.key off the shards.

Limits

  • rdb-shard is not in any release archive; install it with Cargo.
  • The gateway cannot require TLS from clients.
  • The map file is not rewritten on shards or gateways when the cluster changes it. Copy the newest map to every machine yourself: see Moving data.
✓ page rendered in 236 µs axum + tera + htmx · no other javascript