redis-cli is the command-line client for Redis (and Valkey). It’s the quickest way to inspect what your application stores, debug cache behaviour, and check server health.

Connect

redis-cli                                   # localhost:6379
redis-cli -h 10.0.0.5 -p 6379
redis-cli -a 'password'                     # requirepass (shows a warning; see below)
redis-cli --user app --pass 'password'      # ACL user
redis-cli -n 2                              # database 2
redis-cli --tls -h redis.example.com -p 6380 --cacert ca.crt
redis-cli -u redis://app:[email protected]:6379/0

To keep passwords out of shell history and process lists, set REDISCLI_AUTH or type AUTH inside the session:

export REDISCLI_AUTH='password'
redis-cli

Run a single command non-interactively: redis-cli PING.

Keys

EXISTS user:42
TYPE user:42
DEL user:42
UNLINK big:key                 # delete in the background (non-blocking)
RENAME old new
EXPIRE session:abc 3600        # TTL in seconds
TTL session:abc                # -1 = no expiry, -2 = doesn't exist
PERSIST session:abc            # remove expiry
DBSIZE                         # number of keys in the current db

Find keys safely: SCAN, not KEYS

KEYS pattern blocks Redis while it walks the entire keyspace — on a busy production server it can freeze every client for seconds. Use SCAN:

redis-cli --scan --pattern 'session:*' | head
redis-cli --scan --pattern 'cache:*' | wc -l
redis-cli --scan --pattern 'tmp:*' | xargs -r -n 500 redis-cli UNLINK

Strings

SET greeting "hello"
SET lock:job1 1 NX EX 30       # only if not exists, expires in 30 s (simple lock)
GET greeting
INCR page:views
INCRBY credits:42 10
MSET a 1 b 2
MGET a b
GETDEL token:abc

Hashes (objects)

HSET user:42 name "Alice" plan "pro"
HGET user:42 name
HGETALL user:42
HINCRBY user:42 logins 1
HDEL user:42 plan

Lists (queues)

LPUSH jobs '{"id":1}'
RPOP jobs
LRANGE jobs 0 9
LLEN jobs
BRPOP jobs 5                   # blocking pop, wait up to 5 s

Sets

SADD tags:post1 linux redis docker
SMEMBERS tags:post1
SISMEMBER tags:post1 redis
SINTER tags:post1 tags:post2
SCARD tags:post1

Sorted sets (leaderboards, rankings)

ZADD leaderboard 1500 alice 1320 bob
ZINCRBY leaderboard 25 bob
ZREVRANGE leaderboard 0 9 WITHSCORES      # top 10
ZRANK leaderboard alice
ZRANGEBYSCORE leaderboard 1000 2000

Server information

INFO                     # everything
INFO memory              # used_memory_human, maxmemory, fragmentation
INFO stats               # keyspace_hits, keyspace_misses, evicted_keys
INFO persistence
INFO replication
CONFIG GET maxmemory*
CLIENT LIST
SLOWLOG GET 10           # slowest recent commands
MEMORY USAGE user:42     # bytes used by one key

Hit ratio = keyspace_hits / (keyspace_hits + keyspace_misses).

Diagnostics from the shell

redis-cli --latency                 # continuous latency sampling
redis-cli --latency-history -i 5
redis-cli --bigkeys                 # largest keys per type (uses SCAN)
redis-cli --memkeys                 # keys using the most memory
redis-cli --stat                    # live stats: keys, memory, clients, ops
redis-cli MONITOR                   # every command in real time — heavy, use briefly

--bigkeys often explains memory problems: one forgotten list or hash with millions of entries.

Dangerous commands

FLUSHDB          # delete everything in the current database
FLUSHALL         # delete everything in all databases
CONFIG SET …     # change config at runtime
DEBUG …, SHUTDOWN

In production, restrict them with ACLs (-@dangerous for app users) or rename-command in redis.conf. See installing and securing Redis.

Pipelines and scripts

cat commands.txt | redis-cli --pipe     # bulk loading
redis-cli EVAL "return redis.call('INCR', KEYS[1])" 1 counter

Related: Redis persistence, Redis maxmemory and eviction and Redis pub/sub.

  • Redis
  • Command Line
  • Cheat Sheet