dkv is a distributed, replicated key-value store written in Rust. This document is the design spec: what the system does, how it's built, and the order it will be built in.
Status: Early development.
Cache(an in-memory store) and a TCP server that answersPINGexist today. Everything else in this document is planned work.
- Simple data model: string keys mapped to string values, with
GET,SET,DEL,EXISTSand a few admin commands. - Durability: writes that have been acknowledged survive a process crash or restart.
- High availability: a cluster of
2f + 1nodes keeps serving reads and writes while up tofnodes are down. - Strong consistency: writes are linearizable, and reads are linearizable by default.
- Horizontal scale: the keyspace is split into shards that are spread across nodes.
- Learning-friendly codebase: each layer (storage, networking, consensus, sharding) is a separate module with its own tests.
- Transactions that span multiple keys.
- Secondary indexes, range queries or rich data types (lists, hashes, sets).
- Replication across geographic regions.
- Authentication and TLS. These are planned after v1 (see §10).
- Wire compatibility with Redis. The port matches Redis's, but the protocol is our own.
| Term | Meaning |
|---|---|
| Node | One dkv server process. |
| Cluster | A set of nodes that together serve the whole keyspace. |
| Shard | A contiguous range of the hash space, replicated as a unit. |
| Replica group | The nodes that hold copies of one shard. Each group runs its own Raft instance. |
| Leader | The replica that accepts writes for a shard in the current Raft term. |
| Follower | A replica that copies the leader's log. |
| WAL | Write-ahead log, the append-only on-disk record of every applied command. |
- Key: a UTF-8 string of 1 to 512 bytes. It must not contain whitespace or newlines (a protocol limit; see §5).
- Value: a UTF-8 string of 0 to 1 MiB.
- Versioning: every key has a monotonically increasing
u64version, which is set from the Raft log index of the write that last changed it. This makes compare-and-set possible later (§10). - TTL (planned, Milestone 2): an optional expiry time for each key. Expired keys can no longer be read and are removed lazily on access and by a background sweep.
┌──────────────────────────────────────────────┐
client ──▶ │ Network layer (tokio TCP, protocol codec) │
├──────────────────────────────────────────────┤
│ Router (key → shard → leader) │
├──────────────────────────────────────────────┤
│ Consensus (Raft per shard) │
├──────────────────────────────────────────────┤
│ State machine (Cache: HashMap<K, V>) │
├──────────────────────────────────────────────┤
│ Storage (WAL + snapshots on disk) │
└──────────────────────────────────────────────┘
src/
lib.rs # re-exports
cache.rs # in-memory state machine (today's `Cache`)
protocol/ # command parsing + response encoding
server/ # TCP listener, connection handling
storage/ # WAL, snapshots, recovery
raft/ # consensus integration
cluster/ # membership, shard map, routing
config.rs # node configuration
bin/
server.rs # `dkv-server`
cli.rs # `dkv-cli` client
- One tokio task for each client connection. Connections must never be handled one after another. Today's
server.rsawaits each connection inline, which blocks every other client. - The state machine sits behind a single owner task that receives work over an
mpscchannel, soCacheitself has no locks. The connection task sends a command together with aoneshotsender for the reply. - A
RwLock<Cache>is an acceptable stepping stone for Milestone 1, before Raft exists.
The protocol is text-based and line-oriented, so it can be driven by hand with nc or telnet.
- A request is a single line ending in
\n(an optional\rbefore it is ignored). - Tokens are separated by single spaces. The first token is the command name, which is case-insensitive.
- For
SET, everything after the key is the value. The value may contain spaces but not newlines. - A response is a single line ending in
\n. - One connection carries many requests. The server reads line by line and must not read to EOF (today's
read_to_stringdoes).
| Command | Response | Notes |
|---|---|---|
PING |
PONG |
Health check. |
GET <key> |
VALUE <value> or NIL |
|
SET <key> <value> |
OK |
Creates or overwrites the key. |
DEL <key> |
1 or 0 |
Whether the key existed. |
EXISTS <key> |
1 or 0 |
|
LEN |
<n> |
Number of keys on this node or shard. |
CLEAR |
OK |
Admin only. Disabled in cluster mode for v1. |
EXPIRE <key> <secs> |
1 or 0 |
Milestone 2. |
CLUSTER INFO |
multiline (see below) | Milestone 4. Returns the shard map. |
Every error response has the form ERR <code> <message>:
| Code | Meaning |
|---|---|
UNKNOWN |
Unknown command. |
ARGS |
Wrong number or format of arguments. |
TOOLARGE |
The key or value exceeds its size limit. |
MOVED <shard> <addr> |
The key belongs to a shard led by another node. The client should retry at <addr>. |
NOLEADER |
No leader has been elected yet. The client should retry with backoff. |
INTERNAL |
Unexpected server error. |
A multiline response (only CLUSTER INFO) starts with ARRAY <n>, followed by n lines.
If the text protocol becomes a bottleneck, or binary-safe values are needed, a length-prefixed binary framing (or gRPC through tonic) can be added on a separate port. The text protocol stays available for debugging.
- Each mutating command is appended to
data/<shard>/wal-<segment>.logbefore it is applied toCache. - Each record has the layout
[len: u32][crc32: u32][payload], where the payload is the serialized command (bincodeor a hand-written encoding). - A segment rolls over to a new file at 64 MiB.
- Fsync policy is configurable:
always: fsync before acknowledging. The safest and slowest option.batch(default): group commit every 10 ms or every N writes.never: rely on the OS. Only for tests and benchmarks.
- A snapshot is the full
Cacheserialized tosnapshot-<last_index>.bin, written to a temporary file and then atomically renamed into place. - Snapshots are taken when the WAL grows past a threshold (default 256 MiB). Older WAL segments are then deleted.
- Once Raft exists, these same snapshots are what Raft sends to followers that have fallen far behind (
InstallSnapshot).
- Load the newest valid snapshot.
- Replay the WAL records whose index is greater than the snapshot's index.
- Stop at the first record that is truncated or fails its CRC, and truncate the file at that point. This is a torn write from a crash.
Replication uses Raft. The preferred implementation is the openraft crate rather than a from-scratch Raft. A hand-written implementation is a possible learning stretch goal, behind the same trait boundary.
- The
Cachetogether with the WAL and snapshots is the Raft state machine and log store. - Nodes talk to each other over a separate peer port (default
7379), using gRPC throughtonicor a simple framed bincode protocol over TCP.
- A client sends
SET k vto any node. - If that node isn't the leader for
k's shard, it repliesERR MOVED(§5.3). - The leader appends the command to its Raft log and replicates it to followers.
- Once a majority has the entry, it is committed and applied to
Cache, and the leader repliesOK.
| Mode | Guarantee | Mechanism |
|---|---|---|
linearizable (default) |
Reflects all acknowledged writes. | ReadIndex: the leader confirms it is still leader with a heartbeat round, waits until it has applied up to the commit index, then serves the read locally. |
lease |
Linearizable as long as clocks are bounded. | The leader serves reads locally while its lease has not expired. |
stale |
May lag behind. | Any replica serves the read from its local state. Enabled with a per-connection READMODE stale command. |
- Election timeout: randomized between 150 and 300 ms. Heartbeat interval: 50 ms. Both are configurable.
- Leader crash: a follower is elected after the timeout. Clients that receive
NOLEADERor a connection error retry with backoff. - Network partition: only the majority side can commit, so the minority side rejects writes.
- Hash slots:
slot = xxhash64(key) % 1024. There are 1024 fixed slots. - Slots are grouped into shards. Each shard is one Raft replica group with a replication factor of 3 by default.
- The shard map records which slots belong to which shard and which nodes replicate each shard. It is stored in a dedicated metadata Raft group (shard 0) that runs on the first 3 nodes.
- Any node can accept a client request. If the node doesn't lead the key's shard, it replies
ERR MOVED. - Clients (including
dkv-cli) cache the shard map fromCLUSTER INFOand send requests straight to the right leader.
- Nodes join and leave the cluster with
dkv-cli cluster add-nodeanddkv-cli cluster remove-node. These commands are written to the metadata group. - Replica groups change their membership with Raft joint consensus, which
openraftprovides. - Rebalancing (stretch): moving a slot means streaming a snapshot of that slot's keys to the destination shard, then switching ownership atomically in the shard map.
The node reads dkv.toml. CLI flags and DKV_* environment variables override it.
node_id = 1
client_addr = "127.0.0.1:6379"
peer_addr = "127.0.0.1:7379"
data_dir = "./data"
fsync = "batch" # always | batch | never
seeds = ["127.0.0.1:7379", "127.0.0.1:7380", "127.0.0.1:7381"]
[raft]
heartbeat_ms = 50
election_timeout_ms = [150, 300]
snapshot_threshold = 268435456- Logging uses
tracingwithtracing-subscriber, structured and with the level controlled byRUST_LOG. - A Prometheus endpoint at
/metricsreports ops/sec per command, p50/p99 latency, Raft term, commit index, applied index, WAL size and connection count.
| Level | What | Where |
|---|---|---|
| Unit | Cache, protocol parser, WAL encoding and decoding. |
src/** #[cfg(test)] |
| Integration | Real TCP server and client, plus crash-and-recover tests. | tests/ |
| Cluster | 3- and 5-node clusters run in-process. Kill leaders, partition nodes, check results. | tests/cluster_*.rs |
| Linearizability | Record operation histories and check them with a Jepsen-style checker such as stateright or Porcupine-style analysis. |
Stretch goal |
| Property / fuzz | proptest for the parser and the WAL, cargo fuzz for the protocol. |
CI (.github/workflows/) already runs fmt, clippy -D warnings, build, and test on Linux, macOS and Windows. Every milestone must keep all of these passing.
- Over 100k
GETops/sec and over 50kSETops/sec withfsync = batch. - p99 latency under 1 ms for
GETand under 5 ms for replicatedSETon a 3-node local cluster.
These numbers will be tracked with a criterion benchmark and a small load-generator binary.
| Milestone | Scope | Done when |
|---|---|---|
| 0. Core ✅ | In-memory Cache with get, set, delete, exists, len and clear. CI. |
Done. |
| 1. Single-node server | Line protocol (§5), all basic commands, one task per connection, shared state, dkv-cli, config file, tracing. |
nc localhost 6379 supports every §5.2 command, and integration tests cover them. |
| 2. Durability | WAL, snapshots, recovery, TTL. | A server killed with kill -9 loses no acknowledged writes. |
| 3. Replication | openraft integration, peer RPC, leader redirects, ReadIndex reads. |
A 3-node cluster survives the loss of any single node, and the cluster tests pass. |
| 4. Sharding | Hash slots, metadata group, multiple Raft groups, CLUSTER INFO, smart client. |
Keys spread across several shards, and adding a node works. |
| 5. Hardening | Metrics, benchmarks, fuzzing, linearizability checks, docs. | The §9.4 targets are met. |
- Auth (password or token) and TLS on both the client and peer ports.
- Compare-and-set (
CAS <key> <version> <value>) using per-key versions. - Watch and subscribe on keys.
- A pluggable storage engine, for example
sledorrocksdb, for datasets larger than memory. - Automatic rebalancing of slots.
- Raft: use
openraft, or write it from scratch for learning? This spec assumesopenraftbehind a trait so the choice can be revisited. - Peer transport:
tonic(gRPC) brings protobuf tooling, while framed bincode over TCP has fewer dependencies. Which is preferred? - Values: stay with UTF-8
String, or switch toVec<u8>/bytes::Bytesnow, before the WAL format is fixed? Switching later means migrating the on-disk format. - Metadata group: keep a separate Raft group, or start with a static shard map from config for Milestone 4?
# run tests
cargo test
# run the demo binary
cargo run
# run the server (listens on 127.0.0.1:6379)
cargo run --bin server
# in another terminal
echo PING | nc -q1 127.0.0.1 6379 # → PONGTBD.