The Redis cheat sheet includes basic syntax and methods to help you use Redis. Redis is an open-source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.
Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
String
Redis strings store sequences of bytes, including text, serialized objects, and binary arrays. As such, strings are the simplest type of value you can associate with a Redis key. They’re often used for caching, but they support additional functionality that lets you implement counters and perform bitwise operations, too.
APPEND |
Append |
BITCOUNT |
Count set bits |
BITOP |
Bitwise operations |
BITPOS |
Find the first set of bit |
DECR |
Decrement integer |
DECRBY |
Subtract from integer |
GET |
Get by key |
GETBIT |
Get bit by index |
GETRANGE |
Get substring |
GETSET |
Set, returning the old value |
INCR |
Increment integer |
INCRBY |
Add to integer |
INCRBYFLOAT |
Add to float |
MGET |
Get multiple |
MSET |
Set multiple |
MSETNX |
Set multiple if don’t exist |
PSETEX |
Set with expiry (ms) |
SET |
Set |
SETBIT |
Set bit by index |
SETEX |
Set with expiry (seconds) |
SETNX |
Set if doesn’t exist |
SETRANGE |
Set substring |
STRLEN |
Get length |
Strings can be used as numbers, arrays, bit sets, and binary data |
Lists
Redis lists are implemented via Linked Lists. This means that even if you have millions of elements inside a list, the operation of adding a new element in the head or in the tail of the list is performed in constant time.
BLPOP |
Blocking left pop |
BRPOP |
Blocking right pop |
BRPOPLPUSH |
Blocking rotate |
LINDEX |
Access by index |
LINSERT |
Insert next to |
LLEN |
Get length |
LPOP |
Pop from start |
LPUSH |
Push onto start |
LPUSHX |
Push if the list exists |
LRANGE |
Access Range |
LREM |
Remove |
LSET |
Set item by index |
LTRIM |
Remove start and/or end items |
RPOP |
Pop from end |
RPOPLPUSH |
Rotate |
RPUSH |
Push onto end |
RPUSHX |
Push onto the end if a list exists |
Hashes
Redis hashes are record types structured as collections of field-value pairs. You can use hashes to represent basic objects and to store groupings of counters, among other things.
HDEL |
Delete item |
HEXISTS |
Check for item |
HGET |
Get item |
HGETALL |
Return all items |
HINCRBY |
Add to the integer value |
HINCRBYFLOAT |
Add to float value |
HKEYS |
Return all keys |
HLEN |
Get the number of items |
HMGET |
Get multiple items |
HMSET |
Set multiple items |
HSCAN |
Iterate items |
HSET |
Set item |
HSETNX |
Set item if doesn’t exist |
HVALS |
Return all values |
Sets
A Redis set is an unordered collection of unique strings (members). You can use Redis sets to efficiently:
- Track unique items (e.g., track all unique IP addresses accessing a given blog post).
- Represent relations (e.g., the set of all users with a given role).
- Perform common set operations such as intersection, unions, and differences.
SADD |
Add item |
SCARD |
Get size |
SDIFF |
Get difference |
SDIFFSTORE |
Store difference |
SINTER |
Intersection |
SINTERSTORE |
Store intersection |
SISMEMBER |
Check for item |
SMEMBERS |
Get all |
SMOVE |
Move item to another set |
SPOP |
Pop random item |
SRANDMEMBER |
Get random item |
SREM |
Remove matching |
SSCAN |
Iterate items |
SUNION |
Union |
SUNIONSTORE |
Store union |
Sorted sets
A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include:
ZADD |
Add item |
ZCARD |
Get number of items |
ZCOUNT |
Number of items within score range |
ZINCRBY |
Add to score |
ZINTERSTORE |
Store intersection |
ZLEXCOUNT |
Lexicographical range count |
ZRANGE |
Get items within rank range |
ZLEXRANGE |
Get items within lexicographical range |
ZRANGEBYSCORE |
Get items within score range |
ZRANK |
Get item rank |
ZREM |
Remove item(s) |
ZREMRANGEBYLEX |
Remove items within lexicographical range |
ZREMRANGEBYRANK |
Remove items within rank range |
ZREMRANGEBYSCORE |
Remove items within score range |
ZREVRANGE |
ZRANGE in reverse order |
ZREVRANGEBYSCORE |
ZRANGEBYSCORE in reverse order |
ZREVRANK |
ZRANK in reverse order |
ZSCAN |
Iterate items |
ZSCORE |
Get item score |
ZUNIONSTORE |
Store union |
Lexicographical commands require all items to have the same score |
Redis Streams
A Redis stream is a data structure that acts like an append-only log but also implements several operations to overcome some of the limits of a typical append-only log. These include random access in O(1) time and complex consumption strategies, such as consumer groups. You can use streams to record and simultaneously syndicate events in real time.
XADD |
Adds a new entry to a stream. |
XREAD |
Reads one or more entries, starting at a given position and moving forward in time. |
XRANGE |
Returns a range of entries between two supplied entry IDs. |
XLEN |
Returns the length of a stream. |
Redis Geospatial
Redis geospatial indexes let you store coordinates and search for them. This data structure is useful for finding nearby points within a given radius or bounding box.
GEOADD |
Adds a location to a given geospatial index (note that longitude comes before latitude with this command). |
GEOSEARCH |
Returns locations with a given radius or a bounding box. |
Redis Bitmaps
Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type which is treated like a bit vector. Since strings are binary safe blobs and their maximum length is 512 MB, they are suitable to set up to 2^32 different bits.
SETBIT |
Sets a bit at the provided offset to 0 or 1. |
GETBIT |
Returns the value of a bit at a given offset. |
BITOP |
Lets you perform bitwise operations against one or more strings. |
Redis Bitfields
Redis bitfields let you set, increment, and get integer values of arbitrary bit length. For example, you can operate on anything from unsigned 1-bit integers to signed 63-bit integers. These values are stored using binary-encoded Redis strings. Bitfields support atomic read, write and increment operations, making them a good choice for managing counters and similar numerical values.
BITFIELD |
Atomically sets, increments and reads one or more values. |
BITFIELD_RO |
A read-only variant of BITFIELD |
Database
DEL |
Delete item |
DUMP |
Serialise item |
EXISTS |
Check for key |
EXPIRE |
Set a timeout on item |
EXPIREAT |
Set timeout by timestamp |
KEYS |
Get all keys to match the pattern |
MIGRATE |
Transfer an item between Redis instances |
MOVE |
Transfer an item between databases |
OBJECT |
Inspect item |
PERSIST |
Remove timeout |
PEXPIRE |
Set timeout (ms) |
PEXPIREAT |
Set timeout (ms timestamp) |
PTTL |
Get item time to live (ms) |
RANDOMKEY |
Get random key |
RENAME |
Change item’s key |
RENAMENX |
Change item’s key if new key doesn’t exist |
RESTORE |
Deserialise |
SCAN |
Iterate keys |
SORT |
Get or store sorted copy of list, set or sorted set |
TTL |
Get item time to live |
TYPE |
Get type of item |
Client/Server
AUTH |
Request authentication |
ECHO |
Return message |
PING |
Test connection |
QUIT |
Close connection |
SELECT |
Set current database by index |
Scripts
EVAL |
Run |
EVALSHA |
Run cached |
SCRIPT EXISTS |
Check by hash |
SCRIPT FLUSH |
Clear cache |
SCRIPT KILL |
Kill running script |
SCRIPT LOAD |
Add to cache |
Lua scripts access keys through the array KEYS and additional arguments through the array ARGV. |
HyperLogLogs
PFADD |
Add items |
PFCOUNT |
Get approximate size |
PFMERGE |
Merge HyperLogLogs |
Redis Cheat Sheet
Leave a Comment