Sharding and the gateway
Sharding spreads one database over several RasterizedDB servers, called shards, so that no single machine has to hold every row or carry every write. The sharding gateway sits in front of the shards and speaks the PostgreSQL protocol, so applications connect to it as they would to one database.
When to shard
Shard when one server is no longer enough and your data splits cleanly by a key:
- most statements name one key value: a tenant, a store, an account, a customer;
- the rows for one key value are small enough to live on one machine;
- clients speak PostgreSQL (any language, any driver), or you are happy to route in your own Rust code.
Do not shard to make a small database faster. Every statement through the gateway costs one more network hop. A statement without a key has to visit every shard, and joins across shards copy rows between machines.
If your only client is a Rust program using rasterizeddb_client, you can also shard without a gateway: see client-side sharding. For a cluster where each login is locked to its own shard, see per-user shards.
The pieces
PostgreSQL clients (psql, drivers, ORMs)
|
v
+-----------------------+
| rasterizeddb-gateway | one or more, stateless
+-----------------------+
| | | mutual TLS (rshard)
v v v
+-------+ +-------+ +-------+
|shard 0| |shard 1| |shard 2| rasterizeddb --role shard
+-------+ +-------+ +-------+- Shards. Each shard is an ordinary RasterizedDB server started with
--role shardand the shard flags. It holds the whole schema, but only the rows whose key falls in its share of the key space. It refuses any read or write outside that share, so a wrong route never returns or stores rows in the wrong place. - The shard map. A small text file that every shard and every gateway reads. It names the cluster, lists the shards and their addresses, and gives each shard one or more ranges of the key space. It carries an epoch that goes up with every change.
- The route function. A table that is partitioned with
PARTITION BY HASH (column)is a sharded table, and that column is its shard key. A key value hashes to one block of a 2^48-block space, and the map says which shard owns that block. The same value of the same kind of key (integer, text or UUID) lands on the same shard in every table, so all of a tenant's rows live together. - The gateway. A separate program,
rasterizeddb-gateway. For each statement it finds the shard key value, works out which shard owns it, and passes the client's bytes to a session on that shard. It does not run SQL itself: the shards parse, plan and run every statement. A statement without a key can be sent to every shard and the results combined. - The
rdb-shardtool. A command-line tool for operators: it applies schema changes to every shard, moves tenants and ranges between shards, splits a shard, and takes cluster backups.
What clients see
- One PostgreSQL server at the gateway's address (port 6432 by default). Clients log in with the same roles and passwords the shards hold, using SCRAM-SHA-256.
- A statement that names a key value (
WHERE id = 37, orINSERT ... VALUES (37, ...)) runs on one shard, with that shard's normal behaviour. - A statement without a key is refused with
0A000 statement needs a route key in cluster mode, unless the gateway is set to fan such statements out. With fan-out, counts, sums,GROUP BY,DISTINCT,ORDER BY ... LIMITand joins are answered across every shard. - With two-phase commit turned on in the gateway, a transaction can write on several shards and commits on all of them or none.
- Schema changes do not go through the gateway. You apply them to every shard with
rdb-shard ddl. Role and privilege statements (CREATE ROLE,GRANT, ...) do go through the gateway, which applies them on every shard.
Routing and queries lists exactly what routes, what fans out and what is refused.
Where to go next
- Install and run: build the binaries and start a three-shard cluster with a gateway.
- Sharded tables: choose a shard key and declare sharded, global and reference tables.
- Routing and queries: what each statement does through the gateway.
- Configuration: every gateway setting and every shard flag.
- Moving data: move tenants, rebalance, add a shard.
- High availability: what happens when a shard is down.
- Administration: metrics, the configuration API, backups, upgrades.
- Limits: what the gateway does not do.