Redis cheat sheet
96 one-line examples for Redis. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.
Connect and Ping
| Code | What it does |
|---|---|
redis-cli | Open the Redis command line client. |
redis-cli -h 127.0.0.1 -p 6379 | Connect to a host and port. |
redis-cli -a secret | Connect with a password. |
PING | Check that the server answers. |
ECHO "hello" | Return the same text back. |
SELECT 1 | Switch to database number one. |
QUIT | Close the client connection. |
DBSIZE | Count the keys in this database. |
INFO | Show server statistics and settings. |
INFO keyspace | Show key counts for each database. |
CLIENT LIST | List the connected clients. |
CONFIG GET maxmemory | Read one server setting. |
Strings
| Code | What it does |
|---|---|
SET user:1:name "Ana" | Store a text value under a key. |
GET user:1:name | Read the value of a key. |
SETNX lock 1 | Set a key only if it does not exist. |
SET key value EX 60 | Set a key that expires in 60 seconds. |
MSET a 1 b 2 | Set several keys in one call. |
MGET a b | Read several keys in one call. |
APPEND log "line" | Add text to the end of a value. |
STRLEN name | Return the length of a value. |
INCR visits | Add one to a number value. |
INCRBY visits 5 | Add five to a number value. |
DECR visits | Subtract one from a number value. |
GETSET name "Bo" | Set a new value and return the old one. |
Keys and Expiry
| Code | What it does |
|---|---|
KEYS user:* | List keys that match a pattern. |
SCAN 0 MATCH user:* COUNT 100 | List keys in small safe steps. |
EXISTS name | Check if a key exists. |
TYPE name | Show the data type of a key. |
DEL name | Delete one key. |
UNLINK name | Delete a key in the background. |
RENAME old new | Give a key a new name. |
EXPIRE name 300 | Delete a key after 300 seconds. |
TTL name | Show the seconds left before expiry. |
PERSIST name | Remove the expiry from a key. |
RANDOMKEY | Return one random key. |
FLUSHDB | Delete all keys in this database. |
Lists
| Code | What it does |
|---|---|
RPUSH queue "job1" | Add an item to the end of a list. |
LPUSH queue "job0" | Add an item to the start of a list. |
LRANGE queue 0 -1 | Read all items in a list. |
LLEN queue | Count the items in a list. |
LPOP queue | Remove and return the first item. |
RPOP queue | Remove and return the last item. |
LINDEX queue 0 | Read one item by position. |
LTRIM queue 0 9 | Keep only the first ten items. |
BLPOP queue 5 | Wait five seconds for a new item. |
LREM queue 0 "job1" | Remove all items equal to a value. |
LINSERT queue BEFORE a b | Insert an item before another item. |
RPOPLPUSH a b | Move the last item of one list to another. |
Hashes
| Code | What it does |
|---|---|
HSET user:1 name "Ana" | Set one field in a hash. |
HGET user:1 name | Read one field from a hash. |
HMSET user:1 age 30 city "Oslo" | Set several fields in one call. |
HMGET user:1 name age | Read several fields in one call. |
HGETALL user:1 | Read all fields and values. |
HDEL user:1 city | Delete one field from a hash. |
HEXISTS user:1 name | Check if a field exists. |
HKEYS user:1 | List all field names. |
HVALS user:1 | List all field values. |
HLEN user:1 | Count the fields in a hash. |
HINCRBY user:1 age 1 | Add one to a number field. |
HSCAN user:1 0 | List fields in small safe steps. |
Sets
| Code | What it does |
|---|---|
SADD tags "redis" | Add a member to a set. |
SREM tags "redis" | Remove a member from a set. |
SMEMBERS tags | List all members of a set. |
SISMEMBER tags "redis" | Check if a value is a member. |
SCARD tags | Count the members in a set. |
SPOP tags | Remove and return a random member. |
SRANDMEMBER tags 2 | Return two random members. |
SINTER a b | Return members present in both sets. |
SUNION a b | Return members present in either set. |
SDIFF a b | Return members only in the first set. |
SINTERSTORE out a b | Save the shared members to a new set. |
SMOVE a b "redis" | Move a member from one set to another. |
Sorted Sets
| Code | What it does |
|---|---|
ZADD board 10 "ana" | Add a member with a score. |
ZSCORE board "ana" | Read the score of a member. |
ZRANGE board 0 -1 | List members from low to high score. |
ZREVRANGE board 0 2 | List the top three members. |
ZRANGE board 0 -1 WITHSCORES | List members with their scores. |
ZINCRBY board 5 "ana" | Add five to a member score. |
ZRANK board "ana" | Show the low to high position. |
ZREVRANK board "ana" | Show the high to low position. |
ZCARD board | Count the members in a sorted set. |
ZREM board "ana" | Remove a member from a sorted set. |
ZCOUNT board 0 10 | Count members within a score range. |
ZRANGEBYSCORE board 0 10 | List members within a score range. |
Pub/Sub and Pipes
| Code | What it does |
|---|---|
SUBSCRIBE news | Listen for messages on a channel. |
PUBLISH news "hello" | Send a message to a channel. |
PSUBSCRIBE news:* | Listen on channels that match a pattern. |
UNSUBSCRIBE news | Stop listening on a channel. |
PUBSUB CHANNELS | List the active channels. |
MULTI | Start a block of queued commands. |
EXEC | Run all queued commands at once. |
DISCARD | Cancel the queued commands. |
WATCH balance | Watch a key for changes. |
EVAL "return 1" 0 | Run a short Lua script. |
redis-cli --pipe < data.txt | Load many commands from a file. |
redis-cli --latency | Measure the round trip time. |