How Do I Use the Redis CLI?
Table of Contents
redis-cli is Redis’s official interactive client — the fastest way to connect to a Redis instance, run commands, and see results immediately, without writing any application code. It’s also the tool you’ll reach for constantly when debugging: confirming what’s actually cached, checking a key’s TTL, or watching commands arrive in real time.
Connecting #
redis-cli # connects to localhost:6379 by default
redis-cli -h redis.internal -p 6380 # a specific host and port
redis-cli -h redis.internal -a 'yourpassword' # with authentication (see the note below)
redis-cli -u redis://user:pass@host:6379/0 # or as a single connection URL
Avoid -a with a plaintext password on a shared machine — it’s visible in the process list (ps aux) to anyone else on that host. Prefer the REDISCLI_AUTH environment variable instead, which doesn’t show up in process listings:
export REDISCLI_AUTH='yourpassword'
redis-cli -h redis.internal
Running a Docker-hosted Redis without installing the client locally #
If you’re running Redis via
Docker and don’t want to install redis-cli on the host at all:
docker run -d --name redis redis:7
docker exec -it redis redis-cli
Basic key commands #
SET session:abc123 "user-42"
GET session:abc123
EXISTS session:abc123
DEL session:abc123
TTL session:abc123 # seconds until expiry, -1 if no expiry set, -2 if the key doesn't exist
EXPIRE session:abc123 3600
TTL returning -2 is a common point of confusion — it doesn’t mean “no expiry,” it means the key doesn’t exist at all (possibly because it already expired). -1 means the key exists but has no expiry set.
Exploring what’s actually in an instance #
DBSIZE # total number of keys in the current logical database
KEYS session:* # find keys matching a pattern — see the warning below
SCAN 0 MATCH session:* COUNT 100 # the safe alternative to KEYS
TYPE session:abc123 # string, list, hash, set, zset, stream, etc.
Don’t run KEYS against a production instance. It’s O(n) across the entire keyspace and blocks Redis (which is single-threaded for command execution) while it runs — on a large instance this can cause real latency spikes for every other client. SCAN does the same job incrementally, without blocking, and is the command you actually want outside of a tiny local/dev instance.
Inspecting hashes, lists and sets #
HGETALL user:42 # all fields of a hash
LRANGE recent-events 0 -1 # an entire list, start to end
SMEMBERS active-users # all members of a set
ZRANGE leaderboard 0 9 WITHSCORES # top 10 of a sorted set, with scores
Watching commands live #
redis-cli MONITOR
Streams every command Redis processes, from every connected client, in real time — extremely useful for confirming what your application is actually sending, but also a meaningful performance cost on a busy production instance and a potential way to observe sensitive data in transit. Use it briefly, on a specific instance you’re actively debugging, not as a standing monitoring tool.
Selecting a database and flushing #
SELECT 1 # Redis supports multiple numbered logical databases (0-15 by default)
FLUSHDB # delete everything in the current database — no confirmation prompt
FLUSHALL # delete everything across all databases
Both flush commands execute immediately and irreversibly with no “are you sure” — confirm you’re connected to the instance and database you think you are (redis-cli -n <db> or SELECT first) before running either one anywhere near production.