Redis CheatSheet

Introduction

Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. This cheat sheet provides a quick reference for common Redis commands and operations.

CommandDescriptionExample
SET key valueSet the value of a keySET mykey "Hello"
GET keyGet the value of a keyGET mykey
DEL keyDelete a keyDEL mykey
INCR keyIncrement the value of a keyINCR counter
DECR keyDecrement the value of a keyDECR counter
HSET key field valueSet the value of a hash fieldHSET user:id name "John"
HGET key fieldGet the value of a hash fieldHGET user:id name
LPUSH key valuePush a value to the beginning of a listLPUSH mylist "World"
RPUSH key valuePush a value to the end of a listRPUSH mylist "Hello"
LPOP keyPop a value from the beginning of a listLPOP mylist
RPOP keyPop a value from the end of a listRPOP mylist
SADD key memberAdd a member to a setSADD myset "value"
SMEMBERS keyGet all members of a setSMEMBERS myset
ZADD key score memberAdd a member with a score to a sorted setZADD myzset 1 "one"
ZRANGE key start stopGet a range of members from a sorted setZRANGE myzset 0 -1
EXPIRE key secondsSet a key’s time to live in secondsEXPIRE mykey 60
PERSIST keyRemove the expiration of a keyPERSIST mykey
MSET key1 value1 key2 value2 ...Set multiple key-value pairs in one commandMSET key1 "value1" key2 "value2"
GETSET key valueSet the value of a key and return its old valueGETSET mykey "newvalue"
SCAN cursor [MATCH pattern] [COUNT count]Incrementally iterate over a set of keysSCAN 0 MATCH prefix:* COUNT 10
INFOGet information and statistics about the serverINFO
FLUSHDBRemove all keys from the current databaseFLUSHDB
BGSAVEAsynchronously save the dataset to diskBGSAVE
BGREWRITEAOFAsynchronously rewrite the append-only fileBGREWRITEAOF
CONFIG GET parameterGet the value of a configuration parameterCONFIG GET dir
CONFIG SET parameter valueSet the value of a configuration parameterCONFIG SET dir /var/redis
MONITORListen for all requests received by the serverMONITOR
——————————————–———————————————————————————————-
ZSCORE key memberGet the score associated with a member in a sorted setZSCORE myzset "one"
ZRANK key memberGet the rank of a member in a sorted setZRANK myzset "one"
ZREM key member [member ...]Remove one or more members from a sorted setZREM myzset "one"
HGETALL keyGet all fields and values from a hashHGETALL user:id
HMSET key field1 value1 [field2 value2 ...]Set multiple hash fields and valuesHMSET user:id name "John" age 30
HDEL key field1 [field2 ...]Delete one or more fields from a hashHDEL user:id age
SREM key member [member ...]Remove one or more members from a setSREM myset "value"
ZCOUNT key min maxCount the members in a sorted set within a score rangeZCOUNT myzset 0 100
ZREVRANGE key start stop [WITHSCORES]Get a range of members from a sorted set in reverse orderZREVRANGE myzset 0 -1 WITHSCORES
LLEN keyGet the length of a listLLEN mylist
LINDEX key indexGet an element from a list by its indexLINDEX mylist 2
`LINSERT key BEFOREAFTER pivot value`Insert a value before or after a specific value in a list
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]Get members from a sorted set within a score rangeZRANGEBYSCORE myzset 0 10 WITHSCORES LIMIT 0 5
CONFIG REWRITERewrite the configuration file with the in-memory configurationCONFIG REWRITE
CLIENT LISTGet the list of client connections to the serverCLIENT LIST
SCRIPT LOAD scriptLoad a Lua script into the script cacheSCRIPT LOAD "return redis.call('PING')"

This cheat sheet covers some of the fundamental Redis commands. For more detailed information, refer to the official Redis documentation.