redis-cli — The Redis Command-Line Client
Practical guide to redis-cli — the interactive client for Redis and Valkey: manage keys and data structures, use pub/sub and monitor the server.
redis-cli is the interactive command-line client for Redis – and equally for its compatible fork Valkey. With it you connect to an in-memory data store and work directly with keys and data structures such as strings, hashes, lists, sets and sorted sets. Beyond that, you use pub/sub for messaging, monitor the server while it runs and import commands in bulk. This guide walks you through the commands you reach for daily, from establishing a connection through the typical data-type operations to monitoring.
Connect
redis-cli — Connect to local Redis (localhost:6379).
redis-cliredis-cli -h <host> -p <port> — Connect to a remote Redis server.
redis-cli -h redis.example.com -p 6379redis-cli -a <password> — Connect with authentication.
redis-cli -a mysecretpasswordredis-cli -u <uri> — Connect using a Redis URI.
redis-cli -u redis://user:pass@host:6379/0redis-cli -n <db> — Select a specific database number.
redis-cli -n 2redis-cli --tls — Connect with TLS encryption.
redis-cli --tls -h redis.example.comKeys & Strings
SET <key> <value> — Set a key to a string value.
SET user:1:name "John"GET <key> — Get the value of a key.
GET user:1:nameSET <key> <value> EX <seconds> — Set a key with expiration time.
SET session:abc123 "data" EX 3600DEL <key> — Delete one or more keys.
DEL user:1:name user:1:emailKEYS <pattern> — Find keys matching a pattern (use with caution in production).
KEYS user:*SCAN <cursor> MATCH <pattern> COUNT <n> — Iterate keys safely (production-friendly).
SCAN 0 MATCH user:* COUNT 100TTL <key> — Get remaining time to live in seconds.
TTL session:abc123INCR <key> — Increment a numeric value by 1.
INCR page:viewsHashes & Lists
HSET <key> <field> <value> — Set a field in a hash.
HSET user:1 name "John" email "john@example.com"HGET <key> <field> — Get a field value from a hash.
HGET user:1 nameHGETALL <key> — Get all fields and values from a hash.
HGETALL user:1LPUSH <key> <value> — Push a value to the left of a list.
LPUSH queue:tasks "task1"RPOP <key> — Pop a value from the right of a list.
RPOP queue:tasksLRANGE <key> <start> <stop> — Get a range of elements from a list.
LRANGE queue:tasks 0 -1LLEN <key> — Get the length of a list.
LLEN queue:tasksSets & Sorted Sets
SADD <key> <member> — Add a member to a set.
SADD tags:post:1 "redis" "database" "cache"SMEMBERS <key> — Get all members of a set.
SMEMBERS tags:post:1SISMEMBER <key> <member> — Check if a value is in a set.
SISMEMBER tags:post:1 "redis"ZADD <key> <score> <member> — Add a member with a score to a sorted set.
ZADD leaderboard 100 "player1" 200 "player2"ZRANGE <key> <start> <stop> WITHSCORES — Get members from a sorted set by rank.
ZRANGE leaderboard 0 -1 WITHSCORESServer & Monitoring
INFO — Show server information and statistics.
INFO serverINFO memory — Show memory usage statistics.
INFO memoryDBSIZE — Show the number of keys in the current database.
DBSIZEMONITOR — Stream all commands received by the server in real-time.
MONITORSLOWLOG GET <n> — Show the last N slow queries.
SLOWLOG GET 10FLUSHDB — Delete all keys in the current database.
FLUSHDBBGSAVE — Trigger a background save to disk.
BGSAVEPub/Sub & Scripting
SUBSCRIBE <channel> — Subscribe to a channel for messages.
SUBSCRIBE notificationsPUBLISH <channel> <message> — Publish a message to a channel.
PUBLISH notifications "New order received"PSUBSCRIBE <pattern> — Subscribe to channels matching a pattern.
PSUBSCRIBE user:*redis-cli --pipe < <file> — Bulk import Redis commands from a file.
redis-cli --pipe < commands.txt Conclusion
redis-cli is the Swiss Army knife for everyday work with Redis and Valkey: from a quick GET/SET through data-structure operations to live monitoring with MONITOR and INFO. Be careful with the destructive commands: FLUSHALL and FLUSHDB delete all keys (of an entire instance or the current database) irreversibly. Avoid KEYS * in production – the command blocks the server until every key has been scanned; use SCAN or redis-cli --scan instead. Prefer passing passwords through the REDISCLI_AUTH environment variable rather than in clear text, because -a shows up in your shell history and in the process list. Note that CONFIG SET changes runtime behavior live, too – a single typo hits the whole instance instantly.
Further Reading
- Redis documentation – official reference for redis-cli, commands and data types
- redis-cli command-line interface – in-depth guide to the client
- Valkey documentation – open, Redis-compatible alternative