Redis CLI Basics
The Redis command line interface, called redis-cli, is the fastest way to talk to a Redis server. You type a command, press Enter, and Redis answers within a millisecond. Developers use it to learn commands, test ideas, inspect data, and fix problems on live servers.
The Walkie-Talkie Analogy
Think of redis-cli as a walkie-talkie. You press the button, speak one short message, and the other side replies at once. Each message is a single command, and each reply is a single answer.
You (redis-cli) Redis Server ┌──────────────┐ PING ┌──────────────┐ │ Type a │ ───────────────► │ Reads the │ │ command │ │ command │ │ │ ◄─────────────── │ Sends reply │ └──────────────┘ PONG └──────────────┘
Starting redis-cli
Run the tool with no options to connect to a server on your own machine at port 6379.
$ redis-cli 127.0.0.1:6379>
Add options to reach a remote server or to log in with a password.
$ redis-cli -h 192.168.1.20 -p 6380 -a MySecret
Passing a password with -a stores it in your shell history. Type AUTH after connecting when you work on shared machines.
Your First Commands
127.0.0.1:6379> PING PONG 127.0.0.1:6379> ECHO "Hello Redis" "Hello Redis" 127.0.0.1:6379> SET course "Redis" OK 127.0.0.1:6379> GET course "Redis"
PING checks that the server is alive. ECHO returns the text you send. SET and GET store and read a value.
Understanding Replies
Redis replies in a few simple shapes. Learning these shapes helps you read any command output.
Reply Type Example Output Meaning ──────────────── ──────────────────────── ────────────────────────── Simple string OK Command succeeded Bulk string "Redis" A stored value Integer (integer) 5 A count or number result Nil (nil) Key does not exist Array 1) "a" 2) "b" A list of results Error (error) ERR ... Command failed
Getting Help Inside the Shell
Type HELP followed by a command name to see its syntax and a short description.
127.0.0.1:6379> HELP SET SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|...] summary: Set the string value of a key
Type CLEAR to wipe the screen. Type QUIT or press Ctrl+D to leave.
Switching Databases
A Redis server offers 16 numbered databases, from 0 to 15. Database 0 is the default. Use SELECT to move between them.
127.0.0.1:6379> SELECT 2 OK 127.0.0.1:6379[2]>
The number in square brackets shows your current database.
Handy Command Line Options
Run One Command Without Entering the Shell
$ redis-cli GET course "Redis"
Repeat a Command
Use -r for the repeat count and -i for the delay in seconds.
$ redis-cli -r 5 -i 1 INCR visitors (integer) 1 (integer) 2 (integer) 3 (integer) 4 (integer) 5
Watch Live Statistics
$ redis-cli --stat
This option prints a new line of keys, memory, clients, and request counts every second.
Practical Example – Checking a Login Counter
Goal: Confirm that the app records every login. Step 1: redis-cli Step 2: GET logins:today → "152" Step 3: (user logs in on the website) Step 4: GET logins:today → "153" Result: The counter moved by one, so the app writes to Redis correctly.
Key Points
- redis-cli connects to a server and runs commands one at a time.
- Use -h, -p, and -a to set host, port, and password.
- Replies come as strings, integers, nil, arrays, or errors.
- HELP shows command syntax without leaving the shell.
- SELECT switches between the 16 default databases.
- Options such as -r, -i, and --stat save time during testing.
