Design a thread-safe key-value store
NetflixSoftware Engineer · Technical ScreenLast updated
This question evaluates knowledge of concurrent programming and thread-safety, including the choice of in-memory data structures for a key–value store and synchronization techniques that ensure atomic put/get/delete operations without internal corruption.
You are working on infrastructure for an AI platform. Inside a single process, many worker threads need to share a simple in-memory key–value store; any thread can concurrently read, write, or delete keys.
Design and discuss a thread-safe key–value store class with the following requirements:
- Environment: Single process, multiple threads (no multi-machine / distributed concerns).
- Operations:
put(key, value): insert or overwrite the value forkey.get(key): return the current value forkey, ornull/Noneif absent.delete(key): removekeyif it exists.- Correctness:
- Operations must be safe under arbitrary concurrent usage (no lost updates, no corrupted internal state).
- Each operation should appear atomic to callers.
- Performance:
- Aim to minimize lock contention; a single global lock is allowed but you should consider and discuss alternatives.
- Data model:
- Keys can be assumed to be strings; values can be arbitrary objects (or generics).
Answer the following sub-questions:
- What internal data structure(s) would you use to store the key–value pairs, and why?
- What synchronization strategy would you apply (e.g., a single global lock, per-bucket or per-key locks, lock striping, or a language-provided concurrent map)? Discuss the trade-offs.
- Suppose you must implement this in a language without a built-in concurrent map (for example, Python with a normal
dict). How would you implement your chosen synchronization strategy there? Describe or sketch the implementation ofput,get, anddelete. - How would you test / validate that your implementation is correct and free from race conditions? Consider unit tests, concurrent stress tests, and any tools or techniques you might use.
- If this store were to be used in production, what additional concerns would you consider (e.g., validation of inputs, logging, metrics, capacity limits, performance tuning, or persistence)?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.