Skip to content

Lesson 1 of 9 · 23m

Redis Is a Data Structure Server, Not a Cache with Keys

Learn the operational mental model: single-threaded command execution, in-memory data structures, and the access patterns each primitive actually supports.

The worst Redis outages I have debugged started with a sentence like "it's just a cache." It was never just a cache. It was a rate limiter, a session store, a queue, a leaderboard, a distributed lock, and occasionally four of those at once in the same database. Redis is small enough to invite sloppy design and fast enough to hide it until traffic arrives.

The mental model that matters

Redis executes commands sequentially on a single main thread. That gives you sharp atomicity guarantees: one command is not interleaved with another. It also means one expensive command can block every other client:

redis-cli --latency
redis-cli slowlog get 5
redis-cli info commandstats | grep -E 'cmdstat_(keys|hgetall|smembers)'

A GET on one small string is usually measured in microseconds. KEYS * on a production keyspace is an outage generator. HGETALL on a million-field hash is not "one fast Redis call"; it is a data transfer and CPU event.

Choose structures by access pattern

need                                   structure
one value or serialized object         string
object with independently updated fields hash
unique membership / tags               set
ordered ranking or time index          sorted set
append-only event feed                 stream
probabilistic membership               bloom filter module, not a giant set

Do not model around what JSON conveniently serializes. Model around what the application actually does. If you update one user's last_seen forty times a minute, a hash field beats rewriting a serialized user object. If you rank by score, a sorted set beats sorting in application memory.

Key names are operational contracts

Use names that make ownership and cardinality obvious:

session:01JRYC...           one user session
ratelimit:login:198.51.100.7 fixed-window counter
leaderboard:weekly:2026w48   sorted set
stream:app-events            durable stream

The colon convention is not magic. Its value is that humans, metrics, ACLs, scanners, and cleanup jobs can understand lifecycle and blast radius. Avoid data, misc, or the dreaded cache:* containing billing state.

Serialization is part of correctness

Redis stores bytes, not objects. JSON is readable and interoperable, but it loses numeric types, key ordering assumptions, and language-specific behavior. MessagePack and protobuf are smaller but make debugging harder. Whatever you choose, version the payload if it has a long lifetime:

{"v": 2, "user_id": "u_934", "roles": ["support"], "expires_at": 1780000000}

The schema belongs in application documentation, not tribal memory.

Field notes

  • Redis atomicity is per command or per Lua script, not per application request.
  • DEBUG SLEEP belongs in a lab, never as a production troubleshooting trick.
  • A small object is not cheap if the access pattern scans or copies it constantly.
  • The Redis protocol is simple, but client behavior — pooling, pipelining, timeouts — is often where production bugs hide.

Was this lesson helpful?

Sign in to rate