Choosing lock granularity for concurrent storage
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's technical phone screen, last asked Jan 2026. It tests concurrency, locking and storage.
You are building a storage component, such as an in-memory key-value store or a page cache, that many threads use at once. Choose how coarse or fine its locking should be. Compare one global lock, per-partition (striped) locks, per-object locks, reader-writer locks and lock-free structures, and explain the effect on throughput, tail latency and correctness.
Interface
Unless you agree on a different interface with the interviewer, assume the component offers:
get(key) -> value | Noneput(key, value) -> Nonedelete(key) -> None-
multi_put(items: dict) -> None— writes several keys atomically.
Example
Global lock: every call takes one mutex -> simple, correct, throughput capped at one core
Striped (64): lock = stripes[hash(key) % 64] -> parallel single-key ops; multi_put must lock several stripes
Per-object: one lock per entry -> most parallel, more memory, harder deletes
Clarifying questions
- What is the read-to-write ratio, and how skewed are keys (hot keys)?
- Which operations must be atomic across several keys?
- What matters more: average throughput or p99 latency?
- How many cores and threads, and is the data in memory or on disk?
What a strong answer covers
- The global lock as the baseline: easy to reason about, but contention grows with threads and one slow operation stalls everyone (convoying), hurting p99.
- Striping: parallelism across keys, with the number of stripes tuned to cores; hot keys still contend on one stripe.
- Multi-key operations: take stripe locks in a fixed order (sorted stripe index) to avoid deadlock, or use a short global phase.
- Reader-writer locks for read-heavy loads, and their pitfalls: writer starvation and higher cost per acquisition than a plain mutex under short critical sections.
- Lock-free or optimistic designs (compare-and-swap, versioned reads) and their costs: complexity, memory reclamation, and subtle bugs.
- Other hazards: priority inversion, starvation, holding locks across I/O, and measuring with contention profiling before choosing.
Follow-ups
- One key receives half of all writes. What helps?
- Resizing the table under striped locks: how do you do it without stopping the world?
- How would you test the multi-key operation for deadlocks and atomicity?
- When does moving to a single-threaded shard per core beat locking altogether?
Where it's asked
- Microsoft · Technical phone screen · Last asked Jan 202660 min · One technical round
What it tests
concurrency · locking · storage
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
