Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 42 ยท Getting Started

Installing Redis and Using redis-cli

Install Redis with Docker, Linux, macOS or WSL, then learn redis-cli: PING, key naming, SELECT databases, INFO, MONITOR and the RESP wire protocol.

To follow along with real Redis you need two programs: redis-server, the server process, and redis-cli, a command-line client that sends commands and prints replies. Both come in the same package, so installing one installs the other. This lesson shows the common ways to get them, then tours the client, including INFO, MONITOR and the simple text protocol that every Redis client speaks, called RESP.

As in the rest of this track, commands that talk to a real server are shown as read-only blocks. The runnable examples at the end are JavaScript programs that implement the protocol and a small server, so you can see exactly what travels over the wire.

Installing Redis

Pick the route that matches your machine. Everything here is read-only, because it installs software on your computer.

Four ways to install

Docker One command, works everywhere.
Linux Use your package manager or the official repo.
macOS Homebrew installs and runs it.
Windows Use WSL 2 or Docker, not a native build.

Docker is the quickest way to get a clean, throwaway server.

1 / 2

Docker (any operating system with Docker installed):

Terminal
docker run -d --name redis -p 6379:6379 redis:8
docker exec -it redis redis-cli

The first command starts a server in the background and publishes port 6379. The second opens redis-cli inside the container. Pin a version tag (redis:8) instead of latest so upgrades are deliberate.

Linux (Debian or Ubuntu shown):

Terminal
sudo apt update
sudo apt install redis-server
redis-server --version
sudo systemctl status redis-server

Distribution packages can lag behind the newest release. If you need the latest version, Redis publishes its own package repositories, and the project documentation lists the steps for each distribution.

macOS with Homebrew:

Terminal
brew install redis
brew services start redis

Windows: Redis does not publish an official native Windows build. Install WSL 2 with Ubuntu and follow the Linux steps inside it, or use Docker Desktop.

You can also run the server in the foreground with redis-server, optionally followed by a configuration file path such as redis-server /etc/redis/redis.conf. Without a file it starts on port 6379 with default settings. By default it only accepts connections from the local machine. Never expose it to the internet unprotected (see securing Redis).

Redis in Docker, step by step

A container is the easiest way to get a clean Redis, and the easiest way to lose data if you skip the volume. Build the setup in four steps.

1. Run it with a volume. The official image stores data in /data. A named volume keeps it when you remove or upgrade the container:

Terminal
docker run -d --name redis \
  -p 127.0.0.1:6379:6379 \
  -v redis-data:/data \
  redis:8 redis-server --appendonly yes

127.0.0.1:6379:6379 publishes the port to your own machine only; plain -p 6379:6379 listens on every interface, which is dangerous without a password (see security). Everything after the image name is the command, here redis-server with --appendonly yes so writes are logged to disk.

2. Use your own redis.conf. Mount the file read-only and pass its path as the first argument:

Terminal
docker run -d --name redis \
  -p 127.0.0.1:6379:6379 \
  -v redis-data:/data \
  -v "$PWD/redis.conf":/usr/local/etc/redis/redis.conf:ro \
  redis:8 redis-server /usr/local/etc/redis/redis.conf

Keep dir /data in that file so persistence lands in the volume. The options inside are covered in the redis.conf tour.

3. Redis Stack and Redis 8. Older tutorials use the redis/redis-stack image, which bundles Redis with JSON, search, time series and probabilistic data types plus the RedisInsight web UI on port 8001 (docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack). Recent Redis releases (8 and later) include those types in the standard redis image, so check the image documentation for the version you pick. See search, JSON, time series and Bloom filters.

4. Compose and health checks. For an application with a database, put Redis in a Compose file next to it. The service name becomes the host name, so the app connects to redis:6379 instead of localhost, and a health check lets other services wait until Redis answers PING:

YAML
services:
  redis:
    image: redis:8
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
  app:
    build: .
    environment:
      REDIS_URL: redis://redis:6379
    depends_on:
      redis:
        condition: service_healthy

volumes:
  redis-data:

A hardened version with a memory limit, a password and stop_grace_period is in production deployment. Inside a container, localhost means the container itself, which is the most common reason an app "cannot reach Redis" in Compose.

The model below checks a docker run setup against these habits.

JS Lint a docker run setup for Redis

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Meeting redis-cli

Start the client with no arguments to connect to 127.0.0.1:6379, or name the server explicitly. A command given on the shell line runs once and exits, which is handy for scripts.

Terminal
redis-cli
redis-cli -h cache.internal -p 6380
redis-cli PING
redis-cli -n 2 SET color blue
redis-cli --scan --pattern 'user:*'

Inside the interactive prompt you type commands and see replies:

TEXT
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> PING "are you there"
"are you there"
127.0.0.1:6379> SET user:42:name "Ada"
OK
127.0.0.1:6379> GET user:42:name
"Ada"
127.0.0.1:6379> HELP SET

PING with no argument replies PONG, which is the standard "is the server alive" check. With an argument it echoes the text back. HELP followed by a command name prints its syntax. Command names are case-insensitive, keys and values are not.

Naming keys

Redis does not enforce any structure on key names, so a convention keeps a big keyspace understandable. The usual one is colon-separated segments from general to specific: user:42:name, cart:9f3a:items, cache:product:1001. Tools and dashboards often use the colon to group keys into folders.

Keep keys short but readable. A very long key wastes memory on every copy, and an unreadable one wastes your time. Both extremes are mistakes. Binary data is allowed in keys, but plain text keys are much easier to debug. More guidance comes in keys and expiry and choosing the right data type.

Databases and SELECT

One Redis server holds several numbered logical databases. By default there are 16, numbered 0 to 15, and every connection starts in database 0. SELECT switches the current connection, and the prompt shows the number when it is not zero:

TEXT
127.0.0.1:6379> SET color red
OK
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> GET color
(nil)
127.0.0.1:6379[1]> SET color blue
OK
127.0.0.1:6379[1]> SELECT 0
OK
127.0.0.1:6379> GET color
"red"

The same key name can exist in each database with a different value. These databases are only a key-space separator. They share the same memory, the same persistence files and the same access rules, and FLUSHALL wipes every one of them. Redis Cluster supports only database 0. For real separation between applications, use separate servers or key prefixes rather than database numbers.

Looking inside with INFO

INFO returns a long text report of name:value lines grouped in sections. Ask for one section at a time:

TEXT
127.0.0.1:6379> INFO server
# Server
redis_version:8.0.2
redis_mode:standalone
tcp_port:6379
uptime_in_seconds:5120

127.0.0.1:6379> INFO clients
# Clients
connected_clients:1
blocked_clients:0

127.0.0.1:6379> INFO keyspace
# Keyspace
db0:keys=3,expires=1,avg_ttl=48120,subexpiry=0

Numbers will differ on your machine, and newer versions add fields, so treat this output as a shape, not an exact transcript. Other useful sections are memory, persistence, stats and replication. INFO keyspace is a quick way to see how many keys each database has and how many carry an expiry. Monitoring and troubleshooting goes through the important fields.

Watching traffic with MONITOR

MONITOR streams every command the server receives in real time, with a timestamp, the database number and the client address:

TEXT
127.0.0.1:6379> MONITOR
OK
1731000000.123456 [0 127.0.0.1:50122] "SET" "user:42:name" "Ada"
1731000001.654321 [0 127.0.0.1:50122] "GET" "user:42:name"

It is excellent for answering "what is my application actually sending?" during development. It is also expensive: it copies every command to the monitoring client, which can cut throughput substantially. Use it briefly, and avoid it on a busy production server.

RESP: the protocol under the hood

Every client library, from Python to Java, talks to Redis using RESP, the REdis Serialization Protocol. It is deliberately simple: text lines ending in \r\n, where the first byte says what kind of value follows. These are the types you meet in the default version, RESP2:

First byte Type Example on the wire
+ Simple string +OK\r\n
- Error -ERR unknown command\r\n
: Integer :1000\r\n
$ Bulk string $5\r\nhello\r\n
* Array *2\r\n$1\r\na\r\n$1\r\nb\r\n

A bulk string has its length in bytes before the data, which is what makes values binary-safe: the data can contain anything, including \r\n. A missing value is a bulk string of length -1 ($-1\r\n), which clients show as nil. A command from the client is always an array of bulk strings, so SET k v is sent as *3\r\n$3\r\nSET\r\n$1\r\nk\r\n$1\r\nv\r\n. A connection can switch to RESP3 with the HELLO 3 command, which adds richer types such as maps, booleans and doubles. The basic idea stays the same.

Because RESP is so readable, you can speak it by hand. This is a real-Redis transcript using nc, read-only:

Terminal
printf 'PING\r\nSET k v\r\nGET k\r\n' | nc localhost 6379
# +PONG
# +OK
# $1
# v

The first form here is an inline command: plain text without the array framing, allowed for simple cases like this. Real clients always send the array form.

Write a RESP encoder and parser

The code below encodes commands into RESP and parses replies, including nested arrays. It escapes \r and \n when printing so you can see the framing. To keep it short it measures lengths in characters, which is the same as bytes for plain ASCII data, and it expects a complete message (a real client buffers partial reads).

JS A RESP encoder and parser

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The emulator output shows that a command is just an array of bulk strings, that a nested array (like the reply of SCAN) is simply an array inside an array, and that nil is $-1. A real client library does the same thing on a socket.

A tiny server that speaks RESP

Now put both halves together. The next example is a miniature server with 16 databases: it receives raw RESP text, parses the command, runs it against the currently selected database and returns raw RESP text. It also shows how SELECT separates key names. This is an emulator of the protocol and of a few commands, not a network server.

JS RESP in, RESP out, with SELECT

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Both color keys survive side by side, one per database, and SELECT 16 is rejected because only 0 to 15 exist by default. This mirrors real Redis behavior for these commands. The emulator does not model the network or the many other reply types.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does redis-cli PING print when the server is healthy?

    PING with no argument returns the simple string PONG, the standard liveness check.
  2. 2. Which statement about SELECT and logical databases is true?

    Logical databases share memory, persistence and access rules. They only separate key names, and Cluster supports just database 0.
  3. 3. How does a client send the command SET k v over RESP?

    Commands are arrays of bulk strings, each prefixed by its byte length, which makes values binary-safe.
  4. 4. Why should you avoid leaving MONITOR running on a busy production server?

    MONITOR streams every command to the client, which costs CPU and bandwidth. Use it briefly for debugging.

Summary

  • Install Redis with Docker, your Linux package manager, Homebrew, or WSL 2 on Windows; the package includes both redis-server and redis-cli.
  • redis-cli runs interactively or as a one-shot command; PING checks that the server is alive.
  • Name keys with colon-separated segments such as user:42:name.
  • SELECT switches among 16 logical databases that share memory and persistence; prefer key prefixes or separate servers for real isolation.
  • INFO reports server state by section, and MONITOR shows live commands but is costly.
  • RESP is a simple length-prefixed text protocol: clients send arrays of bulk strings and read simple strings, errors, integers, bulk strings and arrays.

Next, start storing real data with Strings and Counters.