KatInterview

Concurrency Strategies for a Shared In-Memory Key-Value Store

System DesignModel answer
HardModel answerSystem Designconcurrencylockingkey-value store

Last updated

Compare global, striped, and lock-free locking for a concurrent hash map, then design an optimal strategy for read-heavy workloads.

Building a Concurrent In-Memory Store

You are designing an in-memory key-value store that supports two operations — get(key) and put(key, value) — and must serve hundreds of concurrent threads without data corruption.

The underlying storage is a hash map. Your job is to choose and justify a locking strategy that maximises throughput while keeping tail latency (p99) low.

Three candidate strategies

Strategy 1 — Single global lock. One mutex guards the entire map. Every operation acquires it before touching any data.

Strategy 2 — Stripe locking. The map's buckets are divided into groups called stripes. Each stripe has its own lock. An operation only locks the stripe that its key hashes into.

Strategy 3 — Lock-free with CAS. Entries are updated with atomic compare-and-swap instructions. No thread ever blocks another.

What you need to address

  1. How does each strategy behave under heavy contention? Consider throughput scaling, p99 latency, and the effect of a single "hot" key.
  2. What are the trade-offs across memory overhead, implementation complexity, and correctness hazards?
  3. If reads outnumber writes by roughly 10:1, which strategy (or combination) would you recommend, and how would you tune its parameters?

Specific design questions to cover

  • For stripe locking: how do you pick the number of stripes, how do you assign buckets to stripes, and how do you safely resize the underlying table?
  • For lock-free CAS: what happens when the table needs to grow while threads are actively reading and writing?

There is no code judge. Write a structured answer that explains your reasoning, quantifies trade-offs where possible, and includes a reference implementation of your recommended approach.

Hints

Hint 1

A global lock's throughput ceiling is determined entirely by serialisation — think about what Amdahl's law implies when the serial fraction is 100%.

Hint 2

For stripe locking, consider what property your stripe-assignment function needs to preserve across a table resize, and why a power-of-two stripe count makes that easy.

Hint 3

Read-write locks let concurrent readers share a stripe without blocking each other; the tricky part is preventing a continuous stream of readers from starving writers.