Redis Cheatsheet
Redis
Database
Cache
NoSQL
Complete reference for Redis commands, covering all data types from basic strings to advanced data structures like HyperLogLog and pub/sub messaging.
Quick Reference
🔧 Basics & Setup
Server management and basic operations
📝 Strings & Keys
String operations and key management
📋 Lists & Sets
Collection data structures and operations
🔄 Advanced Features
Pub/sub, HyperLogLog, and sorted sets
Basics & Setup
Server Management
Start Redis with configuration file
Open Redis prompt
Restart Redis service
Check Redis status
Strings
Strings are the most basic kind of Redis value. Redis strings are binary safe, meaning they can contain any kind of data.
Basic String Operations
Set value in key
Get value in key
Set if not exist value in key
Delete key
String Manipulation
Append a value to a key
Get the length of the value stored in a key
Overwrite part of a string at key starting at the specified offset
Get substring of stored value from start to end offsets (both inclusive)
Multiple Operations
Set multiple keys to multiple values
Set multiple keys to multiple values, only if none of the keys exist
Get the values of all the given keys
Numeric Operations
Increment value in key
Increment the integer value of a key by the given amount
Increment the float value of a key by the given amount
Decrement the integer value of key by one
Decrement the integer value of a key by the given number
Key Expiration
Set key to expire in 120 seconds
Returns the number of seconds until a key is deleted
Count set bits in a string
Lists
Redis lists are ordered collections of strings. You can add elements to the head or tail of a list.
Adding Elements
Put the new value at the end of the list
Append a value at the end of the list, only if it exists
Put the new value at the start of the list
Append a value at the start of the list, only if it exists
Retrieving Elements
Give a subset of the list
Get an element from a list by its index
Return the current length of the list
Modifying Lists
Insert an element before or after another element in a list
Set the value of an element in a list by its index
Delete occurrences of value if the list stored in key
Trim a list to the specified range
Removing Elements
Remove the first element from the list and returns it
Remove the last element from the list and returns it
Remove the last element in a list, prepend it to another list and return it
Blocking Operations
Remove and get the first element in a list, or block until one is available
Remove and get the last element in a list, or block until one is available
Sets
Redis sets are unordered collections of unique strings. You can add, remove, and test for the existence of members.
Basic Set Operations
Add the given value to the set
Get the number of members in a set
Remove the given value from the set
Test if the given value is in the set
Return a list of all the members of this set
Set Operations
Combine two or more sets and returns the list of all elements
Intersect multiple sets
Move a member from one set to another
Remove and return one or multiple random members from a set
Sorted Sets
Sorted sets are similar to regular sets, but each member has an associated score used for ordering.
Basic Operations
Add one or more members to a sorted set, or update its score if it already exists
Get the number of members in a sorted set
Count the members in a sorted set with scores within the given values
Increment the score of a member in a sorted set
Retrieving Members
Returns a subset of the sorted set
Return a range of members in a sorted set, by score
Determine the index of a member in a sorted set
Get the score associated with the given member in a sorted set
Removing Members
Remove one or more members from a sorted set
Remove all members in a sorted set within the given indexes
Remove all members in a sorted set, by index, with scores ordered from high to low
Hashes
Redis hashes are maps between string fields and string values, perfect for representing objects.
Basic Hash Operations
Get the value of a hash field
Get all the fields and values in a hash
Set the string value of a hash field
Set the string value of a hash field, only if the field does not exist
Set multiple fields at once
Hash Information
Determine if a hash field exists
Get all the fields in a hash
Get all the values in a hash
Get the number of fields in a hash
Get the length of the value of a hash field
Hash Modification
Increment value in hash by X
Delete one or more hash fields
HyperLogLog
HyperLogLog provides approximation of unique elements in a set using constant memory, ideal for counting unique visitors.
HyperLogLog Operations
Add the specified elements to the specified HyperLogLog
Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s)
Merge N HyperLogLogs into a single one
Publication & Subscription
Redis pub/sub implements the messaging paradigm where senders (publishers) are not programmed to send messages to specific receivers (subscribers).
Publishing
Post a message to a channel
Subscribing
Listen for messages published to the given channels
Stop listening for messages posted to the given channels
Pattern Subscription
Listen for messages published to channels matching the given patterns
Stop listening for messages posted to channels matching the given patterns
Pub/Sub Information
Inspect the state of the Pub/Sub subsystem
Other Commands
Key Management
Find all keys matching the given pattern
Best Practices
Follow these Redis best practices for optimal performance and reliability.
- Use appropriate data types for your use case to optimize memory and performance
- Set expiration times on keys to prevent memory leaks
- Use pipelining for multiple commands to reduce network overhead
- Monitor memory usage and configure maxmemory policies
- Use Redis persistence (RDB/AOF) based on your durability requirements
- Implement proper error handling for network timeouts and connection issues
- Use Redis Cluster for horizontal scaling in production environments
Learn More
Explore comprehensive Redis documentation and tutorials
Written by
Deepak Jangra
Created At
Wed Jan 15 2025
Updated At
Fri Jun 13 2025