Start here
Architecture
A router per connection, shard objects running Valkey with their AOF in SQLite, and memory-only replicas in other regions. What each part owns, and what it costs.
One database, end to endClick a box to read about itSwipe to explore
Selected pathOther callsResponsibilities, not machines
A box is a responsibility. Shards, replicas, the pub/sub shard and the __meta shard all run the same Durable Object code under different names. An arrow is a call path, not a statement about which machine runs it.
Where everything lives
| Part | Where | Holds |
|---|---|---|
| Router | In the Worker, wherever the connection lands | Per-connection state only |
| Shards | Durable Objects at the database's regions | Valkey's data, and its AOF in SQLite |
| Read replicas | One per shard per replica region | A copy of one shard, in memory |
| Rewriters | One per shard, created on its first rewrite | A copy of the AOF files during a rewrite |
| Pub/sub shard | One per database | Every channel of that database; messages aren't stored |
| __meta shard | One per server flavour | Nothing; it answers COMMAND for the routers' key tables |
| Catalog | One object | Each database's record |
| Watcher | One per watched database, near its first viewer | Five minutes of history, in memory |
| Pulse | One object | Each router process's open connections, in memory |
| Senders | One per region, only while test traffic runs | The traffic's database and deadline |
| Dashboard | The same Worker, loaded on the first page request | Nothing |
You don't run a server or a cluster manager. You still own the shard counts, the placement, the password, and what your clients do when a shard resets.
How clients reach it
- Owns
- Three ways in, all ending at the same router: raw TCP through a Spectrum application, RESP over the /ws WebSocket, or connect() from another Worker.
- Why
- A Worker's public URL takes HTTP, not raw TCP. Spectrum carries TCP to it, and the shim carries any client over a WebSocket.
- How it helps
- Stock clients work unchanged on every path. A service binding never leaves Cloudflare.
- Trade-off
- A WebSocket can't push back on its sender. A client that outruns its router by 16 MB is disconnected, and the shim acknowledges what it has read so the router stops past 8 MB unread.
Router
- Owns
- One connection: parsing, AUTH, the selected database, MULTI and WATCH state, and an upstream per shard and replica it has used.
- Why
- Clients expect one server. Redirecting between shards would need cluster-aware clients.
- How it helps
- Single-slot commands pass through unchanged and pipelined. Multi-key commands that are safe to split are split and merged in order; whole-database commands go to every shard.
- Trade-off
- One more hop than talking to a shard. Other multi-key commands across slots get CROSSSLOT.
Read replicas
- Owns
- A copy of one shard in one region, in memory only.
- Why
- A shard lives in one place, so a far client pays that distance on every read, and a primary's replies also wait for its commits.
- How it helps
- The primary sends its AOF files as a starting copy, then every committed append. Routers read from the replica in their region, which has no commits to wait for.
- Trade-off
- A read on another connection can be one replication lag old. Each replica's stream holds back the primary's writes.
Rewriters
- Owns
- One AOF rewrite at a time for one shard, in a Valkey of its own.
- Why
- Valkey compacts its AOF in a forked child. wasm has no fork(), so a rewrite inside the shard would block it.
- How it helps
- The rewriter loads a copy of the files, rewrites, and sends back a new base. The shard keeps serving and swaps it in.
- Trade-off
- Each rewrite copies the files to another object and the base back. After a rewriter fails, the shard rewrites inline for 10 minutes.
Catalog
- Owns
- Every database's name, creation time, shard count, regions, replica regions, flavour and password hash.
- Why
- Routing must be computed, not looked up per command, and a shard count must never change under existing keys.
- How it helps
- A record never changes after creation, so each Worker process fetches it once and keeps it.
- Trade-off
- No online resharding or moving. A different size or placement means a new database.
Watcher, Pulse and the dashboard
- Owns
- The Watcher keeps five minutes of history for one watched database. Pulse holds each router process's latest report of its open connections.
- Why
- Routers run in many places and share nothing, so something has to collect what the dashboard shows.
- How it helps
- The Watcher polls shards and ready replicas once a second while anyone has /live open, and stops when nobody does.
- Trade-off
- Both keep everything in memory, so a reset loses the history. Each watched database costs a poll of every shard and replica a second.
What the design buys, and costs
| Choice | Benefit | Cost |
|---|---|---|
| Real Valkey in wasm | Valkey's behaviour for every command | 64 MB per shard; memory never shrinks |
| AOF in SQLite, appendfsync always | Acknowledged writes survive resets and deploys | Each write waits for a storage commit |
| Group commit | Under load, one commit carries many writes | Input waits while two commits run |
| Rewrites in a helper object | The shard keeps serving during a rewrite | Files copied both ways |
| Replicas fed by the AOF | Nearby reads that don't wait for commits | Stale reads across connections |
| A router per connection | Stock clients, no cluster support | An extra hop; cross-slot limits |
| Eight shard classes | A database's first 8 shards never share memory | More bindings to keep |
| Size and placement fixed at creation | Routing is computed, with no directory | No resharding |
| Backpressure on every hop | Memory stays bounded under any load | Overload slows clients down |