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-cli

redis-cli -h <host> -p <port> — Connect to a remote Redis server.

redis-cli -h redis.example.com -p 6379

redis-cli -a <password> — Connect with authentication.

redis-cli -a mysecretpassword

redis-cli -u <uri> — Connect using a Redis URI.

redis-cli -u redis://user:pass@host:6379/0

redis-cli -n <db> — Select a specific database number.

redis-cli -n 2

redis-cli --tls — Connect with TLS encryption.

redis-cli --tls -h redis.example.com

Keys & 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:name

SET <key> <value> EX <seconds> — Set a key with expiration time.

SET session:abc123 "data" EX 3600

DEL <key> — Delete one or more keys.

DEL user:1:name user:1:email

KEYS <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 100

TTL <key> — Get remaining time to live in seconds.

TTL session:abc123

INCR <key> — Increment a numeric value by 1.

INCR page:views

Hashes & 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 name

HGETALL <key> — Get all fields and values from a hash.

HGETALL user:1

LPUSH <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:tasks

LRANGE <key> <start> <stop> — Get a range of elements from a list.

LRANGE queue:tasks 0 -1

LLEN <key> — Get the length of a list.

LLEN queue:tasks

Sets & 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:1

SISMEMBER <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 WITHSCORES

Server & Monitoring

INFO — Show server information and statistics.

INFO server

INFO memory — Show memory usage statistics.

INFO memory

DBSIZE — Show the number of keys in the current database.

DBSIZE

MONITOR — Stream all commands received by the server in real-time.

MONITOR

SLOWLOG GET <n> — Show the last N slow queries.

SLOWLOG GET 10

FLUSHDB — Delete all keys in the current database.

FLUSHDB

BGSAVE — Trigger a background save to disk.

BGSAVE

Pub/Sub & Scripting

SUBSCRIBE <channel> — Subscribe to a channel for messages.

SUBSCRIBE notifications

PUBLISH <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

  • artisan – command-line tool of the Laravel framework
  • cargo – package manager and build tool for Rust
  • composer – dependency manager for PHP