Revision · Project 2 — kvlite
Project 2 built kvlite, a mini-Redis that holds data instead of just reading it — long-lived, shared, mutable, and durable all at once. That single shape pulled in the next tier of Rust. Here’s what to carry forward.
What this part covered
Section titled “What this part covered”- Traits are contracts — a
Storetrait names the behavior (get/set/delete) without dictating the type, so the engine is reusable and swappable rather than a single hard-coded class. - Generics are zero-cost — a generic
MemStoreworks for any key and value type, and monomorphization compiles a specialized copy per concrete type, so the abstraction costs nothing at runtime. - Lifetimes in context —
getclones for simplicity, but the page shows how a borrow could return a reference without copying, making lifetimes something you feel rather than memorize. - Fearless concurrency —
Arc<RwLock<T>>shares one store across threads,Send/Syncare how the compiler knows it’s safe, and the shared-mutable access simply won’t compile until it’s behind a lock. - Message passing —
mpscchannels are the other tool: instead of sharing state, hand ownership down a channel, sidestepping locks entirely for some designs. - Locks aren’t correctness — an honest note that a lock buys race-freedom, not logical correctness; you still have to reason about what happens between operations.
- Durability by log — RAM is wiped on exit, so writes go to an append-only log that you replay on startup with an iterator pipeline;
flushis notfsync, and the durability spectrum matters. - A networked store — a
std::netthread-per-connection TCP server speaks a line protocol (SET/GET/DEL), letting many clients share one store over the network.
The takeaway
Section titled “The takeaway”A KV store is the smallest program that is simultaneously shared, mutable, long-lived, and durable, so every big idea earned its place. The headline lesson is fearless concurrency: the moment two threads touch one HashMap, C would let you race and Rust simply won’t compile it. Deliberately using std threads here also sets up the next project, apilite, where you’ll feel exactly why you’d reach for async.