How it works
Sharding and routing
Keys hash to 16,384 slots, slots map to shards in contiguous ranges, and a database's size and placement are fixed when it is created.
First key: slot 8000, shard 7. Second key: slot 8000, shard 7. Each shard owns 1,024 contiguous slots.
Same slot: any multi-key command, transaction or script over both keys works.
{hashtag} hashes only the part in braces, so related keys can share a slot.Slots and shards
A key hashes to one of 16,384 slots with CRC16, as in Valkey Cluster, and a {hashtag} hashes only the part in braces. Shard i of n owns a contiguous range of slots, so a shard could later split without remapping its neighbours. A 1-shard database has no cross-slot limits.
Shard count
A database is created on first use with the shard count of the SHARD_EPOCHS entry in force at that moment, or with an explicit count through the bench API (/db?db=<name>&shards=<n>).
[
{ "from": 0, "shards": 1 },
{ "from": 1800000000000, "shards": 16 }
]Append an epoch to size new databases. Never edit an existing one: its databases would route to the wrong shards.
Placement
With a regions list (regions=weur,enam, or regions=world for all nine), shard i is created with the location hint regions[i % n]. Without one, shards go to LOCATION_HINT if set, or near their first caller. The pub/sub shard always uses LOCATION_HINT.
A hint applies when an object is created; an existing shard keeps its place. Durable Objects hinted to sam, afr or me run in a nearby region instead. The dashboard shows where each shard actually runs.
How the router routes
| Command | Where it goes |
|---|---|
| Keys in one slot | That shard, unchanged and pipelined, or a nearby replica for reads |
| MGET, MSET, DEL, UNLINK, EXISTS, TOUCH | Split by shard, replies merged in key order |
| SCAN, KEYS, DBSIZE, FLUSHALL, INFO | Every shard, 32 at a time, after every earlier reply |
| SCRIPT LOAD, FUNCTION LOAD | Every shard, so EVALSHA and FCALL work wherever their keys land |
| SUBSCRIBE, PUBLISH | The database's pub/sub shard |
| AUTH, HELLO, SELECT, CLIENT, WAIT, WAITAOF, CONFIG | Answered by the router itself |
CROSSSLOT
Other multi-key commands with keys in different slots get CROSSSLOT, and so do MULTI, WATCH and Lua over keys in different slots. Put related keys under one {hashtag}, or use a 1-shard database. A transaction always runs on its shard's socket.
The commands the router answers itself are refused inside MULTI: queued, they would reach one shard's connection instead of the router.
Shard classes
Objects of one Durable Object class on one machine can share a process, and with it the memory limit and the single thread. Objects of different classes never do. So the shard is exported as eight classes, and shard i uses class i % 8: a database's shards 0 to 7 never share a process.
Replicas of class k live in class 8 + k, so a replica never shares a process with a primary. In tests, 16 shards in one class ran in 14 processes, one holding 3 shards; with 8 classes they ran in 16.
Connections
Each router opens a socket to every shard and replica it uses, which keeps per-shard order for free. Past 128 sockets in one Worker process (ROUTER_SOCKET_BUDGET), new upstreams send batches of commands as method calls instead, which cost more per batch: 200 connections over 16 shards measured about 20 to 26k ops/s.
Shard sockets idle for 30 s close, so an idle shard stops being billed and can sleep. The client's connection doesn't notice.
Unsupported commands
MONITOR; SYNC, PSYNC, REPLICAOF and FAILOVER (replication is internal); SHUTDOWN, DEBUG, MIGRATE, MOVE, SWAPDB, SAVE, BGSAVE, BGREWRITEAOF and ACL; CLIENT TRACKING and the other connection-wide CLIENT subcommands; SELECT above 0 (use another database name); CONFIG inside MULTI.