Using redis-cli
redis-cli — the standard Redis client
Section titled “redis-cli — the standard Redis client”redis-cli is the command-line interface bundled with every Redis installation (and available inside the official Docker image). It supports two modes: interactive (a live REPL prompt) and inline (a single command passed directly from your shell).
Connecting options
Section titled “Connecting options”The most common flags:
| Flag | Default | Purpose |
|---|---|---|
-h host | 127.0.0.1 | Redis server hostname or IP |
-p port | 6379 | Redis server port |
-n db-number | 0 | Select a database number (0–15) on connect |
# Connect to the default host and portredis-cli
# Connect explicitly — useful when targeting a remote serverredis-cli -h 127.0.0.1 -p 6379
# Connect and immediately switch to database 1redis-cli -n 1Inline mode vs interactive mode
Section titled “Inline mode vs interactive mode”In inline mode you pass a single command as arguments to redis-cli and the result is printed to stdout — handy for scripts:
redis-cli PINGIn interactive mode you run redis-cli with no command arguments and get the 127.0.0.1:6379> prompt where you can type commands one by one.
PING — verifying the connection
Section titled “PING — verifying the connection”PING is the lightest possible check that the server is reachable:
127.0.0.1:6379> PINGPONGPass an optional message and Redis echoes it back — useful for testing round-trip latency in scripts:
127.0.0.1:6379> PING "hello world""hello world"ECHO — sending a string back
Section titled “ECHO — sending a string back”ECHO returns the argument unchanged. It is mainly useful for testing that the client encodes strings correctly:
127.0.0.1:6379> ECHO "Hello""Hello"SELECT — switching databases
Section titled “SELECT — switching databases”Redis provides 16 logical databases numbered 0 to 15. They share the same server process and memory but have completely separate keyspaces. The default is database 0.
127.0.0.1:6379> SELECT 1OK127.0.0.1:6379[1]> SET greeting "hi from db1"OK127.0.0.1:6379[1]> SELECT 0OK127.0.0.1:6379> GET greeting(nil)Notice the prompt changes to 127.0.0.1:6379[1] when you are on a database other than 0.
DBSIZE — counting keys
Section titled “DBSIZE — counting keys”DBSIZE returns the number of keys in the currently selected database:
127.0.0.1:6379> DBSIZE(integer) 5FLUSHDB — clearing the current database
Section titled “FLUSHDB — clearing the current database”FLUSHDB deletes every key in the current database. This is irreversible — use it only in development or test environments:
127.0.0.1:6379> FLUSHDBOK127.0.0.1:6379> DBSIZE(integer) 0Built-in HELP
Section titled “Built-in HELP”redis-cli ships with inline documentation. Use HELP followed by a command name or a category tag:
127.0.0.1:6379> HELP SET127.0.0.1:6379> HELP @stringHELP @string lists every command in the string group — a fast way to discover commands without leaving the terminal.
PING
ECHO "Hello"
SELECT 1
DBSIZE
SELECT 0