redis starters, one command per page
Eighteen steps for developers who installed Redis, cached one string and stopped: the key-value model, SET and GET with their options, INCR as an atomic counter, EXPIRE and TTL for keys that delete themselves, lists that push and range from either end, sets that ignore duplicates, hashes as rows, sorted sets that sort themselves, DEL and EXISTS, SCAN instead of KEYS, and pub/sub, one command per page.
A diagram, the classic trap, and one thing to go try this week. That's a page.
redis starters, one command per page
Eighteen steps for developers who installed Redis, cached one string and stopped: the key-value model, SET and GET with their options, INCR as an atomic counter, EXPIRE and TTL for keys that delete themselves, lists that push and range from either end, sets that ignore duplicates, hashes as rows, sorted sets that sort themselves, DEL and EXISTS, SCAN instead of KEYS, and pub/sub, one command per page.
Set in Space Grotesk, Inter and JetBrains Mono (SIL Open Font License).
Redis behaviour checked against the official command reference and develop guides at redis.io (docs/latest/commands and docs/latest/develop), all fetched and read during this build. Command syntax, defaults, return values and edge cases are named as those documents name them. Teaching conventions (one command a page) are named as conventions. This book quotes no verbatim passages and is an independent guide not affiliated with or endorsed by Redis Ltd.
General information only. Not professional advice; verify against your own Redis version and the current documentation.
Your purchase is for personal use only. You do not have redistribution rights: please do not share, resell, or republish this book or its pages.
© 2026 Steve Hodgkiss. All rights reserved. Personal use only; no redistribution rights.
Edition 1.0 · stevehodgkiss.net
Contents
Redis is an in-memory data structure store that can be used as a database, cache, streaming engine and message broker. Data lives in a keyspace: each key names one value, and commands operate on those keys. Per the redis.io develop quick starts.
One verb, one key
Let's say Redis is a cache. It can be, and it's four other things.
The docs list the jobs: database, cache, streaming engine, message broker. Under all of them sits one model: a keyspace, a flat map where every key names one value. No tables, no joins. You point a command at a key and it does one thing well.
One key, one value, one verb at a time. That's the whole shape.
Draw your app's data as ten keys with values on paper this week. If a value needs two names to be found, you've found your first design decision.
INCR key increments the integer value of a key by one, treating a missing key as 0 before the operation, and returns the value after the increment. It is limited to 64 bit signed integers and returns an error if the value cannot be represented as an integer. Per the redis.io INCR documentation.
INCR
Let's say you count with GET, add one in code, and SET it back. Three round trips and a race.
INCR key does the whole thing on the server: add one, return the new value. Two clients INCR at once and both counts land, no lock, no lost update. A missing key starts at 0, so the first INCR answers 1. It's a string operation, Redis has no separate integer type, and it's limited to 64 bit signed values.
Counters, rate limits, view counts: one command, no race.
Rate limit something this week: INCR a key per user per minute and treat the first reply, 1, as your signal. That's the counter half of a limiter built.
EXPIRE key seconds sets a timeout on a key; after the timeout expires, the key is automatically deleted. A key with a timeout is volatile. The timeout is cleared by commands that delete or overwrite the whole value, such as DEL, SET and GETSET, while INCR, LPUSH and HSET leave it untouched; PERSIST makes the key persistent again. Per the redis.io EXPIRE documentation.
Keys that expire
Let's say you delete old sessions with a nightly job. Redis folds the job into the key.
EXPIRE key seconds arms a timer; when it hits zero the key is deleted automatically. A key with a timeout is volatile. The trap is in the docs: commands that overwrite the whole value, like SET, clear the timer, while INCR, LPUSH and HSET leave it alone. Your session refreshes with SET, its timer is cleared, and the key lives forever.
The timer follows the value. Overwrite it, and the timer is gone.
SET a key, EXPIRE it 60, then SET it again and run TTL. The -1 is the lesson: refresh with SET EX, or KEEPTTL.
SCAN iterates the set of keys in the database incrementally using a cursor. A full iteration starts with cursor 0 and calls SCAN until the returned cursor is 0 again. SCAN gives guarantees for full iterations: an element present in the collection from start to end of a full iteration is returned at some point. Per the redis.io SCAN documentation.
The scanner
Let's say you must visit every key in production. Take bites, not the whole meal.
SCAN walks the keyspace by cursor: start at 0, it returns some keys plus the next cursor, and you loop until the cursor comes back 0, a full iteration. The guarantee in the docs: anything present from start to end of a full iteration is returned at some point. Each call is small, so the walk leaves room for everything else.
Start at 0, stop at 0. Small bites, no blockade.
Write the six-line SCAN loop this week: cursor 0, take a bite, repeat until 0. It's the loop every cleanup script should already be.
The core Redis data types are strings, lists, sets, hashes and sorted sets, each with typed commands, plus generic key operations such as DEL, EXISTS, EXPIRE and SCAN, and pub/sub commands PUBLISH and SUBSCRIBE. Per the redis.io command reference groups.
The whole keyspace
Let's say Redis was a cache that also did pub/sub. It's one keyspace with five shapes of value.
Strings count and cache. Lists feed. Sets hold the unique. Hashes row the fields. Sorted sets rank. Around them, the same verbs apply everywhere: EXPIRE arms, DEL removes, SCAN walks, PUBLISH shouts. You now know the whole core: types, timers, traversal and messages.
One model, five shapes. Everything else is combinations.
Close the book and list the five types with their verbs. Whatever you miss is the page to reread, not a gap in the model.