A simple distributed key‑value store built with only the standard library. It supports:
- In‑memory storage with thread safety.
- Append‑only log (AOF) and periodic snapshots for persistence.
- Master‑slave replication.
- Binary protocol over TCP.
- Multi‑threaded server with a fixed thread pool.
- Graceful shutdown via a file.
- Rust 1.70 or newer.
cargo build --releaseThe binary is target/release/microdb.
Configuration can be set in a file (default microdb.conf) or via environment variables. Environment variables override file settings.
Lines: key=value. Empty lines and lines starting with # are ignored.
| Key | Description | Default |
|---|---|---|
port |
TCP port to listen on | 6379 |
data_dir |
Directory for data files | ./data |
snapshot_interval |
Seconds between automatic snapshots | 10 |
snapshot_commands |
Number of commands between snapshots | 1000 |
role |
master or slave |
master |
master_addr |
Address of master (if slave) | (none) |
threads |
Number of worker threads | CPU cores |
Prefix with MICRODB_. Example: MICRODB_PORT=6380.
Create master.conf:
port=6379
data_dir=./data_master
role=master
Start:
./target/release/microdb --config master.confCreate slave.conf:
port=6380
data_dir=./data_slave
role=slave
master_addr=127.0.0.1:6379
Start:
./target/release/microdb --config slave.confA simple client is provided in client.rs. Compile it:
rustc client.rs -o clientUse it to send commands:
# Set key "foo" to value "bar"
./client 127.0.0.1:6379 set foo bar
# Get key "foo"
./client 127.0.0.1:6379 get foo
# Delete key "foo"
./client 127.0.0.1:6379 delete foo
# Save a snapshot manually
./client 127.0.0.1:6379 saveResponses are printed as Rust debug output.
- AOF – Every mutating command (
set,delete) is appended toappendonly.aofin the data directory. - Snapshots – A snapshot is saved to
dump.rdbin the data directory. Snapshots are taken automatically when the command count or time interval is reached, or manually via thesavecommand. - Recovery – On startup, the latest snapshot is loaded, then the AOF is replayed.
- Master listens for slave connections. A slave connects and sends a
Replicatecommand. - After connection, the master sends every mutating command to all connected slaves.
- Slaves apply the commands to their own store (they do not write to their own AOF – they rely on the master for persistence).
Create a file named shutdown.txt in the server’s working directory. The server will:
- Stop accepting new connections.
- Flush the AOF.
- Write a final snapshot.
- Exit cleanly.
A test script test_all.sh is included. It runs a series of tests and reports results.
chmod +x test_all.sh
./test_all.shThe script covers:
- Basic operations (set, get, delete, save)
- Persistence after restart
- Replication (master‑slave)
- Concurrency (100 simultaneous requests)
- Snapshot scheduling
- Graceful shutdown
- Malformed command handling
- Performance (operations per second)
- Keys are limited to 255 bytes.
- Values can be up to 2³²‑1 bytes.
- Only synchronous I/O.
- No authentication or encryption.
MIT