redis-cli
Redis client — commands for strings, lists, hashes and sets, TTL, finding keys with SCAN, INFO and CONFIG
redis-cli sends commands to a Redis server — an in-memory "key → value" store. You can pass a command as arguments (the reply is printed and the client exits) or work in the interactive console.
Syntax
redis-cli [-h host] [-p port] [-n db_number] [COMMAND args…]
By default the client goes to 127.0.0.1:6379, database 0. Command names are case-insensitive: GET and get are the same; key names are not. Help on a command right in the console: HELP SET.
Client options
| Option | What it does |
|---|---|
-h host, -p port | server address |
-u redis://[:password@]host:port/db | the whole connection in one string |
-a password | password (better via the REDISCLI_AUTH variable — it won't end up in history or ps) |
--user name --pass password | log in as an ACL user |
-n 2 | work with database number 2 (there are 16 by default: 0–15) |
--raw / --no-raw | output without / with quotes and types, regardless of whether it's a terminal or a pipe |
-e | return code 1 if the command ended with an error |
-x | take the command's last argument from stdin |
-r N / -i sec | repeat the command N times / with a pause between repeats |
--scan --pattern 'pattern' | list keys via SCAN without handling the cursor by hand |
--bigkeys | find the biggest keys of each type |
--stat | live statistics: keys, memory, clients, requests |
Commands
Keys (any type)
| Command | What it does |
|---|---|
EXISTS key… | how many of the listed keys exist |
DEL key… / UNLINK key… | delete (UNLINK frees memory in the background) |
TYPE key | type: string, list, hash, set, zset, stream; none — no such key |
RENAME old new | rename |
EXPIRE key sec / PEXPIRE key ms | time to live |
EXPIREAT key unix_time | delete at a given moment |
TTL key / PTTL key | remaining time in seconds / ms: -1 — no expiry, -2 — no such key |
PERSIST key | remove the time to live |
KEYS pattern | all keys matching a pattern (*, ?, [ab]) — in a single pass |
SCAN cursor [MATCH pattern] [COUNT n] [TYPE type] | iterate over keys in batches |
Strings and counters
| Command | What it does |
|---|---|
SET key value | write; EX sec / PX ms — with expiry, NX — only if the key doesn't exist, XX — only if it does, KEEPTTL — keep the previous expiry |
GET key / GETDEL key | read / read and delete |
MSET k1 v1 k2 v2 / MGET k1 k2 | several keys at once |
INCR / DECR / INCRBY key n | change a number atomically |
APPEND key text / STRLEN key | append to the end / string length |
Lists
| Command | What it does |
|---|---|
LPUSH / RPUSH key elem… | add to the head / tail |
LPOP / RPOP key | take from the head / tail |
LRANGE key 0 -1 | elements from one position to another (-1 — the last) |
LLEN key / LINDEX key i | length / element by index |
LREM key 0 elem | remove all occurrences of an element |
Hashes (an object with fields)
| Command | What it does |
|---|---|
HSET key field val [field val…] | write fields |
HGET key field / HGETALL key | one field / all fields and values |
HKEYS / HVALS / HLEN key | field names / values / number of fields |
HEXISTS key field / HDEL key field | does a field exist / delete a field |
HINCRBY key field n | change a numeric field |
Sets and sorted sets
| Command | What it does |
|---|---|
SADD / SREM key elem… | add / remove set members |
SMEMBERS / SCARD key | all members (order not guaranteed) / their count |
SISMEMBER key elem | is the element a member |
SINTER / SUNION / SDIFF k1 k2 | intersection / union / difference |
ZADD key score elem… | add to a sorted set with a score |
ZRANGE key 0 -1 [WITHSCORES] [REV] | by ascending score (REV — descending) |
ZRANGEBYSCORE key min max | members with a score in a range |
ZSCORE / ZRANK key elem | score / position of a member |
ZINCRBY key n elem / ZREM key elem | change the score / remove |
Server
| Command | What it does |
|---|---|
PING | connectivity check — replies PONG |
SELECT n | switch to database n (in the interactive console) |
DBSIZE | number of keys in the current database |
INFO [section] | statistics: server, memory, clients, persistence, replication, keyspace |
CONFIG GET parameter | read a setting (* — all) |
CONFIG SET parameter value | change it on the fly |
CONFIG REWRITE | write the current settings to the config file |
SAVE / BGSAVE / LASTSAVE | snapshot to disk: synchronous / in the background / time of the last one |
CLIENT LIST | connected clients |
MONITOR | show every command arriving at the server (debugging) |
FLUSHDB / FLUSHALL | delete all keys in the current database / in all databases |
Examples
# is the server alive?
redis-cli PING
# write and read
redis-cli SET greeting hello
redis-cli GET greeting
# write with a 30-second time to live and check what's left
redis-cli SET token abc EX 30
redis-cli TTL token
# write only if the key doesn't exist yet (a simple lock)
redis-cli SET lock:job 1 NX EX 60
# time to live for an existing key
redis-cli EXPIRE greeting 3600
# an object as a hash
redis-cli HSET user:1 name ann role admin
redis-cli HGETALL user:1
# a queue on a list
redis-cli RPUSH jobs a b c
redis-cli LPOP jobs
redis-cli LRANGE jobs 0 -1
# safely list keys matching a pattern
redis-cli --scan --pattern 'session:*'
# how many keys in each database and how much memory is used
redis-cli INFO keyspace
redis-cli INFO memory | grep used_memory_human
# current value of a setting
redis-cli CONFIG GET maxmemory
# the same work, but in database 1
redis-cli -n 1 SET flag on
redis-cli -n 1 DBSIZE
# in a script: fail if the command returned an error
redis-cli -e GET greeting || echo 'Redis error'
# a value from a file
redis-cli -x SET config < settings.json
Pitfalls
KEYS *blocks the server until it has gone through all keys. On a large database that's a pause for every client. Outside training setups list keys viaSCANor--scan.SCANreturns a cursor and a batch of keys. The iteration is complete only when the cursor is0again. One batch is not the whole list, and keys in it may repeat.SETresets the time to live. Overwriting a key with a plainSETmakesTTLequal to-1. Need the previous expiry — addKEEPTTLor set it again.- Databases are isolated. A key written with
-n 2isn't visible in database 0, andKEYS/DBSIZEcount only the current database. - An error doesn't change the exit code. On
WRONGTYPE,NOAUTHor an unknown commandredis-cliprints a message but exits with code 0. In scripts add-eor check the reply itself. - A command for the wrong type gives
WRONGTYPE.GETon a list key won't work: find out the type withTYPEfirst. CONFIG SETlasts until a restart. To keep a setting, edit the config file or runCONFIG REWRITE(works only if the server was started with a config file).- Output to a terminal and to a pipe differ. In a terminal you see
(nil)and(integer) 1, in a pipe — bare values. Don't make scripts depend on the "pretty" format. -xtakes all of stdin, including the final newline.echo hello | redis-cli -x SET kwriteshello\n.FLUSHALLdoesn't ask for confirmation and deletes the data of all databases at once.
See also
psql · sqlite3 · service · exit-codes