Design of ConcurrentHashMap (thread-safe get, put, delete)
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-12-24
Problem Statement
Design a thread-safe hash map (ConcurrentHashMap) that supports concurrent read and write operations. The focus is on low-level design, concurrency control, and modular data structures.
✅ Functional Requirements
1.Basic Operations
•put(key, value): Insert or update a key-value pair.
•get(key): Retrieve the value associated with a key.
•remove(key): Remove a key-value pair.
2.Thread-Safety
•Support concurrent reads and writes without data corruption.
•Minimize lock contention for performance.
3.Performance
•Allow multiple threads to access different segments/buckets concurrently.
•Optimize for high throughput under concurrent access.
4.Optional Features
•Iterators for traversing keys or values (weakly consistent).
•Size estimation method (size()) in concurrent environment.
•Support for dynamic resizing while maintaining thread safety.
🔐 Constraints & Assumptions
•Keys and values are generic types (<K,V>).
•Focus is on low-level thread-safe design, not persistence or distributed storage.
•Use synchronization primitives like mutexes, locks, or atomic operations.
🧱 Key Components & Classes
1.Node / Entry Class
1
2template <typename K, typename V>
3class Node {
4public:
5 K key;
6 V value;
7 Node* next; // for handling collisions (chaining)
8};1.Bucket / Segment Class
1
2template <typename K, typename V>
3class Bucket {
4private:
5 mutex bucketLock; // lock for thread safety
6 Node<K,V>* head;
7public:
8 void put(K key, V value);
9 V get(K key);
10 void remove(K key);
11};1.ConcurrentHashMap Class
1
2template <typename K, typename V>
3class ConcurrentHashMap {
4private:
5 vector<Bucket<K,V>*> buckets;
6 int numBuckets;
7 hash<K> hashFunction;
8public:
9 void put(K key, V value);
10 V get(K key);
11 void remove(K key);
12};1.Optional: Lock Striping / Segmentation
•Divide the map into segments or buckets with individual locks to reduce contention.
•Each bucket handles collisions using linked lists or balanced trees.
🔄 Example Workflow
1.Thread 1 executes put("key1", "value1") → locks the corresponding bucket → inserts value → unlocks.
2.Thread 2 executes get("key1") concurrently → locks same or different bucket → retrieves value → unlocks.
3.Thread 3 executes remove("key2") concurrently → locks bucket → removes node → unlocks.
4.Multiple threads operate on different buckets without blocking each other.
🧪 Expected Deliverables
•Class diagram showing ConcurrentHashMap, Bucket, and Node.
•Implementation of thread-safe get, put, remove operations.
•Test cases:
•Concurrent puts and gets
•Concurrent remove operations
•Collision handling under concurrency
•Optional: dynamic resizing without losing data
💡 Discussion Points
•Trade-offs between fine-grained locks (per bucket) vs coarse-grained lock (entire map).
•Handling resizing under concurrent access safely.
•Optional: supporting iterators with weak consistency.
•Optimizations for high throughput and low contention.