Redis Commands Cheatsheet
Essential Redis Commands for Developers
This cheatsheet provides a quick reference for commonly used Redis commands, categorized for easy access. Redis is a powerful open-source, in-memory data structure store used as a database, cache, and message broker. Understanding its core commands is crucial for efficient development.
Key Management Commands
Manage your Redis keys effectively with these fundamental commands.
# Get the type of a key
TYPE key
# Delete keys
DEL key [key ...]
# Check if a key exists
EXISTS key
# Set a timeout on a key
EXPIRE key seconds
# Set a timeout in milliseconds
PEXPIRE key milliseconds
# Get the remaining time to live of a key
TTL key
# Get remaining time in milliseconds
PTTL key
# Persist a key (remove its timeout)
PERSIST key
# Rename a key
RENAME key newkey
# Get a random key
RANDOMKEY
String Commands
Work with Redis strings, which can be any sequence of bytes.
# Set a string value
SET key value
# Get a string value
GET key
# Append a value to a string
APPEND key value
# Get a substring of a string
GETRANGE key start end
# Increment a number stored in a string
INCR key
INCRBY key increment
# Decrement a number stored in a string
DECR key
DECRBY key decrement
List Commands
Redis lists are linked lists, useful for implementing queues and stacks.
# Add an element to the head of a list
LPUSH key element [element ...]
# Add an element to the tail of a list
RPUSH key element [element ...]
# Get an element from the head of a list and remove it
LPOP key
# Get an element from the tail of a list and remove it
RPOP key
# Get elements from a list
LRANGE key start end
# Get the length of a list
LLEN key
Hash Commands
Hashes are maps between string fields and string values, perfect for representing objects.
# Set a field in a hash
HSET hash field value
# Get a field from a hash
HGET hash field
# Get all fields and values from a hash
HGETALL hash
# Get all fields from a hash
HKEYS hash
# Get all values from a hash
HVALS hash
# Check if a field exists in a hash
HEXISTS hash field
Set Commands
Sets are unordered collections of unique strings.
# Add members to a set
SADD set member [member ...]
# Get all members of a set
SMEMBERS set
# Check if a member is in a set
SISMEMBER set member
# Remove members from a set
SREM set member [member ...]
# Get the number of members in a set
SCARD set
Sorted Set Commands
Sorted sets are sets where each member has a score, ordered by score.
# Add members to a sorted set
ZADD sorted_set score member [score member ...]
# Get members in a range by score
ZRANGE sorted_set start end [WITHSCORES]
# Get members in a range by rank
ZRANGEBYRANK sorted_set start end [WITHSCORES]
# Get the score of a member
ZSCORE sorted_set member
# Get the number of members in a sorted set
ZCARD sorted_set
Pub/Sub Commands
Enable real-time messaging between clients.
# Subscribe to channels
SUBSCRIBE channel [channel ...]
# Publish a message to a channel
PUBLISH channel message
Transactions
Group multiple commands into a single atomic operation.
# Start a transaction
MULTI
# Queue commands
SET key value
GET key
# Execute the transaction
EXEC
# Discard the transaction
DISCARD
Connection Commands
Manage client connections.
# Ping the server
PING
# Authenticate to the server
AUTH password
# Select a database
SELECT db_index
# Quit the connection
QUIT
Further Resources
- Official Redis Commands Documentation
- Redis Documentation
- MDN Web Docs (for general web development context)