Redis
Data structures, persistence, caching patterns, and scaling concepts for Redis interviews.
Fundamentals
What is Redis & Common Use Cases
Redis is an in-memory data store used for caching, sessions, queues, leaderboards, and real-time analytics.
Why Redis is Fast
Redis hits sub-millisecond latency through in-memory storage, a single-threaded event loop, and hand-tuned data structures.
Redis vs Memcached
Both are in-memory key-value stores, but Redis has richer data types, persistence, and replication while Memcached is multi-threaded and dead simple.
Keyspace & Key Naming Conventions
Redis is one flat keyspace per database. Colon-separated namespaces keep it organized, and keyspace notifications let us subscribe to key events.
Data Structures
Strings
The fundamental Redis data type — binary-safe blobs up to 512MB, perfect for cached values and atomic counters.
Lists
Doubly-linked lists for queues, capped logs, and producer-consumer patterns with blocking pops.
Hashes
Field-value maps stored under one key — the natural way to represent objects in Redis.
Sets
Unordered collections of unique strings — perfect for tags, unique visitors, and set-algebra queries like intersection and difference.
Sorted Sets (ZSET)
Unique members each tied to a numeric score — the Swiss army knife behind leaderboards, time-series, and rate limiters.
Bitmaps & HyperLogLog
Two probabilistic-friendly tools for counting at scale — bitmaps for boolean flags per ID, HyperLogLog for unique counts in 12KB.
Streams
Append-only log inside Redis with consumer groups and replay — Kafka-lite for event sourcing without standing up a new cluster.
Persistence
RDB Snapshots
Point-in-time binary dumps of the dataset — fast restarts, smaller files, but you lose recent writes.
AOF (Append Only File)
Log every write to disk — durability over speed. Tune with fsync policies and compact with BGREWRITEAOF.
Hybrid Persistence (RDB + AOF)
The recommended setup — RDB snapshot at the head of the AOF file gives fast restarts and strong durability.
Caching Patterns
TTL & Expiry
How Redis expires keys — set a TTL with EXPIRE, check with TTL, and understand the active + lazy cleanup algorithm.
Eviction Policies
When Redis hits maxmemory, what gets kicked out? LRU, LFU, random, TTL-based — pick the right one for your workload.
Cache Strategies
Cache-aside, read-through, write-through, write-behind — how your app and cache and DB talk to each other.
Cache Stampede / Thundering Herd
When a hot key expires and a thousand requests hit the DB at once. Fix with locks, probabilistic early expiry, or request coalescing.
Advanced
Pub/Sub
Fire-and-forget messaging in Redis using SUBSCRIBE, PUBLISH, and pattern matching.
Transactions
MULTI/EXEC, optimistic locking with WATCH, and why Redis transactions aren't really ACID.
Lua Scripting & Pipelining
EVAL for atomic multi-step logic and pipelining for round-trip throughput — two different tools, often confused.
Distributed Locks (Redlock)
SET NX EX lock pattern, the Redlock algorithm, and the Kleppmann critique that every senior engineer should know.