Course map

Topic 087 / Core

Redis and distributed caching

Chapter 6.2 · Distributed Caching and CDNs

What and why

Cache-aside (→ 83) needs a fast, shared store that every application instance can read and write identical values from — an in-process cache alone would let each instance disagree. Distributed caching puts the cache on a separate server (or cluster) that stateless application instances (→ 91) share. Redis is this book's canonical example: an in-memory data store with data structures beyond plain key-value (strings, hashes, sorted sets, sets, lists), TTLs (→ 85), tunable persistence, and a cluster mode that partitions keys across nodes by hashing (→ 74). Builds on: → 86 (cache eviction policies: LRU, LFU).

Diagram

Structure Shape Typical use
String raw bytes/text a simple cache-aside value, counters
Hash field → value map an object's fields without round-tripping the whole blob
Sorted set score-ordered members leaderboards, time-ordered feeds (→ 99)
Set unique members deduplication, membership checks
List ordered sequence simple queues, write-back buffers (→ 84)

How it works

  1. Clients talk to Redis over a lightweight protocol; most commands execute from memory, so latency is dominated by the network round trip, not computation.
  2. Every key can carry a TTL (→ 85); an expired key never returns a stale value, whether it was removed by a background sweep or caught lazily on access.
  3. Persistence is optional and tunable: periodic snapshotting (can lose recent writes on crash) or logging every write (safer, slower) — the same durability-vs-latency trade as write-back (→ 84), now at the infrastructure level.
  4. Cluster mode splits the keyspace across multiple nodes using a hashing scheme (→ 74); a client or proxy routes each command to the node that owns that key.
  5. Commands on a given connection execute one at a time, in the order received — treat a single command as atomic, but never assume a sequence of separate commands from your application is atomic unless you use a transaction or scripting feature built for that.

Practical example

(numbers illustrative). A session lookup costs ~20 ms from the database versus ~1 ms from Redis on the same network. For a service handling 2,000 requests/second, each needing one session lookup, moving the lookup to Redis saves roughly (20 − 1) = 19 ms per request and removes 2,000 queries/second from the database entirely — the same arithmetic as topic 82's hierarchy, now anchored to one concrete store.

When to use it

  • Use when: multiple stateless instances need to share cache or ephemeral state (sessions, rate-limit counters → 96, feed caches → 99) at low, predictable latency.
  • Avoid as a system of record: Redis is a cache or ephemeral store first; even with persistence enabled, treat the database as the source of truth (→ 81) and Redis as disposable.
  • Failure scenarios: treating Redis as durable storage and losing data to a restart or memory-pressure eviction; a single Redis instance as a hidden single point of failure for every dependent service (pair with replication or cluster mode); a network partition between app and Redis degrading every cache-dependent path at once — the system still needs a correct, if slower, path when Redis is unreachable (→ 83's degrade-to-database rule).
  • Simpler alternative: an in-process (local) cache per instance when data doesn't need to be shared across instances — no network hop, but each instance keeps its own hit ratio.

Practice

(mechanism/pattern kind, 20–30 min, lab: Redis).

  1. Store a value as a plain string versus as a hash with three fields; compare retrieving one field from the hash against fetching and parsing the whole string blob.
  2. Set a TTL on a key and confirm it is gone after expiry with no application code polling for it.
  3. Build a tiny shared rate-limit counter (INCR plus TTL) and confirm two separate client processes both see the same running count — the shared-state behavior Project 4 depends on.

Done when the hash-field read and the shared counter behave identically regardless of which of two client processes performs them.

Shared lab setup

Key takeaway

Redis is the working example of a distributed cache: in-memory, rich data structures, TTLs, and a cluster mode that reuses consistent hashing. Done when you can: pick the right data structure for a given access pattern and state precisely what is lost if the instance disappears right now. Used next in → 88 (stampede and hot keys, Redis's sharp edges at scale), → 96 (rate limiting, shared state), → 99 (fan-out on write, sorted sets as per-user feeds); Project 4 (shared rate-limit state).

References

None — this topic makes no external claims.

Source directory

Search the guide