TechSchool
Home / Cheat sheets / Redis

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

CodeWhat it does
redis-cliOpen the Redis command line client.
redis-cli -h 127.0.0.1 -p 6379Connect to a host and port.
redis-cli -a secretConnect with a password.
PINGCheck that the server answers.
ECHO "hello"Return the same text back.
SELECT 1Switch to database number one.
QUITClose the client connection.
DBSIZECount the keys in this database.
INFOShow server statistics and settings.
INFO keyspaceShow key counts for each database.
CLIENT LISTList the connected clients.
CONFIG GET maxmemoryRead one server setting.

Strings

CodeWhat it does
SET user:1:name "Ana"Store a text value under a key.
GET user:1:nameRead the value of a key.
SETNX lock 1Set a key only if it does not exist.
SET key value EX 60Set a key that expires in 60 seconds.
MSET a 1 b 2Set several keys in one call.
MGET a bRead several keys in one call.
APPEND log "line"Add text to the end of a value.
STRLEN nameReturn the length of a value.
INCR visitsAdd one to a number value.
INCRBY visits 5Add five to a number value.
DECR visitsSubtract one from a number value.
GETSET name "Bo"Set a new value and return the old one.

Keys and Expiry

CodeWhat it does
KEYS user:*List keys that match a pattern.
SCAN 0 MATCH user:* COUNT 100List keys in small safe steps.
EXISTS nameCheck if a key exists.
TYPE nameShow the data type of a key.
DEL nameDelete one key.
UNLINK nameDelete a key in the background.
RENAME old newGive a key a new name.
EXPIRE name 300Delete a key after 300 seconds.
TTL nameShow the seconds left before expiry.
PERSIST nameRemove the expiry from a key.
RANDOMKEYReturn one random key.
FLUSHDBDelete all keys in this database.

Lists

CodeWhat 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 -1Read all items in a list.
LLEN queueCount the items in a list.
LPOP queueRemove and return the first item.
RPOP queueRemove and return the last item.
LINDEX queue 0Read one item by position.
LTRIM queue 0 9Keep only the first ten items.
BLPOP queue 5Wait five seconds for a new item.
LREM queue 0 "job1"Remove all items equal to a value.
LINSERT queue BEFORE a bInsert an item before another item.
RPOPLPUSH a bMove the last item of one list to another.

Hashes

CodeWhat it does
HSET user:1 name "Ana"Set one field in a hash.
HGET user:1 nameRead one field from a hash.
HMSET user:1 age 30 city "Oslo"Set several fields in one call.
HMGET user:1 name ageRead several fields in one call.
HGETALL user:1Read all fields and values.
HDEL user:1 cityDelete one field from a hash.
HEXISTS user:1 nameCheck if a field exists.
HKEYS user:1List all field names.
HVALS user:1List all field values.
HLEN user:1Count the fields in a hash.
HINCRBY user:1 age 1Add one to a number field.
HSCAN user:1 0List fields in small safe steps.

Sets

CodeWhat it does
SADD tags "redis"Add a member to a set.
SREM tags "redis"Remove a member from a set.
SMEMBERS tagsList all members of a set.
SISMEMBER tags "redis"Check if a value is a member.
SCARD tagsCount the members in a set.
SPOP tagsRemove and return a random member.
SRANDMEMBER tags 2Return two random members.
SINTER a bReturn members present in both sets.
SUNION a bReturn members present in either set.
SDIFF a bReturn members only in the first set.
SINTERSTORE out a bSave the shared members to a new set.
SMOVE a b "redis"Move a member from one set to another.

Sorted Sets

CodeWhat it does
ZADD board 10 "ana"Add a member with a score.
ZSCORE board "ana"Read the score of a member.
ZRANGE board 0 -1List members from low to high score.
ZREVRANGE board 0 2List the top three members.
ZRANGE board 0 -1 WITHSCORESList 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 boardCount the members in a sorted set.
ZREM board "ana"Remove a member from a sorted set.
ZCOUNT board 0 10Count members within a score range.
ZRANGEBYSCORE board 0 10List members within a score range.

Pub/Sub and Pipes

CodeWhat it does
SUBSCRIBE newsListen for messages on a channel.
PUBLISH news "hello"Send a message to a channel.
PSUBSCRIBE news:*Listen on channels that match a pattern.
UNSUBSCRIBE newsStop listening on a channel.
PUBSUB CHANNELSList the active channels.
MULTIStart a block of queued commands.
EXECRun all queued commands at once.
DISCARDCancel the queued commands.
WATCH balanceWatch a key for changes.
EVAL "return 1" 0Run a short Lua script.
redis-cli --pipe < data.txtLoad many commands from a file.
redis-cli --latencyMeasure the round trip time.
More on TechSchool
See the Redis interview questions Python JavaScript TypeScript HTML CSS React Vue 3 Node.js Java PHP C C++ C# Go SQL MongoDB Linux Git Docker Data Structures