Design a system similar to Redis-like In-Memory Key-Value Store
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-04-22
Problem Statement
Design a distributed in-memory key-value store (like Redis) that supports high-performance read/write operations, persistence, and optional clustering. The system should handle concurrent access, replication, eviction policies, and data durability.
Functional Requirements (FR)
1.Core Operations
•SET key value – Store a value for a key.
•GET key – Retrieve the value of a key.
•DEL key – Delete a key-value pair.
•Support additional operations: INCR, DECR, EXPIRE, TTL.
2.Data Types
•Support basic data types: string, list, set, hash, sorted set.
3.Persistence & Durability
•Optional snapshotting (RDB) – periodic persistence.
•Append-only file (AOF) – log every write for durability.
4.Eviction & Memory Management
•Evict keys when memory is full using policies like LRU, LFU, or TTL-based expiration.
5.Replication & High Availability
•Master-slave replication for fault tolerance.
•Failover to slave if master fails.
6.Pub/Sub (Optional)
•Allow clients to subscribe to channels and publish messages.
7.Clustering & Sharding
•Distribute data across multiple nodes for scalability.
•Support key-based partitioning.
Non-Functional Requirements (NFR)
•High Performance – Low latency reads/writes (microseconds).
•Scalability – Handle millions of keys and concurrent clients.
•Consistency – Strong consistency on master, eventual consistency on slaves.
•Availability – Minimal downtime with failover and replication.
•Memory Efficiency – Optimize memory usage for large datasets.
Core Components
1.Client Interface
•Provides API for GET, SET, DEL, etc.
•Handles network communication.
2.Command Processor
•Parses client commands and routes them to the appropriate data store.
3.In-Memory Storage Engine
•Primary store for all key-value data.
•Supports efficient data structures for different types.
4.Persistence Layer
•Snapshotting (RDB)
•Append-only log (AOF)
5.Eviction & Expiry Manager
•Monitors memory usage and evicts keys based on policies.
•Handles TTL expiration for keys.
6.Replication & High Availability
•Master-slave replication.
•Failover management.
7.Cluster Manager (Optional)
•Handles data partitioning and node membership.
•Redistributes keys when nodes join/leave.
Data Flow
1.Client sends a command → Command Processor.
2.Command Processor accesses In-Memory Storage → returns result.
3.Persistence Layer logs the write operation (AOF) or snapshots periodically (RDB).
4.Replication Layer synchronizes data with slave nodes.
5.Eviction/Expiry Manager removes expired keys or evicts keys if memory limit exceeded.
Design Considerations
1.Data Structures
•Hash tables for key-value mappings.
•Linked lists, skip lists, or sorted sets for advanced data types.
2.Concurrency & Threading
•Single-threaded for simplicity (like Redis) or multi-threaded for scaling.
•Event-loop architecture for handling multiple clients efficiently.
3.Persistence Trade-offs
•RDB: low performance overhead, less durable.
•AOF: high durability, higher write overhead.
4.Replication & HA
•Asynchronous replication for performance.
•Consistency vs availability trade-offs during failover.
5.Scaling
•Vertical scaling: bigger memory per node.
•Horizontal scaling: partitioning/sharding with cluster manager.
✅ This HLD problem tests:
•Designing high-performance in-memory systems
•Concurrency, memory management, and eviction strategies
•Replication, failover, and distributed clustering
•Trade-offs between persistence, latency, and durability