Hello Redis: keys, values, first commands
Redis is one big dictionary that lives in RAM. You send it a command, it looks up a key, and it answers in well under a millisecond.
One dictionary in memory
Every key is a string. Every value has a type. The type decides which commands work on it.
GET on a list fails with WRONGTYPE.One command, start to finish
- redis-cli sends SET greeting hello as plain text over TCP port 6379.
- Redis parses it: command SET, two arguments.
- One thread executes it: greeting → "hello" goes into the in-memory table. No disk write on this path.
- Redis replies OK. On one machine the round trip is far below a millisecond.
- GET greeting walks the same path and reads the same table: "hello".
Run it yourself
Real Redis in 10 seconds
docker run -d --name redis -p 6379:6379 redis:7
docker exec -it redis redis-cli
127.0.0.1:6379> PING
PONG
Or use the lab below
It is a small simulator of the common commands. Click a chip or type. The right side draws every key, its type, and its TTL.
Read the reply
Six reply shapes cover almost everything.
OKsimple status: it worked(integer) 3a count or a number"hello"a string value(nil)no such key — not an error1) "a"
2) "b"an array of replies(error) WRONGTYPEwrong command for this typeCommon operations
| SET / GET | Write and read one value. SET k v EX 60 adds a TTL, NX only if missing. |
|---|---|
| MSET / MGET | Many keys in one round trip. Missing keys come back as (nil). |
| INCR / INCRBY / DECR | Atomic counters on a numeric string. A missing key counts as 0. |
| APPEND / STRLEN | Grow a string, measure it. |
| DEL / UNLINK | Remove keys. UNLINK frees big values in the background. |
| EXISTS / TYPE | Is it there? What shape is it? |
| KEYS / SCAN | Find keys by pattern. KEYS blocks on big databases — see chapter 2. |
Quick check
GET user:99 returns (nil). Did the command fail?
Show answer
No. (nil) means “no such key”. Errors start with (error).
You run
GET queue:emails on a list. What happens?
Show answer
WRONGTYPE. Lists are read with LRANGE, LPOP, and friends. Try it in the lab: RPUSH q a, then GET q.