Foundations 1 of 5 · Visual guide

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.

The Redis keyspace Clients connect over TCP port 6379 to one Redis server. Inside, a table maps keys to typed values: a string, a hash, a list, a set, and a sorted set. redis-cli your app TCP :6379 Redis server · all data in RAM key value type greeting "hello" string user:42 name→Ada city→Pune hash queue:emails [ j3 | j2 | j1 ] list tags:post:9 { redis, cache } set board ada 90 · bea 70 sorted set
A key is a name. Its value is one of a few shapes. GET on a list fails with WRONGTYPE.

One command, start to finish

The life of SET and GET redis-cli sends SET greeting hello. Redis parses the command, executes it against the in-memory table, and replies OK. A later GET reads the same table. redis-cli SET greeting hello Redis parse SET · 2 args execute one thread RAM table greeting→"hello" OK no disk on this path · typically < 1 ms
  1. redis-cli sends SET greeting hello as plain text over TCP port 6379.
  2. Redis parses it: command SET, two arguments.
  3. One thread executes it: greeting → "hello" goes into the in-memory table. No disk write on this path.
  4. Redis replies OK. On one machine the round trip is far below a millisecond.
  5. GET greeting walks the same path and reads the same table: "hello".
Parse, execute, reply. Persistence to disk happens in the background, not on this path.

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 error
1) "a" 2) "b"an array of replies
(error) WRONGTYPEwrong command for this type

Common operations

The string commands you will type daily
SET / GETWrite and read one value. SET k v EX 60 adds a TTL, NX only if missing.
MSET / MGETMany keys in one round trip. Missing keys come back as (nil).
INCR / INCRBY / DECRAtomic counters on a numeric string. A missing key counts as 0.
APPEND / STRLENGrow a string, measure it.
DEL / UNLINKRemove keys. UNLINK frees big values in the background.
EXISTS / TYPEIs it there? What shape is it?
KEYS / SCANFind 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.

Redis basics: common questions

What is Redis used for?

Caching, sessions, rate limiting, counters, leaderboards, queues and pub/sub: anything that needs very fast reads and writes on small pieces of data.

Why is Redis so fast?

It keeps data in memory, uses simple data structures, and runs commands on one thread with no locking.