Using Redis Well
Patterns, anti-patterns and the summary.
Every pattern here is built from commands you have already seen.
Cache-aside
Read the cache; on a miss, fetch and store with a TTL:
GET post:7
SET post:7 "{title}" EX 600
GET post:7
TTL post:7The TTL is what keeps a bug from becoming permanent.
Cache the misses too, or every request for absent data reaches the database:
SET post:404 "" EX 30
EXISTS post:404
STRLEN post:404When a popular key expires, every request misses at once. SET NX lets one through:
SET lock:post:7 1 NX EX 5
SET lock:post:7 1 NX EX 5Invalidation
DEL on write is the direct version — the next read repopulates:
SET post:7 "{title}" EX 600
DEL post:7
GET post:7The trap: a plain SET clears the TTL, making the key permanent:
SET cfg v1 EX 100
TTL cfg
SET cfg v2
TTL cfgKEEPTTL carries the expiry across a write:
SET cfg v1 EX 100
TTL cfg
SET cfg v2 KEEPTTL
TTL cfgTo invalidate many keys at once, put a version in the name and bump it:
SET cfg:v1:theme dark
GET cfg:v1:theme
GET cfg:v2:themeThere is no delete-by-prefix, so the orphans need a TTL of their own.
Sessions
A hash holds the fields; the TTL belongs to the key:
HSET session:abc user_id 42 ip 10.0.0.1
EXPIRE session:abc 1800
HGETALL session:abc
TTL session:abcWriting a field does not extend the session:
HSET session:abc user_id 42 ip 10.0.0.1
EXPIRE session:abc 1800
HSET session:abc theme dark
TTL session:abcFor a string, GETEX reads and slides in one command:
SET sess:a u42 EX 100
GETEX sess:a EX 1800
TTL sess:aIdempotency keys
SET NX asks “have I handled this?” and claims it in one step:
SET processed:order-99 1 NX EX 86400
SET processed:order-99 1 NX EX 86400The TTL is a retention policy — long enough to outlive any retry.
Rate limiting
INCR a key named for the window, expiring when the window does:
INCR rate:ip:10.0.0.1:min
EXPIRE rate:ip:10.0.0.1:min 60
INCR rate:ip:10.0.0.1:min
INCR rate:ip:10.0.0.1:min
TTL rate:ip:10.0.0.1:minINCR creates the key at 1. Compare its reply against your limit.
Four anti-patterns
KEYS scans everything in one blocking pass. On 500,000 keys:
redis-cli KEYS 'k:*' > /dev/nullAn untrimmed list grows until memory runs out. LTRIM caps it:
RPUSH log a b c d e
LTRIM log -3 -1
LRANGE log 0 -1Keys with no TTL never leave. INFO keyspace counts both:
redis-cli INFO keyspaceFourth: one value big enough that touching it is slow. A 100,000-member list is one key on one node:
redis-cli MEMORY USAGE hugeSplit by time or id — log:2026-11-27 rather than log.
Picking a structure
| Storing | Use | Chapter |
|---|---|---|
| A single value, a counter, a cached blob | String | 3 |
| An object with fields you read separately | Hash | 4 |
| A queue, a job list, an event log | List or Stream | 5 |
| Membership, tags, “has this happened” | Set | 6 |
| A leaderboard, anything ordered by score | Sorted Set | 6 |
| A flag per id, for millions of ids | Bitmap | 7 |
| An approximate unique count | HyperLogLog | 7 |
| Nested documents you query by field | JSON + Search | 8 |
Work down the list and stop at the first row that fits.
Looking things up
The server documents itself:
redis-cli COMMAND DOCS GETEX | head -8COMMAND COUNT461 commands; this course covered about eighty. The rest are variations on the same eight structures, which is why the structure is the part worth knowing.