← Full reference

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

OptionWhat it does
-h host, -p portserver address
-u redis://[:password@]host:port/dbthe whole connection in one string
-a passwordpassword (better via the REDISCLI_AUTH variable — it won't end up in history or ps)
--user name --pass passwordlog in as an ACL user
-n 2work with database number 2 (there are 16 by default: 0–15)
--raw / --no-rawoutput without / with quotes and types, regardless of whether it's a terminal or a pipe
-ereturn code 1 if the command ended with an error
-xtake the command's last argument from stdin
-r N / -i secrepeat the command N times / with a pause between repeats
--scan --pattern 'pattern'list keys via SCAN without handling the cursor by hand
--bigkeysfind the biggest keys of each type
--statlive statistics: keys, memory, clients, requests

Commands

Keys (any type)

CommandWhat it does
EXISTS key…how many of the listed keys exist
DEL key… / UNLINK key…delete (UNLINK frees memory in the background)
TYPE keytype: string, list, hash, set, zset, stream; none — no such key
RENAME old newrename
EXPIRE key sec / PEXPIRE key mstime to live
EXPIREAT key unix_timedelete at a given moment
TTL key / PTTL keyremaining time in seconds / ms: -1 — no expiry, -2 — no such key
PERSIST keyremove the time to live
KEYS patternall 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

CommandWhat it does
SET key valuewrite; 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 keyread / read and delete
MSET k1 v1 k2 v2 / MGET k1 k2several keys at once
INCR / DECR / INCRBY key nchange a number atomically
APPEND key text / STRLEN keyappend to the end / string length

Lists

CommandWhat it does
LPUSH / RPUSH key elem…add to the head / tail
LPOP / RPOP keytake from the head / tail
LRANGE key 0 -1elements from one position to another (-1 — the last)
LLEN key / LINDEX key ilength / element by index
LREM key 0 elemremove all occurrences of an element

Hashes (an object with fields)

CommandWhat it does
HSET key field val [field val…]write fields
HGET key field / HGETALL keyone field / all fields and values
HKEYS / HVALS / HLEN keyfield names / values / number of fields
HEXISTS key field / HDEL key fielddoes a field exist / delete a field
HINCRBY key field nchange a numeric field

Sets and sorted sets

CommandWhat it does
SADD / SREM key elem…add / remove set members
SMEMBERS / SCARD keyall members (order not guaranteed) / their count
SISMEMBER key elemis the element a member
SINTER / SUNION / SDIFF k1 k2intersection / 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 maxmembers with a score in a range
ZSCORE / ZRANK key elemscore / position of a member
ZINCRBY key n elem / ZREM key elemchange the score / remove

Server

CommandWhat it does
PINGconnectivity check — replies PONG
SELECT nswitch to database n (in the interactive console)
DBSIZEnumber of keys in the current database
INFO [section]statistics: server, memory, clients, persistence, replication, keyspace
CONFIG GET parameterread a setting (* — all)
CONFIG SET parameter valuechange it on the fly
CONFIG REWRITEwrite the current settings to the config file
SAVE / BGSAVE / LASTSAVEsnapshot to disk: synchronous / in the background / time of the last one
CLIENT LISTconnected clients
MONITORshow every command arriving at the server (debugging)
FLUSHDB / FLUSHALLdelete 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 via SCAN or --scan.
  • SCAN returns a cursor and a batch of keys. The iteration is complete only when the cursor is 0 again. One batch is not the whole list, and keys in it may repeat.
  • SET resets the time to live. Overwriting a key with a plain SET makes TTL equal to -1. Need the previous expiry — add KEEPTTL or set it again.
  • Databases are isolated. A key written with -n 2 isn't visible in database 0, and KEYS/DBSIZE count only the current database.
  • An error doesn't change the exit code. On WRONGTYPE, NOAUTH or an unknown command redis-cli prints a message but exits with code 0. In scripts add -e or check the reply itself.
  • A command for the wrong type gives WRONGTYPE. GET on a list key won't work: find out the type with TYPE first.
  • CONFIG SET lasts until a restart. To keep a setting, edit the config file or run CONFIG 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.
  • -x takes all of stdin, including the final newline. echo hello | redis-cli -x SET k writes hello\n.
  • FLUSHALL doesn't ask for confirmation and deletes the data of all databases at once.

See also

psql · sqlite3 · service · exit-codes