Concurrency in an In-Memory Key-Value Store and Hit Counter
Context and Assumptions
Assume you are implementing an in-memory key-value store and a hit counter within a single process, shared by multiple threads:
-
KeyValueStore supports: get(k), put(k, v), delete(k), and optionally compareAndSet(k, expected, new).
-
HitCounter supports: increment([key]), getCount([key]) — either a single global counter or per-key counters.
-
Data structure backing the store is a hash map with buckets and possible resizing.
-
High read-to-write ratio is common, but hot keys and heavy write bursts can occur.
Tasks
-
Identify the race conditions that can occur under concurrent access.
-
Propose thread-safety mechanisms (coarse-/fine-grained locks, read/write locks, atomic operations, lock-free designs) and explain their correctness.
-
Discuss performance trade-offs of the chosen mechanisms.
-
Outline how to test and validate concurrency behavior.