Skip to content

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.

  • Traits are contracts — a Store trait 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 MemStore works for any key and value type, and monomorphization compiles a specialized copy per concrete type, so the abstraction costs nothing at runtime.
  • Lifetimes in contextget clones for simplicity, but the page shows how a borrow could return a reference without copying, making lifetimes something you feel rather than memorize.
  • Fearless concurrencyArc<RwLock<T>> shares one store across threads, Send/Sync are how the compiler knows it’s safe, and the shared-mutable access simply won’t compile until it’s behind a lock.
  • Message passingmpsc channels 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; flush is not fsync, and the durability spectrum matters.
  • A networked store — a std::net thread-per-connection TCP server speaks a line protocol (SET/GET/DEL), letting many clients share one store over the network.

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.