Skip to content
Caching

Lesson 8 of 14 · 28 min

x
8/14

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

Redis Deep Dive

Redis is the dominant distributed cache. It is an in-memory data store that supports strings, hashes, lists, sets, sorted sets, bitmaps, and more — far beyond simple key-value caching. Local Redis calls are typically very fast because data lives in RAM; once you add network hops, measure p50/p99 rather than assuming a fixed latency.

Persistence options matter for production. RDB (Redis Database) takes point-in-time snapshots at intervals — fast recovery, but you may lose data since the last snapshot. AOF (Append Only File) logs every write operation — more durable, slower recovery. Most teams use both: RDB for fast restarts, AOF for durability. Redis Pub/Sub enables real-time messaging between services — useful for cache invalidation broadcasts. For caching specifically, strings with SETEX (set with expiry) and hashes for structured objects are the most common patterns.

Security boundary: do not cache authorization decisions or full auth cookies in a shared cache without careful key design and TTL. Keep Redis off the public internet — require network controls and AUTH/ACL; treat cache contents as sensitive as the data you store there.

Before
Plain string cache — no structure
1await redis.setex('user:42', 300, JSON.stringify({2  name: 'Alice', email: 'alice@example.com', role: 'admin'3}));4// Must deserialize entire object to read one field
After
Redis hash — partial field access
1await redis.hset('user:42', {2  name: 'Alice',3  email: 'alice@example.com',4  role: 'admin',5});6await redis.expire('user:42', 300);7 8// Read a single field without deserializing everything9const email = await redis.hget('user:42', 'email');10 11// Atomic increment for rate limiting12await redis.incr('rate:user:42');13await redis.expire('rate:user:42', 60);

Exercise

Store a user profile as a Redis hash with EXPIRE, read one field with HGET, and implement a simple INCR rate-limit key with TTL. Note that Redis must not be exposed to the public internet without auth/network controls.

Previous

Progress is saved in this browser.

Next Lesson