TExcelTAD
Home › Redis › Redis Get Started

Redis Get Started

To follow along on your own machine you need two things: the Redis server (redis-server), which holds the data, and a client. The standard client is redis-cli, an interactive shell that ships with the server. This lesson shows how to install both, how to connect, and the first commands to try.

Installing Redis

Pick the method that matches your system. All of them give you redis-server and redis-cli.

bash
# Docker (any OS): start a server in the background on port 6379
docker run -d --name redis -p 6379:6379 redis:8

# Open redis-cli inside the container
docker exec -it redis redis-cli

# macOS with Homebrew
brew install redis
brew services start redis

# Ubuntu / Debian
sudo apt-get install redis-server
sudo systemctl enable --now redis-server

On Windows, the easiest options are Docker Desktop or WSL (Windows Subsystem for Linux) with the Ubuntu instructions above.

Tip: Docker is the cleanest way to try different versions: docker run --rm -p 6379:6379 redis:8 gives you a throwaway server that disappears when you stop it.

Connecting with redis-cli

By default redis-cli connects to 127.0.0.1 on port 6379. Options let you choose another host, port, password or database:

bash
redis-cli                                  # local server, port 6379
redis-cli -h cache.internal -p 6380        # another host and port
redis-cli -a 's3cret' --user app           # log in as an ACL user
redis-cli -n 2                             # use database number 2
redis-cli PING                             # run one command and exit
redis-cli --scan --pattern 'user:*'        # list keys safely (uses SCAN)

In interactive mode the prompt shows where you are connected, for example 127.0.0.1:6379>. In this course the prompt is shortened to redis>.

Your first commands

PING checks that the server is alive. ECHO returns its argument. DBSIZE counts the keys in the current database:

redis · try it yourself
PING
PING "are you there?"
ECHO "Redis says hi"
DBSIZE
SET language redis
DBSIZE
redis> PING
PONG
redis> PING "are you there?"
"are you there?"
redis> ECHO "Redis says hi"
"Redis says hi"
redis> DBSIZE
(integer) 0
redis> SET language redis
OK
redis> DBSIZE
(integer) 1

How redis-cli shows replies

Redis replies have a few basic types, and redis-cli prints each one in a recognisable way. You will see all of them throughout the course:

Reply type Printed as Example command
Simple string (status) OK, PONG, string SET, PING, TYPE
Bulk string "text" in quotes GET
Integer (integer) 3 INCR, DEL, EXISTS
Null (nil) GET on a missing key
Array 1) "a" 2) "b" LRANGE, KEYS
Empty array (empty array) LRANGE on a missing key
Error (error) ERR ... a wrong command or wrong type
redis · try it yourself
GET missing-key
LRANGE missing-list 0 -1
SET city Hanoi
GET city
INCR city
NOSUCHCOMMAND
redis> GET missing-key
(nil)
redis> LRANGE missing-list 0 -1
(empty array)
redis> SET city Hanoi
OK
redis> GET city
"Hanoi"
redis> INCR city
(error) ERR value is not an integer or out of range
redis> NOSUCHCOMMAND
(error) ERR unknown command 'NOSUCHCOMMAND'

The last two lines show errors: INCR refuses a value that is not a number, and an unknown command is rejected. An error never crashes the server or closes your connection; you simply get an error reply.

Quoting arguments

Arguments are separated by spaces. To put a space inside a value, wrap it in double quotes (which understand escapes like \n) or single quotes (taken literally):

redis · try it yourself
SET title "Learning Redis in Hanoi"
GET title
SET note 'single quotes keep \n as two characters'
GET note
SET lines "first\nsecond"
GET lines
redis> SET title "Learning Redis in Hanoi"
OK
redis> GET title
"Learning Redis in Hanoi"
redis> SET note 'single quotes keep \n as two characters'
OK
redis> GET note
"single quotes keep \\n as two characters"
redis> SET lines "first\nsecond"
OK
redis> GET lines
"first\nsecond"

In the single-quoted value the backslash and the n were stored as two ordinary characters, so redis-cli prints the backslash escaped as \\n. In the double-quoted value \n became a real newline, which redis-cli prints as \n so that the reply stays on one line.

Databases

A Redis server has 16 numbered databases (0 to 15) by default. SELECT switches between them; keys in one database are invisible in the others. Most applications use only database 0, and Redis Cluster supports only database 0, so prefer key prefixes (app1:..., app2:...) over database numbers to separate data.

redis · try it yourself
SET mode production
SELECT 1
GET mode
SET mode testing
SELECT 0
GET mode
redis> SET mode production
OK
redis> SELECT 1
OK
redis> GET mode
(nil)
redis> SET mode testing
OK
redis> SELECT 0
OK
redis> GET mode
"production"

Getting help

  • COMMAND DOCS SET returns the documentation of a command from the server itself.
  • In redis-cli, type HELP SET or HELP @string to see the syntax of a command or of a group.
  • The official command reference lives at redis.io/docs/latest/commands.

Test yourself

What does redis-cli PING print when the server is running?

  1. OK
  2. PONG
  3. (integer) 1
  4. HELLO

How does redis-cli show a reply for a key that does not exist with GET?

  1. ""
  2. (empty array)
  3. (nil)
  4. (error) ERR no such key

What is the recommended way to separate the data of two applications on one Redis server?

  1. Use database 0 and database 1
  2. Use key prefixes such as shop: and blog:
  3. Start keys with numbers
  4. Use different value types