smikic.com
evergreentended May 24, 2025

Building a Distributed Key-Value Store in C++ (Part 1)

Key takeaways
  • A key-value store is a glorified dictionary - start with the simplest possible in-memory implementation before adding any distribution concerns.
  • Defining only three operations (put, get, del) up front keeps the first phase small enough to ship and test.
  • Shipping phase by phase forces you to confront distributed problems one at a time instead of all at once.

I wanted to understand distributed systems properly, not just read about them. So I decided to build a key-value store from scratch in C++ - something that starts as a simple std::unordered_map and eventually becomes a multi-node cluster with leader election. This is the first post in that series.

The plan is six phases, built one at a time. Here's the rough roadmap:

  • Phase 1 - Local store ✅: in-memory KVStore with put, get, del
  • Phase 2 - Persistence: append-only log for crash recovery
  • Phase 3 - Networking: TCP server and CLI client
  • Phase 4 - Multi-node: replication across instances
  • Phase 5 - Consensus: leader election (Bully algorithm)
  • Phase 6 - Testing & resilience: fuzzing, chaos, graceful failure

Phase 1: the simplest thing that works

A key-value store is just a dictionary with some extras. For now, the extras are zero.

File structure

distribkvstore/
├── CMakeLists.txt
├── src/
│   ├── kvstore.cpp
│   ├── kvstore.hpp
│   └── main.cpp
└── tests/
    └── test_kvstore.cpp  (planned)

Three operations

  • put(key, value) - insert or update
  • get(key) - retrieve
  • del(key) - delete

Example usage

cpp
int main() {
  KVStore store;
  store.put("hello", "world");
 
  if (auto val = store.get("hello")) {
    std::cout << "GET hello: " << *val << "\n";
  }
 
  if (store.del("hello")) {
    std::cout << "deleted hello\n";
  }
 
  if (!store.get("hello")) {
    std::cout << "hello not found after deletion\n";
  }
 
  return 0;
}

That's it for Phase 1. It compiles, it runs, and it's the foundation for everything else.

Architecture

KV store architecture diagram - client, KVStore, and append-only log
Phase 1: a single-node KVStore backed by an append-only log. The log is replayed on startup for crash recovery.

What's next

Part 2 adds unit tests and an append-only log for persistence. Without that, the store forgets everything when the process exits.

2 linked references
Building a Distributed Key-Value Store in C++ (Final)

...s is the final post in the series. If you are arriving here for the first time, [Part 1](/notes/building-distributed-kv-store-pt1) is the right starting point - it covers the in-memory store and the overall roa...

Building a Distributed Key-Value Store in C++ (Part 3)

...tests before introducing a test harness for socket I/O. </KeyTakeaways> Parts [1](/notes/building-distributed-kv-store-pt1) and [2](/notes/building-distributed-kv-store-pt2) gave us a persistent, tested...

Stefan Mikic
Stefan Mikicdata gardener

Data is my veggies - healthy, versatile, and sometimes hard to digest, but in the end, it always brings value.