Skip to content
Sign inGet all access
Chapter 1·10 minFree

Getting Started

A server in one command, two commands to use it, and the model underneath.

Redis is usually called a cache. It is a server holding data structures in memory, and a cache is one use for that.

Running Redis in one line

No install, no configuration file, no schema:

terminal
docker run -d --name rd -p 6379:6379 redis:8-alpine
docker exec -it rd redis-cli PING
Output$ docker run -d --name rd -p 6379:6379 redis:8-alpine$ docker exec -it rd redis-cli PINGPONG

redis-cli ships inside the image. Every snippet runs against that container.

redis-cli

With no arguments it opens a prompt showing host, port and database number:

terminal
docker exec -it rd redis-cli
Output$ docker exec -it rd redis-cli127.0.0.1:6379> PINGPONG127.0.0.1:6379> exit

SET and GET

The two commands you already know:

SET city London
GET city
OutputOK"London"

SET always succeeds. Writing over an existing key replaces it, with no warning:

SET city London
SET city Paris
GET city
OutputOKOK"Paris"

Everything is a key

A key is the only way in. No tables, no columns, no asking which keys hold what:

SET user:1138:name ada
SET user:1138:email [email protected]
GET user:1138:name
GET user:1138:email
OutputOKOK"ada""[email protected]"

Each key has one type. The wrong command is an error, not a conversion:

SET city London
LPUSH city Paris
OutputOK(error) WRONGTYPE Operation against a key holding the wrong kind of value

WRONGTYPE usually means two parts of your code disagree about what a key holds.

TYPE, EXISTS, DEL

Three commands for inspecting what you created:

SET city London
TYPE city
EXISTS city
DEL city
EXISTS city
OutputOKstring(integer) 1(integer) 1(integer) 0

DEL returns how many it removed, so a missing key is not an error:

SET a 1
SET b 2
DEL a b missing
OutputOKOK(integer) 2

Reading a reply

Five reply shapes cover almost everything, and redis-cli labels them:

SET n 10
GET n
INCR n
EXISTS n
GET missing
TYPE missing
OutputOK"10"(integer) 11(integer) 1(nil)none

INFO and DBSIZE

DBSIZE counts the keys in the current database:

DBSIZE
SET a 1
SET b 2
DBSIZE
Output(integer) 0OKOK(integer) 2

INFO reports on the server itself, in sections:

terminal
docker exec -it rd redis-cli INFO server | head -3
Output$ docker exec -it rd redis-cli INFO server | head -3# Serverredis_version:8.12.0redis_git_sha1:00000000

INFO memory and INFO stats are the ones worth knowing; chapter 10 reads both.

FLUSHALL

It empties every database, immediately:

SET a 1
SET b 2
DBSIZE
FLUSHALL
DBSIZE
OutputOKOK(integer) 2OK(integer) 0