Design a Key-Value Store
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-03-02
Problem Statement
Designing a Key-Value Store that allows storing, retrieving, and deleting key-value pairs efficiently. The design should focus on modularity, scalability, and clean class design, with optional support for concurrency and persistence.
✅ Functional Requirements
1.Basic Operations
•put(key, value): Insert or update a key-value pair.
•get(key): Retrieve the value associated with a key. Returns null if key does not exist.
•delete(key): Remove a key-value pair from the store.
2.Advanced Features (Optional)
•exists(key): Check if a key exists.
•Support for time-to-live (TTL) per key.
•Versioning or history of values.
•Persistence to disk or database for durability.
3.Concurrency
•Support concurrent get/put/delete operations safely.
•Ensure thread-safety without significant performance degradation.
4.Scalability & Efficiency
•Efficient key lookup (O(1) for basic operations).
•Handle a large number of keys (millions).
•Optional: sharding or partitioning for horizontal scalability.
🔐 Constraints & Assumptions
•Keys and values can be assumed as strings (can be generalized).
•Focus primarily on LLD and class design; low-level storage can be in-memory (e.g., HashMap or dictionary).
•For concurrency, assume single-machine setup; distributed locking is optional.
•Memory usage should be optimized for large-scale data.
🧱 Key Components & Classes
1.KeyValueStore Class
1
2class KeyValueStore {
3private:
4 unordered_map<string, string> store;
5 mutex mtx; // For thread-safe access
6
7public:
8 KeyValueStore() {}
9
10 void put(const string &key, const string &value);
11 string get(const string &key);
12 void deleteKey(const string &key);
13 bool exists(const string &key);
14};1.TTL (Optional)
1
2struct ValueWithTTL {
3 string value;
4 time_t expiryTime; // Optional
5};1.Concurrency Handling
•mutex or read-write locks to allow concurrent reads while serializing writes.
2.Persistence (Optional)
•Append-only log or periodic snapshot to disk.
•Could use a FileHandler or DBAdapter class for I/O abstraction.
🔄 Example Workflow
1.put("user:1", "Alice") → store key-value pair.
2.get("user:1") → returns "Alice".
3.delete("user:1") → removes key "user:1".
4.Concurrent requests handled via thread-safe operations.
5.Optional TTL expires a key automatically after a specified duration.
🧪 Expected Deliverables
•Class diagram showing KeyValueStore and optional TTL/Persistence components.
•Implementation of put, get, delete, and exists methods.
•Thread-safety mechanisms.
•Test cases:
•Adding, updating, deleting keys
•Retrieving non-existent keys
•TTL expiration behavior
•Concurrent access correctness
💡 Discussion Points / Trade-offs
1.Data Structures
•HashMap → O(1) access, simple in-memory storage
•TreeMap → O(log N) access, keeps keys sorted
2.Concurrency Strategy
•Single mutex → simple but may reduce throughput
•Read-Write locks → allow concurrent reads, serialize writes
3.Persistence vs Performance
•In-memory store → fast but volatile
•Periodic snapshot or append-only log → durable but slower
4.Scalability
•Horizontal scaling: consistent hashing for sharding across nodes
•Eviction policies for memory-limited environments (LRU,