Catalog / Redis Cheatsheet

Redis Cheatsheet

A quick reference guide to Redis commands and concepts, covering data structures, operations, and configuration.

Data Structures & Basic Commands

Strings

SET key value

Set the string value of a key.

GET key

Get the value of a key.

MSET key value [key value ...]

Set multiple keys to multiple values.

MGET key [key ...]

Get the values of all the given keys.

INCR key

Increment the integer value of a key by one.

DECR key

Decrement the integer value of a key by one.

APPEND key value

Append a value to a key.

STRLEN key

Get the length of the value stored in a key.

Lists

LPUSH key value [value ...]

Prepend one or multiple values to a list.

RPUSH key value [value ...]

Append one or multiple values to a list.

LPOP key

Remove and get the first element in a list.

RPOP key

Remove and get the last element in a list.

LRANGE key start stop

Get a range of elements from a list.

LLEN key

Get the length of a list.

LREM key count value

Remove elements from a list.

Hashes & Sets

Hashes

HSET key field value

Set the string value of a hash field.

HGET key field

Get the value of a hash field.

HMSET key field value [field value ...]

Set multiple hash fields to multiple values.

HMGET key field [field ...]

Get the values of all the given hash fields.

HGETALL key

Get all the fields and values in a hash.

HDEL key field [field ...]

Delete one or more hash fields.

HLEN key

Get the number of fields in a hash.

Sets

SADD key member [member ...]

Add one or more members to a set.

SREM key member [member ...]

Remove one or more members from a set.

SMEMBERS key

Get all the members in a set.

SISMEMBER key member

Determine if a given value is a member of a set.

SCARD key

Get the number of members in a set.

SINTER key [key ...]

Intersect multiple sets.

SUNION key [key ...]

Add multiple sets.

Sorted Sets & Pub/Sub

Sorted Sets

ZADD key score member [score member ...]

Add one or more members to a sorted set, or update their score if they already exist.

ZREM key member [member ...]

Remove one or more members from a sorted set.

ZRANGE key start stop [WITHSCORES]

Return a range of members in a sorted set, by index.

ZREVRANGE key start stop [WITHSCORES]

Return a range of members in a sorted set, by index, with scores ordered from high to low.

ZSCORE key member

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

ZCARD key

Get the number of members in a sorted set.

ZCOUNT key min max

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

Pub/Sub

PUBLISH channel message

Post a message to a channel.

SUBSCRIBE channel [channel ...]

Subscribe the client to the given channels.

UNSUBSCRIBE channel [channel ...]

Unsubscribe the client from the given channels.

PSUBSCRIBE pattern [pattern ...]

Subscribe the client to the given patterns.

PUNSUBSCRIBE pattern [pattern ...]

Unsubscribe the client from the given patterns.

Transactions & Persistence

Transactions

MULTI

Mark the start of a transaction block.

EXEC

Execute all commands in a transaction block.

DISCARD

Discard all commands in a transaction block.

WATCH key [key ...]

Watch the given keys to determine execution of the MULTI/EXEC block.

UNWATCH

Forget about all watched keys.

Persistence

SAVE

Perform a synchronous save of the dataset to disk.

BGSAVE

Perform an asynchronous save of the dataset to disk.

LASTSAVE

Return the UNIX timestamp of the last successful save to disk.

CONFIG GET save

Get the current save configuration.

BGREWRITEAOF

Asynchronously rewrite the append-only file.