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. Mastering Redis commands is essential for efficiently managing and utilizing Redis. This cheat sheet provides a quick reference to some of the most commonly used Redis commands.
Redis Commands Cheat Sheet
Key Management
Command |
Description |
Syntax |
Example |
SET |
Sets the value of a key. |
SET key value |
SET name "Alice" |
GET |
Gets the value of a key. |
GET key |
GET name |
DEL |
Deletes a key. |
DEL key |
DEL name |
EXISTS |
Checks if a key exists. |
EXISTS key |
EXISTS name |
EXPIRE |
Sets a timeout on a key. |
EXPIRE key seconds |
EXPIRE name 60 |
TTL |
Gets the remaining time to live of a key. |
TTL key |
TTL name |
KEYS |
Finds all keys matching a pattern. |
KEYS pattern |
KEYS user:* |
RENAME |
Renames a key. |
RENAME old_key new_key |
RENAME name full_name |
String Commands
Command |
Description |
Syntax |
Example |
APPEND |
Appends a value to a key. |
APPEND key value |
APPEND name "Smith" |
STRLEN |
Gets the length of the value stored in a key. |
STRLEN key |
STRLEN name |
INCR |
Increments the value of a key by one. |
INCR key |
INCR age |
DECR |
Decrements the value of a key by one. |
DECR key |
DECR age |
MGET |
Gets the values of all the given keys. |
MGET key1 key2 ... |
MGET name age |
MSET |
Sets multiple keys to multiple values. |
MSET key1 value1 key2 value2 ... |
MSET name "Alice" age 30 |
Hash Commands
Command |
Description |
Syntax |
Example |
HSET |
Sets the value of a field in a hash. |
HSET key field value |
HSET user:1000 name "Alice" |
HGET |
Gets the value of a field in a hash. |
HGET key field |
HGET user:1000 name |
HDEL |
Deletes one or more fields in a hash. |
HDEL key field1 field2 ... |
HDEL user:1000 name |
HLEN |
Gets the number of fields in a hash. |
HLEN key |
HLEN user:1000 |
HGETALL |
Gets all the fields and values in a hash. |
HGETALL key |
HGETALL user:1000 |
HEXISTS |
Checks if a field exists in a hash. |
HEXISTS key field |
HEXISTS user:1000 name |
List Commands
Command |
Description |
Syntax |
Example |
LPUSH |
Inserts a value at the head of a list. |
LPUSH key value |
LPUSH friends "Alice" |
RPUSH |
Inserts a value at the tail of a list. |
RPUSH key value |
RPUSH friends "Bob" |
LPOP |
Removes and returns the first element of a list. |
LPOP key |
LPOP friends |
RPOP |
Removes and returns the last element of a list. |
RPOP key |
RPOP friends |
LLEN |
Gets the length of a list. |
LLEN key |
LLEN friends |
LRANGE |
Gets a range of elements from a list. |
LRANGE key start stop |
LRANGE friends 0 -1 |
Set Commands
Command |
Description |
Syntax |
Example |
SADD |
Adds one or more members to a set. |
SADD key member1 member2 ... |
SADD skills "Python" "SQL" |
SREM |
Removes one or more members from a set. |
SREM key member1 member2 ... |
SREM skills "SQL" |
SMEMBERS |
Gets all the members in a set. |
SMEMBERS key |
SMEMBERS skills |
SISMEMBER |
Checks if a member exists in a set. |
SISMEMBER key member |
SISMEMBER skills "Python" |
SCARD |
Gets the number of members in a set. |
SCARD key |
SCARD skills |
SUNION |
Returns the union of multiple sets. |
SUNION key1 key2 ... |
SUNION set1 set2 |
Sorted Set Commands
Command |
Description |
Syntax |
Example |
ZADD |
Adds one or more members to a sorted set, or updates its score if it already exists. |
ZADD key score1 member1 score2 member2 ... |
ZADD leaderboard 100 "Alice" 90 "Bob" |
ZREM |
Removes one or more members from a sorted set. |
ZREM key member1 member2 ... |
ZREM leaderboard "Bob" |
ZSCORE |
Gets the score of a member in a sorted set. |
ZSCORE key member |
ZSCORE leaderboard "Alice" |
ZRANGE |
Gets a range of members in a sorted set by index. |
ZRANGE key start stop |
ZRANGE leaderboard 0 -1 |
ZRANK |
Gets the rank of a member in a sorted set. |
ZRANK key member |
ZRANK leaderboard "Alice" |
ZCARD |
Gets the number of members in a sorted set. |
ZCARD key |
ZCARD leaderboard |
Transaction Commands
Command |
Description |
Syntax |
Example |
MULTI |
Starts a transaction block. |
MULTI |
MULTI |
EXEC |
Executes all commands issued after MULTI . |
EXEC |
EXEC |
DISCARD |
Discards all commands issued after MULTI . |
DISCARD |
DISCARD |
WATCH |
Watches one or more keys for changes. |
WATCH key1 key2 ... |
WATCH balance |
UNWATCH |
Unwatches all keys previously watched by the WATCH command. |
UNWATCH |
UNWATCH |
Pub/Sub Commands
Command |
Description |
Syntax |
Example |
PUBLISH |
Posts a message to a channel. |
PUBLISH channel message |
PUBLISH news "Breaking News" |
SUBSCRIBE |
Subscribes to one or more channels. |
SUBSCRIBE channel1 channel2 ... |
SUBSCRIBE news sports |
UNSUBSCRIBE |
Unsubscribes from one or more channels. |
UNSUBSCRIBE channel1 channel2 ... |
UNSUBSCRIBE news |
PSUBSCRIBE |
Subscribes to channels that match a pattern. |
PSUBSCRIBE pattern |
PSUBSCRIBE news* |
PUNSUBSCRIBE |
Unsubscribes from channels that match a pattern. |
PUNSUBSCRIBE pattern |
PUNSUBSCRIBE news* |
Server Commands
Command |
Description |
Syntax |
Example |
INFO |
Provides information and statistics about the server. |
INFO |
INFO |
CONFIG GET |
Gets the value of a configuration parameter. |
CONFIG GET parameter |
CONFIG GET maxmemory |
CONFIG SET |
Sets a configuration parameter to the given value. |
CONFIG SET parameter value |
CONFIG SET maxmemory 256mb |
SAVE |
Synchronously saves the dataset to disk. |
SAVE |
SAVE |
BGSAVE |
Asynchronously saves the dataset to disk. |
BGSAVE |
BGSAVE |
FLUSHDB |
Deletes all keys in the current database. |
FLUSHDB |
FLUSHDB |
FLUSHALL |
Deletes all keys in all databases. |
FLUSHALL |
FLUSHALL |
Conclusion
Mastering Redis commands is essential for efficiently managing and utilizing Redis. This cheat sheet provides a quick reference to some of the most commonly used commands, helping you navigate and operate your Redis instances more effectively. Keep this guide handy to make the most of Redis. Happy coding!