Redis Cheatsheet

Complete Redis cheatsheet covering strings, lists, sets, sorted sets, hashes, HyperLogLog, pub/sub, and essential commands for data manipulation.

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

redis-server /path/redis.conf

Open Redis prompt

redis-cli

Restart Redis service

sudo systemctl restart redis.service

Check Redis status

sudo systemctl status redis

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

SET key value

Get value in key

GET key

Set if not exist value in key

SETNX key value

Delete key

DEL key

String Manipulation

Append a value to a key

APPEND key value

Get the length of the value stored in a key

STRLEN key

Overwrite part of a string at key starting at the specified offset

SETRANGE key offset value

Get substring of stored value from start to end offsets (both inclusive)

GETRANGE key start end

Multiple Operations

Set multiple keys to multiple values

MSET key value [key value ...]

Set multiple keys to multiple values, only if none of the keys exist

MSETNX key value [key value ...]

Get the values of all the given keys

MGET key [key ...]

Numeric Operations

Increment value in key

INCR key

Increment the integer value of a key by the given amount

INCRBY key increment

Increment the float value of a key by the given amount

INCRBYFLOAT key increment

Decrement the integer value of key by one

DECR key

Decrement the integer value of a key by the given number

DECRBY key decrement

Key Expiration

Set key to expire in 120 seconds

EXPIRE key 120

Returns the number of seconds until a key is deleted

TTL key

Count set bits in a string

BITCOUNT key [start end]

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

RPUSH key value [value ...]

Append a value at the end of the list, only if it exists

RPUSHX key value

Put the new value at the start of the list

LPUSH key value [value ...]

Append a value at the start of the list, only if it exists

LPUSHX key value

Retrieving Elements

Give a subset of the list

LRANGE key start stop

Get an element from a list by its index

LINDEX key index

Return the current length of the list

LLEN key

Modifying Lists

Insert an element before or after another element in a list

LINSERT key BEFORE|AFTER pivot value

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

LSET key index value

Delete occurrences of value if the list stored in key

LREM key number_of_occurrences value

Trim a list to the specified range

LTRIM key start stop

Removing Elements

Remove the first element from the list and returns it

LPOP key

Remove the last element from the list and returns it

RPOP key

Remove the last element in a list, prepend it to another list and return it

RPOPLPUSH source destination

Blocking Operations

Remove and get the first element in a list, or block until one is available

BLPOP key [key ...] timeout

Remove and get the last element in a list, or block until one is available

BRPOP key [key ...] timeout

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

SADD key member [member ...]

Get the number of members in a set

SCARD key

Remove the given value from the set

SREM key member [member ...]

Test if the given value is in the set

SISMEMBER myset value

Return a list of all the members of this set

SMEMBERS myset

Set Operations

Combine two or more sets and returns the list of all elements

SUNION key [key ...]

Intersect multiple sets

SINTER key [key ...]

Move a member from one set to another

SMOVE source destination member

Remove and return one or multiple random members from a set

SPOP key [count]

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

ZADD key [NX|XX] [CH] [INCR] score member [score member ...]

Get the number of members in a sorted set

ZCARD key

Count the members in a sorted set with scores within the given values

ZCOUNT key min max

Increment the score of a member in a sorted set

ZINCRBY key increment member

Retrieving Members

Returns a subset of the sorted set

ZRANGE key start stop [WITHSCORES]

Return a range of members in a sorted set, by score

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

Determine the index of a member in a sorted set

ZRANK key member

Get the score associated with the given member in a sorted set

ZSCORE key member

Removing Members

Remove one or more members from a sorted set

ZREM key member [member ...]

Remove all members in a sorted set within the given indexes

ZREMRANGEBYRANK key start stop

Remove all members in a sorted set, by index, with scores ordered from high to low

ZREMRANGEBYSCORE key min max

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

HGET key field

Get all the fields and values in a hash

HGETALL key

Set the string value of a hash field

HSET key field value

Set the string value of a hash field, only if the field does not exist

HSETNX key field value

Set multiple fields at once

HMSET key field value [field value ...]

Hash Information

Determine if a hash field exists

HEXISTS key field

Get all the fields in a hash

HKEYS key

Get all the values in a hash

HVALS key

Get the number of fields in a hash

HLEN key

Get the length of the value of a hash field

HSTRLEN key field

Hash Modification

Increment value in hash by X

HINCRBY key field increment

Delete one or more hash fields

HDEL key field [field ...]

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

PFADD key element [element ...]

Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s)

PFCOUNT key [key ...]

Merge N HyperLogLogs into a single one

PFMERGE destkey sourcekey [sourcekey ...]

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

PUBLISH channel message

Subscribing

Listen for messages published to the given channels

SUBSCRIBE channel [channel ...]

Stop listening for messages posted to the given channels

UNSUBSCRIBE [channel [channel ...]]

Pattern Subscription

Listen for messages published to channels matching the given patterns

PSUBSCRIBE pattern [pattern ...]

Stop listening for messages posted to channels matching the given patterns

PUNSUBSCRIBE [pattern [pattern ...]]

Pub/Sub Information

Inspect the state of the Pub/Sub subsystem

PUBSUB subcommand [argument [argument ...]]

Other Commands

Key Management

Find all keys matching the given pattern

KEYS 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

Cheatsheets

Your go-to resource for quick reference guides on essential development tools and technologies.

© 2025 Deepak Jangra. All rights reserved.