Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
What is Redis?  ›  Capstone Project

Starting Redis and Connecting with redis-cli

Connecting using Redis CLI 12 minStart a Redis server with Docker, a package manager or a downloaded build, then connect with redis-cli on port 6379 and check the connection with PING.

Starting Redis and Connecting with redis-cli

Before any of Wordcraft's counters or leaderboards can exist, you need a Redis server running and a way to type commands at it. There are several ways to install Redis, but they all end at the same place: a server listening on a port and a client that talks to it.

The rule

Start the server, connect with redis-cli, and PING until it says PONG.

Start a server. Pick whichever route suits your machine:

# Docker: nothing to install except Docker itself
docker run -d --name redis -p 6379:6379 redis

# macOS with Homebrew
brew install redis
brew services start redis

# Debian or Ubuntu
sudo apt install redis-server

# Any system: run the server binary directly
redis-server

Docker is the quickest to try and the easiest to remove. A package manager (Homebrew, apt) installs Redis as a system service that starts with the machine. You can also download the source release and build it with make, or use a pre-compiled binary if one is packaged for your platform; either way you then start the server by running redis-server. By default the server listens on port 6379.

Connect. redis-cli is the command-line client that ships with Redis. With no options it connects to 127.0.0.1 on port 6379; -h and -p choose another host or port. In Docker you can run the client inside the container:

docker exec -it redis redis-cli

Check it. Your first command should be PING:

127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET greeting hello
OK
127.0.0.1:6379> GET greeting
"hello"

The prompt shows the host and port you are connected to. Command names are not case-sensitive (ping works too), but key names are: greeting and Greeting are different keys.

The shortcut

When something does not work, walk the chain in order: server, port, client, PING. Every step depends on the one before.

redis-cli says "Could not connect"Step 1 of 4
  1. Server

    Is a Redis server running? docker ps (or brew services list) shows it.

Once connected, two habits save time. redis-cli remembers your command history (press the up arrow), and typing HELP SET shows the syntax of any command. You can also run a single command without entering the prompt, as in redis-cli GET greeting, which is handy in scripts. To stop a Docker server, run docker stop redis; docker start redis brings it back.

Drills

Answer fast — trust the rule.

quick drill

What does a healthy server reply to PING?

quick drill

Which port does Redis listen on by default?

quick drill

You ran docker run -d --name redis redis and redis-cli on your laptop cannot connect. What is missing?

quick drill

After SET greeting hello, what does GET Greeting reply?

Remember

  • Start the server, connect with redis-cli, and PING until it says PONG.
  • Debug in order: server, port, client, PING.
  • Commands ignore case; keys do not. The default port is 6379.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainYour first Redis session

Describe, step by step, how you would get a Redis server running on your laptop with Docker and prove you can store and read a value. Include the commands you would type and the reply you expect from each.