Redis
An in-memory data store used as a cache, session store, rate limiter, and message broker — microsecond reads.
By Ratish Jain · Updated
Definition
Redis (REmote DIctionary Server) is an in-memory key-value data store. Because data lives in RAM, reads and writes complete in microseconds instead of the milliseconds a disk-based database needs.
Why It Exists
Databases are durable but slow — every read may hit disk. Most apps read the same hot data far more than they write it. Redis puts that hot data in memory, cutting latency ~100x and shielding the database from load.
What It's Used For
- Cache — the classic cache-aside pattern in front of a DB.
- Session store — fast, shared session state across app servers.
- Rate limiting — atomic counters with
INCR+ TTL. - Leaderboards — sorted sets rank millions of entries in real time.
- Pub/Sub & queues — lightweight messaging and job queues.
How It Works
Redis is single-threaded for command execution, so operations are atomic with no locking. It offers rich types — strings, hashes, lists, sets, sorted sets — not just opaque blobs. Durability is optional via RDB snapshots and the AOF append-only log; you trade some durability for raw speed.
Tradeoffs
- Sub-millisecond latency.
- Rich data structures beyond plain key-value.
- Atomic operations without app-side locks.
- RAM is expensive — dataset must fit in memory (or shard).
- Not a primary durable store; risk of data loss on crash.
- Single-threaded — one slow command blocks others.
When to Use / Avoid
- Use when
- Read-heavy hot data, sessions, counters, real-time ranking.
- Avoid when
- Data is larger than RAM budget or must be the durable source of truth.
- Real-world
- Twitter timelines, Stack Overflow cache, Discord presence.
Interview Questions
Both are in-memory caches. Redis adds rich data types, persistence, replication, and pub/sub; Memcached is simpler and multi-threaded for pure string caching. Default to Redis unless you only need a plain LRU string cache.
It doesn't automatically — you manage it. Cache-aside with TTLs is common; on writes you invalidate or update the key. Stale reads within the TTL window are the accepted tradeoff.
- →In-memory KV store → microsecond reads.
- →Cache, sessions, rate limits, leaderboards, pub/sub.
- →Single-threaded → atomic ops, but one slow command blocks all.
- →Optional durability (RDB snapshot + AOF log).
- →Must fit in RAM or shard across a cluster.
Related
Everything on Archprimer is connected — follow the graph.