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
| Program | What it is | Where it comes from |
|---|---|---|
rasterizeddb | The server. Each shard is one server process started with --role shard. | rasterizeddb-server-<version>-<platform> archive |
rasterizeddb-gateway | The gateway clients connect to. | rasterizeddb-gateway-<version>-<platform> archive |
rdb-shard | The 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:
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-gatewayReplace 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:
cargo install --root ~/.local rasterizeddb_client --bin rdb-shard
cp ~/.local/bin/rdb-shard ./rdb-shard- The release server is built with sharding (
--role shardand the shard flags), two-phase commit across shards (sharding_2pc, needed for the gateway'stwo_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:
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-shardThe 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:
| Shard | Data directory | PostgreSQL port | Native port | rshard port (gateway links) |
|---|---|---|---|---|
| 0 | ./shard0 | 5440 | 61180 | 7400 |
| 1 | ./shard1 | 5441 | 61181 | 7401 |
| 2 | ./shard2 | 5442 | 61182 | 7402 |
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.
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
subjectAltNamemust name that host. On several machines, give each shard a certificate with its own host name or IP address. - Keep
ca.keysomewhere 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.
openssl rand -hex 16Write the map. This one splits the key space into three equal ranges:
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
EOFThe map's lines:
| Line | Meaning |
|---|---|
rasterizeddb shard map | Must be the first line. |
format 1 | The file format. Optional; 1 is the only one. |
cluster HEX | The cluster id. Every shard started with --cluster-id checks it. Required. |
epoch N | The map's version, at least 1. Every change to the map must raise it. Required. |
route_v 1 | The 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 SHARD | The 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 HEX | Optional. 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
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 &
doneWhat the flags do here:
--protocol bothopens the PostgreSQL port (for you, withpsql) and the native port (forrdb-shard).--role shard --cluster-map cluster.map --shard-id Nmakes the server shardNof the map: it serves only the ranges the map gives it.--cluster-idmakes the server refuse a map of another cluster.--shard-listenopens the port gateways connect to.--shard-certand--shard-keyare the certificate it presents there, and--gateway-caadmits only gateways whose certificate the CA signed.--peer-cais the CA the other shards' certificates are signed by. A shard connects to the other shards to combine aggregates,DISTINCTand sorted results.RASTERIZEDDB_PG_PASSWORDis the password of therasterizedrole 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.
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)"
doneThe 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:
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:61182shard 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 1rdb-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
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 &mapis the same map file the shards read.shard_trust = ca:tls/ca.pemchecks every shard's certificate against the CA, andshard_cert/shard_keyare the certificate the gateway presents to the shards.unrouted = fan_outlets a read without a shard key run on every shard, with the results combined. The default,refuse, refuses it.two_phase = onlets one transaction write on several shards, committed with two-phase commit.adminopens the metrics endpoint on the loopback address.
The gateway's log starts with a line naming the cluster:
cluster 0123456789abcdef0123456789abcdef, map epoch 1, 3 shards, digest ...Configuration lists every gateway setting.
7. Use it as one database
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"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
INSERTand the keyedSELECTgo to the one shard that ownsid = 37. - The two-row
INSERTmay put its rows on two shards; withtwo_phase = onthe gateway splits it and commits both parts together. storesis a plain table, so it lives on the home shard.- The
count,sumand 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:
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"
done8. Stop the cluster
Stop the gateway first, then the shards:
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
shardline names the address gateways reach that shard's--shard-listenon, for exampleshard 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.1by default (--pg-addr,--rdb-addr). Applications only need the gateway. Open a shard's native port to the machine that runsrdb-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
| Link | Required | Set on the server side | Set on the client side |
|---|---|---|---|
| Client to gateway | No | tls_cert, tls_key in the gateway | the client's sslmode |
| Gateway to shard (rshard) | Yes, mutual | --shard-listen, --shard-cert, --shard-key, and --gateway-ca or --gateway-pin on the shard | shard_cert, shard_key, shard_trust in the gateway |
| Shard to shard | Yes, mutual | the same --shard-cert/--shard-key | --peer-ca on each shard (falls back to --gateway-ca) |
rdb-shard to shard | Native protocol TLS | the 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
tls_cert = /etc/rasterizeddb/gateway-public.pem
tls_key = /etc/rasterizeddb/gateway-public.keyWith 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:
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.
./rdb-shard keygen --key authority.keyauthority key written to authority.key; start every shard with --authority-key 3b6a..../rdb-shard sign --map cluster.map --key authority.keysign 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-shardis 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.