Redis · lesson 1 of 14
Beginner
4Intermediate
7- 5 MULTI/EXEC Transactions and the WATCH-Based Optimistic Lock
- 6 Pub/Sub: Channels, Patterns, and the Message That Never Arrives
- 7 RDB vs AOF: What Survives a Hard Kill and What Doesn't
- 8 Replication: A Read-Only Replica, and What WAIT Actually Guarantees
- 9 ACL: a User Restricted to One Key Pattern and One Command
- 12 maxmemory and Eviction: What Redis Throws Away When It's Full
- 13 Cache-Aside with Redis: Stale Reads, Races and Stampedes
Advanced
3Also part of Redis
3Redis in Five Commands: SET, GET, TYPE, and a WRONGTYPE Error
Run a real Redis server in Docker in one command, then hit the error every new user eventually hits: calling GET on a key that isn't a string.
By SkillByExample · Published
A Redis server is a single Docker command away — no account, no cloud, no installer.
Running a real Redis server
docker run -d --name redis-lesson -p 6379:6379 redis:8-alpine
docker exec redis-lesson redis-cli PING
PONG
-p 6379:6379 publishes Redis’s default port to the host; docker exec ... redis-cli runs
the client that ships inside the same container, which is what every example in this track
uses. Every lesson assumes this container is running.
Strings: the simplest type
docker exec redis-lesson redis-cli SET book:1 "Dune"
docker exec redis-lesson redis-cli GET book:1
docker exec redis-lesson redis-cli EXISTS book:1
docker exec redis-lesson redis-cli EXISTS book:999
OK
Dune
1
0
SET and GET are the whole interface for a string — EXISTS returns 1 or 0, not a
boolean, because RESP (Redis’s wire protocol) has no boolean type; everything is an integer,
a string, or an array.
Every key has a type — and Redis enforces it
docker exec redis-lesson redis-cli LPUSH mylist a b c
docker exec redis-lesson redis-cli TYPE mylist
3
list
LPUSH created mylist as a list, not a string — TYPE confirms it. 3 is the list’s
length after the push, not an error code; LPUSH returns the new length on success.
The bug: calling the wrong command for the type
docker exec redis-lesson redis-cli GET mylist
WRONGTYPE Operation against a key holding the wrong kind of value
mylist is real and populated — TYPE just confirmed it — but GET only understands
strings. Redis doesn’t try to coerce a list into a string representation; it refuses outright,
by design, rather than returning something a caller might silently misinterpret.
The fix: use the command for the actual type
docker exec redis-lesson redis-cli LRANGE mylist 0 -1
c
b
a
LRANGE is the list-reading command, and 0 -1 means “the whole list.” Notice the order:
c, b, a — the reverse of how they were pushed. LPUSH inserts at the head of the
list each time, so the last value pushed (c) ends up first.
Practice
1. Check multiple keys for existence in a single command.
docker exec redis-lesson redis-cli SET a 1
docker exec redis-lesson redis-cli SET b 2
docker exec redis-lesson redis-cli EXISTS a b nonexistent
OK
OK
2
EXISTS accepts multiple key names and returns the count of how many exist — not one
result per key. Two out of three here, so 2.
2. Delete a key that doesn't exist and see what comes back.
docker exec redis-lesson redis-cli DEL nonexistent
0
DEL doesn’t error on a missing key — it returns the count of keys actually deleted, which is
0 when there was nothing to delete. Deleting is idempotent by design.
3. List every key currently in the database with KEYS, then clear it with FLUSHALL.
docker exec redis-lesson redis-cli KEYS "*"
docker exec redis-lesson redis-cli FLUSHALL
a
b
mylist
book:1
OK
KEYS "*" is fine here — a handful of keys in a learning container — but reads the entire
keyspace in one blocking pass, which is exactly why it’s unsafe on a production-sized
database. FLUSHALL wipes everything, useful for resetting between experiments.
Next: strings in depth — expiry, atomic counters, and the increment-on-a-non-number error.