⚡ Redis In-Memory Data Structures & Distributed Systems
1️⃣ Why Redis? RAM-First Engine & Single-Threaded Event Loop
Definition: Redis (Remote Dictionary Server) ek open-source, ultra-fast in-memory key-value data store hai jiska primary storage RAM (Random Access Memory) me hota hai. Iska core execution model single-threaded non-blocking I/O multiplexing (epoll/kqueue) par operate karta hai, jo race-condition-free atomic execution deliver karta hai.
| Criteria | Traditional Disk DB (Postgres/MySQL) | Redis In-Memory Engine |
|---|---|---|
| Primary Storage | SSD / NVMe Disk with Buffer Pool | DRAM (Physical RAM) |
| Read/Write Latency | 5 - 25 milliseconds ($10^{-3}$ s) | Sub-millisecond (100 - 500 microseconds, $10^{-6}$ s) |
| Concurrency Control | Complex table/row locks & MVCC | Atomic Single-Threaded Event Loop (Zero locking overhead) |
2️⃣ Advanced Core Data Structures & Complexity
| Data Structure | Key Commands | Time Complexity | Ideal Production Use Case |
|---|---|---|---|
| Strings | SET, GET, INCRBY, SETNX |
$O(1)$ | Session tokens, rate-limiting counters, serialized JSON caches. |
| Hashes | HSET, HGET, HGETALL |
$O(1)$ per field | User profiles, cart metadata (Memory-efficient ziplist/hashtable). |
| Lists | LPUSH, RPOP, BLPOP |
$O(1)$ ends, $O(N)$ lookup | Producer-Consumer message queues, activity feeds. |
| Sets | SADD, SISMEMBER, SINTER |
$O(1)$ add/check, $O(N)$ union | Unique visitor IP sets, tag intersections, friend graphs. |
| Sorted Sets (ZSET) | ZADD, ZRANGEBYSCORE, ZRANK |
$O(\log N)$ (Skip List) | Real-time gaming leaderboards, sliding window rate-limiters. |
# Sliding Window Rate Limiter using ZSET (Allow max 100 requests per 60 seconds)
$now = CURRENT_EPOCH_MS
$window_start = $now - 60000
MULTI
ZREMRANGEBYSCORE "rate_limit:user_102" 0 $window_start
ZADD "rate_limit:user_102" $now $now
ZCARD "rate_limit:user_102"
EXPIRE "rate_limit:user_102" 60
EXEC
# If ZCARD count > 100 → Reject request with HTTP 429 Too Many Requests!
3️⃣ Enterprise Caching Strategies & Stampede Prevention
| Pattern | Application Flow | Consistency & Tradeoff |
|---|---|---|
| Cache-Aside (Lazy Loading) | Read from Cache → If miss, read DB → Write to Cache. | Data can get stale until TTL expires. Simplest to maintain. |
| Write-Through | App writes to Cache → Cache synchronously writes to DB. | Strong consistency; higher write latency overhead. |
| Write-Behind (Write-Back) | App writes to Cache immediately → Background async batch worker syncs DB. | Ultra-fast writes; risk of data loss if Redis crashes before DB commit. |
• Mutex Locking: Sirf pehla thread lock lekar DB hit kare, baaki threads 50ms wait karein.
• Probabilistic Early Expiration (XFetch Algorithm): Key ke actual TTL khatam hone se thoda pehle background thread asynchronously cache refresh kar de ($-\beta \cdot \log(\text{rand}()) \cdot \Delta > \text{TTL}$).
4️⃣ Persistence Deep Dive: RDB vs AOF
| Persistence Mechanism | How It Works | Data Loss Risk | Recovery Speed |
|---|---|---|---|
| RDB (Redis Database Snapshot) | Periodic point-in-time binary dump file (dump.rdb) via bgsave (COW - Copy On Write). |
High (Lost writes between snapshot intervals, e.g. 5-15 mins). | Ultra-Fast: Direct memory restoration from compact file. |
| AOF (Append Only File) | Har write query ko append log file me log karta hai (fsync everysec). |
Low (Maximum 1 second of transactions lost). | Slower (Must sequentially replay commands; uses AOF rewrite). |
5️⃣ Memory Management & Eviction Policies
Jab Redis ka allocated RAM limit (maxmemory) full ho jata hai, tab naye keys accept karne ke liye configured policy execute hoti hai:
| Eviction Policy | Target Key Pool | Algorithm Mechanism |
|---|---|---|
noeviction |
None | Naye write queries par OOM (Out Of Memory) error throw karta hai. |
allkeys-lru |
All keys | Approximated Least Recently Used keys ko evict karta hai. (General Caching default). |
volatile-lru |
Keys with TTL set | Sirf un keys ko evict karta hai jinka expiry time configured ho. |
allkeys-lfu |
All keys | Least Frequently Used keys ko drop karta hai frequency access counters ke basis par. |
6️⃣ Distributed Locking: Redlock & Lua Scripts
Multiple microservice nodes ke beech shared resources ko mutate hone se bachane ke liye distributed locks:
# 1. Atomic Lock Acquisition with Random Token (UUID) and 10s auto-release
SET resource_lock "uuid_payload_8829" NX PX 10000
# 2. Safe Atomic Release via Lua Script (Prevents releasing another worker's expired lock)
if redis.call("get", KEYS[1]) == ARGV[1] then
return redis.call("del", KEYS[1])
else
return 0
end
7️⃣ Redis Streams vs Pub/Sub & Cluster Sharding
- Pub/Sub (Fire & Forget): Real-time notifications ke liye jahan message retention ki zaroorat nahi hoti (Agar subscriber offline hai toh message permanently drop ho jata hai).
- Redis Streams (
XADD, XREADGROUP): Kafka-like consumer groups, persistence log aur consumer acknowledgment (ACK) capabilities provide karta hai. - Redis Cluster Sharding: Data ko 16,384 Hash Slots me divide karke horizontally scale karta hai (Key hash slot =
CRC16(key) % 16384).