Implement thread-safe Key-Value store (Put, Get, Delete) without inbuilt libraries

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-01-22

Problem Statement

Design a Key-Value Store that supports basic operations (put, get, delete) and is thread-safe. Do not use inbuilt concurrent libraries (like ConcurrentHashMap). Focus on OOP design, concurrency control, and low-level data structures.


Functional Requirements


1.Put Operation
•Add or update a key-value pair.
•If the key exists, update its value.
2.Get Operation
•Retrieve the value associated with a key.
•Return null or error if the key does not exist.
3.Delete Operation
•Remove a key-value pair from the store.
•Return status indicating success or failure.
4.Thread-Safety
•Multiple threads should be able to perform get, put, delete concurrently without corrupting data.
5.Optional Features
•Expiry / TTL for keys.
•Size limit and eviction policies (LRU, LFU).
•Support for atomic read-modify-write operations.


Non-Functional Requirements


•Concurrency: Ensure operations are atomic and thread-safe.
•Performance: Minimize lock contention and maximize throughput.
•Scalability: Efficiently handle a large number of keys and concurrent threads.
•Memory Efficiency: Optimize storage for key-value pairs.


Edge Cases & Constraints


•Concurrent put and delete on the same key → must not leave store in inconsistent state.
•High-frequency get → should be fast and ideally lock-free if possible.
•Null keys or values → handle explicitly.
•Resizing underlying storage → ensure thread safety during resize.


Design Hints


•Key Classes / Interfaces:
•KeyValueStore<K, V> → exposes put(key, value), get(key), delete(key).
•Bucket → internal class storing key-value pairs (for hash-based storage).
•Lock → manage synchronization at bucket or store level.
•Data Structures:
•Array of Buckets (Hash Table) → for O(1) average lookup.
•LinkedList / BST inside Bucket → handle collisions.
•Reentrant Locks / Mutex per bucket → fine-grained locking for concurrency.
•Concurrency Control:
•Bucket-level locking → lock only affected bucket for put/delete.
•Read-Write Locks → allow multiple concurrent reads but exclusive writes.
•Atomic operations → for counters or reference updates if needed.
•Design Patterns:
•Singleton (Optional) → for a single global store instance.
•Strategy (Optional) → pluggable collision handling or eviction policy.


Example Flow


1.Thread A executes put("user1", "Alice") → hash key, acquire bucket lock, insert/update value, release lock.
2.Thread B executes get("user1") → hash key, acquire read lock (or lock-free), return value.
3.Thread C executes delete("user1") → hash key, acquire bucket lock, remove key, release lock.
4.Multiple threads can perform these operations concurrently on different keys with minimal contention.


Evaluation Areas


•Clean class and method design.
•Correct hashing and collision resolution.
•Efficient thread-safe implementation without inbuilt concurrency libraries.
•Extensibility for TTL, eviction policies, or atomic operations.


This LLD problem tests OOP design, hash-based data structures, concurrency handling, and low-level thread-safe implementation of a core system component.

Solution

Comments