Skip to content

Redis Commands Cheat Sheet

Basic Commands

# Start Redis server
redis-server

# Connect to Redis
redis-cli

Key Commands

# Set a key
SET key value

# Get a key
GET key

# Delete a key
DEL key

# Check if a key exists
EXISTS key

# Set a key with an expiration time (in seconds)
SETEX key seconds value

# Set a key to expire after a certain number of seconds
EXPIRE key seconds

# Get the remaining time to live of a key
TTL key

# Get the type of value stored at key
TYPE key

String Commands

# Increment the integer value of a key
INCR key

# Decrement the integer value of a key
DECR key

# Increment the integer value of a key by the given amount
INCRBY key increment

# Decrement the integer value of a key by the given amount
DECRBY key decrement

# Append a value to a key
APPEND key value

# Get a substring of the string stored at a key
GETRANGE key start end

# Set the value of a key and return its old value
GETSET key value

Hash Commands

# Set the value of a hash field
HSET key field value

# Get the value of a hash field
HGET key field

# Get all the fields and values in a hash
HGETALL key

# Set multiple fields at once
HMSET key field1 value1 field2 value2

# Get multiple fields at once
HMGET key field1 field2

# Delete one or more hash fields
HDEL key field1 field2

# Increment the integer value of a hash field by the given number
HINCRBY key field increment

# Get the number of fields in a hash
HLEN key

# Get all the fields in a hash
HKEYS key

# Get all the values in a hash
HVALS key

List Commands

# Push a value onto the end of a list
RPUSH key value

# Push a value onto the beginning of a list
LPUSH key value

# Get the length of a list
LLEN key

# Get a range of elements from a list
LRANGE key start stop

# Remove and get the first element in a list
LPOP key

# Remove and get the last element in a list
RPOP key

# Get an element from a list by its index
LINDEX key index

# Set the value of an element in a list by its index
LSET key index value

# Remove elements from a list
LREM key count value

Set Commands

# Add one or more members to a set
SADD key member1 member2

# Get all the members in a set
SMEMBERS key

# Remove one or more members from a set
SREM key member1 member2

# Remove and return a random member from a set
SPOP key

# Get the number of members in a set
SCARD key

# Check if a member is in a set
SISMEMBER key member

# Get the intersection of multiple sets
SINTER key1 key2

# Get the union of multiple sets
SUNION key1 key2

# Get the difference between multiple sets
SDIFF key1 key2

Sorted Set Commands

# Add one or more members to a sorted set, or update its score if it already exists
ZADD key score1 member1 score2 member2

# Get the number of members in a sorted set
ZCARD key

# Get the score associated with a member in a sorted set
ZSCORE key member

# Increment the score of a member in a sorted set
ZINCRBY key increment member

# Remove one or more members from a sorted set
ZREM key member1 member2

# Get a range of members in a sorted set by index
ZRANGE key start stop

# Get a range of members in a sorted set by score
ZRANGEBYSCORE key min max

Other Useful Commands

# Get information and statistics about the server
INFO

# List all keys matching a pattern
KEYS pattern

# Delete all keys in the current database
FLUSHDB

# Delete all keys in all databases
FLUSHALL

# Get the value of a configuration parameter
CONFIG GET parameter

# Set a configuration parameter
CONFIG SET parameter value

# Save the dataset to disk
SAVE

# Asynchronously save the dataset to disk
BGSAVE

# Shut down the server
SHUTDOWN