Implementing Key-Value Stores
1. Designing Key Naming Conventions
| Convention | Example |
|---|---|
| Colon-separated | user:42:profile |
| Namespaced | app:env:entity:id:field |
| Versioned | v2:user:42 |
| Avoid | Spaces, very long keys |
| Hash-tagged (Redis Cluster) | {user:42}:profile co-locates keys |
2. Storing Simple Values
Example: Redis Strings
SET user:42:name "Ada Lovelace"
GET user:42:name
SET counter 100
INCR counter # → 101
SET session:abc "..." EX 3600 # 1-hour TTL
MSET k1 v1 k2 v2
3. Implementing Complex Data Structures
| Type | Use |
|---|---|
| Hash | HSET user:42 name Ada age 36 |
| List | LPUSH / RPUSH queue work |
| Set | SADD tags:42 "ml" — uniqueness |
| Sorted Set | ZADD leaderboard 100 user:42 — ranking |
| Stream | XADD events * field val — event log |
| HyperLogLog | PFCOUNT — approximate cardinality |
| Bitmap / Geo | SETBIT, GEOADD |
4. Setting TTL and Expiration
| Command | Use |
|---|---|
| EXPIRE k 60 | Seconds TTL |
| PEXPIRE k 1000 | Milliseconds |
| EXPIREAT k timestamp | Absolute |
| PERSIST k | Remove TTL |
| SET k v EX 60 NX | Set if not exists with TTL |
5. Implementing Atomic Operations
| Mechanism | Detail |
|---|---|
| Single command | INCR, GETSET — atomic |
| MULTI / EXEC | Pipeline as transaction |
| WATCH | Optimistic concurrency |
| Lua script (EVAL) | Server-side atomic block |
| SETNX / SET ... NX | Distributed lock primitive |
6. Using Key-Value for Caching
| Pattern | Detail |
|---|---|
| Cache-aside | App reads cache → DB on miss |
| Write-through | Write both cache & DB |
| Write-behind | Buffered async DB write |
| Invalidation | TTL + explicit DEL on update |
| Cache stampede | Mutex / probabilistic early refresh |
7. Implementing Session Storage
| Aspect | Detail |
|---|---|
| Key | session:<sid> |
| Value | Hash or serialized JSON |
| TTL | Sliding (refresh on access) |
| Security | Long random sid; httpOnly cookie |
| Sticky-less | Centralized store allows any app server |
8. Designing for High Throughput
| Tip | Detail |
|---|---|
| Pipelining | Batch commands per round-trip |
| Cluster sharding | 16,384 slots in Redis Cluster |
| Avoid O(N) commands | KEYS *, large HGETALL |
| SCAN over KEYS | Non-blocking iteration |
| Connection pooling | Reuse TCP connections |
9. Implementing Persistence Strategies
| Option | Detail |
|---|---|
| RDB snapshot | Periodic; smaller files; some data loss |
| AOF (append-only) | Log every write; fsync policy controls RPO |
| RDB + AOF | Best of both |
| No persistence | Pure cache; rebuildable |
| Replica + AOF on replica | Offload disk I/O |
10. Handling Hot Keys
| Mitigation | Detail |
|---|---|
| Local in-process cache | Reduce remote hits |
| Key sharding | counter:{shard} + aggregate |
| Read replicas | Fan out hot reads |
| Detect | Redis MONITOR / redis-cli --hotkeys |