Skip to content
Sign inGet all access
Chapter 11·14 minFree

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:7
Output(nil)OK"{title}"(integer) 600

The 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:404
OutputOK(integer) 1(integer) 0

When 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 5
OutputOK(nil)

Invalidation

DEL on write is the direct version — the next read repopulates:

SET post:7 "{title}" EX 600
DEL post:7
GET post:7
OutputOK(integer) 1(nil)

The trap: a plain SET clears the TTL, making the key permanent:

SET cfg v1 EX 100
TTL cfg
SET cfg v2
TTL cfg
OutputOK(integer) 100OK(integer) -1

KEEPTTL carries the expiry across a write:

SET cfg v1 EX 100
TTL cfg
SET cfg v2 KEEPTTL
TTL cfg
OutputOK(integer) 100OK(integer) 100

To 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:theme
OutputOK"dark"(nil)

There 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:abc
Output(integer) 2(integer) 11) "user_id"2) "42"3) "ip"4) "10.0.0.1"(integer) 1800

Writing 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:abc
Output(integer) 2(integer) 1(integer) 1(integer) 1800

For a string, GETEX reads and slides in one command:

SET sess:a u42 EX 100
GETEX sess:a EX 1800
TTL sess:a
OutputOK"u42"(integer) 1800

Idempotency 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 86400
OutputOK(nil)

The 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:min
Output(integer) 1(integer) 1(integer) 2(integer) 3(integer) 60

INCR 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:

terminal
redis-cli KEYS 'k:*' > /dev/null
Output$ redis-cli KEYS 'k:*' > /dev/null$ redis-cli SLOWLOG GET 11) 1) (integer) 0 2) (integer) 1795764290 3) (integer) 29299 4) 1) "KEYS" 2) "k:*"$ redis-cli SCAN 0 MATCH 'k:*' COUNT 10 > /dev/null$ redis-cli SLOWLOG LEN(integer) 0

An untrimmed list grows until memory runs out. LTRIM caps it:

RPUSH log a b c d e
LTRIM log -3 -1
LRANGE log 0 -1
Output(integer) 5OK1) "c"2) "d"3) "e"

Keys with no TTL never leave. INFO keyspace counts both:

terminal
redis-cli INFO keyspace
Output$ redis-cli INFO keyspace# Keyspacedb0:keys=500000,expires=2,avg_ttl=99927,subexpiry=0

Fourth: one value big enough that touching it is slow. A 100,000-member list is one key on one node:

terminal
redis-cli MEMORY USAGE huge
Output$ redis-cli LLEN huge(integer) 100000$ redis-cli MEMORY USAGE huge(integer) 793525

Split 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:

terminal
redis-cli COMMAND DOCS GETEX | head -8
Output$ redis-cli COMMAND DOCS GETEX | head -8getexsummaryReturns the string value of a key after setting its expiration time.since6.2.0groupstringcomplexity
COMMAND COUNT
Output(integer) 461

461 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.