Redis CLI
Connect to Redis, run commands, and debug issues with the redis-cli tool.
What is redis-cli?
redis-cli is the official command-line client for Redis. It ships with Redis and lets you connect to any Redis server, run commands interactively, pipe bulk data, and run diagnostic checks. It is the first tool most people reach for when debugging a Redis problem or exploring data.
Connecting to Redis
Running redis-cli with no arguments connects to 127.0.0.1 on port 6379, the default Redis address:
To connect to a different host or port, use -h and -p:
Connecting with a password
If your Redis server requires authentication, pass the password with -a:
Alternatively, connect first and then authenticate inside the session using the AUTH command:
The -a flag prints a warning to stderr by default. To suppress it, add --no-auth-warning.
Connecting with TLS
For Redis servers that require TLS (common with managed cloud providers), add the --tls flag:
If your server uses a self-signed certificate, you can provide a CA certificate:
Interactive mode vs. single-command mode
redis-cli works in two modes.
Interactive mode opens a prompt where you type commands one at a time:
Single-command mode runs one command and exits, which is useful in scripts:
You can also pass a series of commands non-interactively using -e to exit on errors, or pipe commands via stdin:
Basic commands
These are the commands you will use most often:
TTL returns seconds remaining. It returns -1 if the key has no expiry and -2 if the key does not exist.
Working with data types
Redis supports several data structures beyond plain strings.
Lists
Lists are ordered sequences of strings. Use them for queues, feeds, or logs:
LRANGE 0 -1 returns all elements. LPUSH inserts at the head, so the most recent item comes first.
Sets
Sets store unique, unordered values. Good for tags, memberships, and deduplication:
Hashes
Hashes store field-value pairs under a single key. Useful for representing objects:
Sorted sets
Sorted sets associate a score with each member. Use them for leaderboards, rate limiting, and time-ordered data:
REV returns members from highest to lowest score (requires Redis 6.2+).
Scanning keys safely
Avoid KEYS * in production. It blocks the server while scanning the entire keyspace, which can cause latency spikes on large databases.
Use SCAN instead. It returns results in batches using a cursor and does not block:
The first return value is the next cursor. Repeat the call with that cursor until it returns 0, which means the scan is complete.
For a full scan from the command line in one shot, use the --scan flag:
This handles the cursor iteration automatically.
The MONITOR command
MONITOR streams every command processed by the server in real time:
This is useful for debugging what an application sends to Redis. Be aware that MONITOR has a measurable performance impact. On a busy server, it can reduce throughput by 50% or more. Use it briefly on development or staging instances, not in production under load.
Press Ctrl+C to stop.
The INFO command
INFO returns a snapshot of server statistics:
The output is split into sections. To retrieve a specific section:
The keyspace section shows which databases have keys and their counts:
The memory section shows used_memory_human and maxmemory_human, which tells you how close you are to the memory limit.
Pipelining commands
The --pipe flag sends commands in bulk over a single connection without waiting for individual replies. This is significantly faster for loading large datasets:
Where commands.txt contains commands in Redis inline protocol format, one per line:
After the operation completes, redis-cli prints a summary:
Running Lua scripts with EVAL
Redis supports Lua scripts that run atomically on the server. EVAL executes a script inline:
The arguments after the script are: the number of keys, the keys themselves, then any additional arguments.
For a more realistic use case, a script that increments a counter and returns the new value only if it is below a limit:
For scripts you run repeatedly, load them with SCRIPT LOAD and call them with EVALSHA to avoid re-sending the script body each time.
Connecting to Redis Cluster
When connecting to a Redis Cluster, add the -c flag to enable cluster mode. This allows redis-cli to follow redirects when a key lives on a different node:
Without -c, the client will receive MOVED or ASK responses and fail to retrieve keys that are not on the node you connected to. With -c, it follows those redirects automatically.
Useful flags
| Flag | What it does |
|---|---|
--no-auth-warning | Suppresses the warning printed when using -a |
--resp3 | Uses the RESP3 protocol (Redis 6+), which adds richer type information |
--latency | Samples latency between client and server continuously |
--latency-history | Shows latency samples over time in 15-second windows |
--stat | Prints rolling server statistics (memory, keys, commands/sec) every second |
--bigkeys | Scans the keyspace and reports the largest key per data type |
The --stat flag is handy for a quick live view of server activity without parsing INFO output manually:
Connecting to remote Redis
For managed Redis services like AWS ElastiCache or Upstash, the connection command follows the same pattern:
ElastiCache does not allow connections from outside the VPC. To connect from your local machine, set up an SSH tunnel through a bastion host:
Then connect locally:
Quick reference
| Command / Flag | Description |
|---|---|
redis-cli | Connect to localhost:6379 |
redis-cli -h host -p port | Connect to a specific host and port |
redis-cli -a password | Connect with a password |
redis-cli --tls | Connect using TLS |
redis-cli -c | Connect in cluster mode |
PING | Test the connection |
SET key value | Set a string value |
GET key | Get a string value |
DEL key | Delete a key |
EXISTS key | Check if a key exists |
EXPIRE key seconds | Set a TTL on a key |
TTL key | Check remaining TTL |
TYPE key | Check the data type of a key |
SCAN 0 MATCH pattern COUNT n | Safely scan keys |
redis-cli --scan --pattern "prefix:*" | Scan keys from the shell |
MONITOR | Stream all commands in real time |
INFO section | Server statistics |
redis-cli --pipe | Send bulk commands via stdin |
redis-cli --stat | Rolling live stats |
redis-cli --bigkeys | Find the largest keys per type |