liveBook · Manning

Key-value store

As explained in Latency

A key-value store is a data-storage system that associates values with keys and supports operations such as retrieving and updating a value by its key.

A key-value store associates each value with a key and supports direct operations such as retrieving or updating a value by that key. A hash map provides a simple in-memory implementation; a durable database can replace it when persistence matters. section 4.8

The interface is effective when data access is simple enough to avoid transactions or complex queries. Caching commonly uses keys such as user IDs with profiles, settings, or session data as values, but freshness and consistency become important trade-offs. section 6.1

A replicated design adds an operational structure around the store:

  • A primary accepts writes and broadcasts Put updates to replicas. section 4.8
  • A joining replica receives a Snapshot before applying incremental updates. section 4.8
  • Replicas can serve local reads, reducing access latency while introducing replication and consistency concerns. section 4.8 section 6.1

Primary and replica shells

Start a primary with PUT and GET commands, store a = 1 and b = 2, then start a replica. Before joining, the replica returns Not found; after JOIN localhost:8080, it receives a snapshot and returns a = 1 and b = 2. A later primary write, c = 3, becomes readable on the replica. section 4.8

pub struct KVStore {
 data: Mutex<HashMap<String, String>>,
}

impl struct KVStore {
 pub fn get(&self, key: &str) -> Option<String> {
   self.data.lock().unwrap().get(key).cloned()
 }

 pub fn put(&self, key: String, value: String) {
   self.data.lock().unwrap().insert(key, value);
 }
}

Inspect the compact Rust implementation of the store’s mutex-protected HashMap, including get and put operations. section 4.8

How Latency covers it

How deep it goes
Builds an in-memory implementation and replicated primary–replica workflow, while deferring durable database design and complex queries.
Examples
Demonstrates Rust shells storing a, b, and c, synchronizing snapshots, and propagating later writes from primary to replica.
What it assumes
Assumes basic storage and latency concepts, while teaching the hash-map-backed store and replication protocol directly.
What you can do after
Build it from scratch as an in-memory store, then operate a primary–replica setup with snapshots and update broadcasts.
How it connects
Connects key-value stores to caching, replication, latency, durability, freshness, and consistency.

Also explained in

Compare how 8 books explain key-value store →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.