Redis
Add a database to store and retrieve data.
The Redis plugin is designed to be fast, scalable and secure, and it supports a subset of the full Redis API, including:
- Transactions for things like counting votes atomically in polls
- String operations for batch writing or incrementing numbers
- Sorted sets for creating leaderboards
- Hashes for managing a collection of key-value pairs
Each app version installed on a subreddit is namespaced, which means Redis data is siloed from other subreddits. Keep in mind that there won’t be a single source of truth for all installations of your app, since each app installation can only access the data that it has stored in the Redis database.
Redis is a more robust replacement for the key value store. All apps using the key value store should migrate to Redis.
Limits and Quotas
- Max commands per second: 1000
- Max request size: 5 MB
- Max storage: 500 MB
All limits are applied at a per-installation granularity.
Add Redis to your app
When creating an app, enable the plugin in Devvit.configure.
Devvit.configure({
redis: true,
});
For existing apps, learn how to migrate the KV store to Redis.
Examples
Menu items
Devvit.addMenuItem({
location: 'subreddit',
label: 'Test Redis',
onPress: async (event, { redis }) => {
const key = 'hello';
await redis.set(key, 'world');
const value = await redis.get(key);
console.log(`${key}: ${value}`);
},
});
Custom Posts
You can follow this template to create a custom post containing an interactive progress bar backed by Redis.
// start from a template
devvit new <replace-with-your-app-name> --template=redis
devvit playtest <your-test-subreddit>
Supported Redis commands
Not all Redis features are supported. If you would like to request a specific Redis feature, please reach out to our team via modmail or Discord.
Simple read/write
| Command | Action |
|---|---|
| get | Gets the value of key. |
| set | Sets key to hold a string value. |
| del | Removes the specified keys. |
| type | Returns the string representation of the type of value stored at key. |
Batch read/write
| Command | Action |
|---|---|
| mget | Returns the values of all specified keys. |
| mset | Sets the given keys to their respective values. |
Strings
| Command | Action |
|---|---|
| getRange | Returns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive). |
| setRange | Overwrites part of the string stored at key, starting at the specified offset, for the entire length of value. |
| strlen | Returns the length of the string value stored at key. |
Hash
Redis hashes can store upto ~ 4.2 billion key-value pairs. We recommend hash usage for managing collections of key/value pairs
whenever possible and iterating over it using a combination of hscan, hkeys and hgetall - this will also allow you easily migrate away from using kvStore.list method. Please see the migration guide for more details.
| Command | Action |
|---|---|
| hget | Returns the value associated with field in the hash stored at key. |
| hset | Sets the specified fields to their respective values in the hash stored at key. |
| hdel | Removes the specified fields from the hash stored at key. |
| hgetall | Returns a map of fields and their values stored in the hash |
| hkeys | Returns all field names in the hash stored at key. |
| hscan | Iterates fields of Hash types and their associated values. |
| hincrby | Increments the score of member in the sorted set stored at key by value |
| hlen | Returns the number of fields contained in the hash stored at key. |
Numbers
| Command | Action |
|---|---|
| incrBy | Increments the number stored at key by increment. |
Key expiration
| Command | Action |
|---|---|
| expire | Sets a timeout on key. |
| expireTime | Returns the absolute Unix timestamp (since January 1, 1970) in seconds at which the given key will expire. |
Transactions
Redis transactions allow execution of a group of commands in a single isolated step. For example, let’s say you are implementing voting action in a polls app. These three steps need to happen together:
- Store the selected option for the user
- Increment the count for selected option
- Add the user to voted user list
We can sequence all of the above steps in a single transaction using multi and exec to ensure that either all of the steps happen together or none at all.
| Command | Action |
|---|---|
| multi | Marks the start of a transaction block. |
| exec | Executes all previously queued commands in a transaction and restores the connection state to normal. |
| discard | Flushes all previously queued commands in a transaction and restores the connection state to normal. |
| watch | Marks the given keys to be watched for conditional execution of a transaction. |
| unwatch | Flushes all the previously watched keys for a transaction. |
Sorted set
| Command | Action |
|---|---|
| zAdd | Adds all the specified members with the specified scores to the sorted set stored at key. |
| zCard | Returns the sorted set cardinality (number of elements) of the sorted set stored at key. |
| zRange | Returns the specified range of elements in the sorted set stored at key. |
| zRem | Removes the specified members from the sorted set stored at key. |
| zScore | Returns the score of member in the sorted set at key. |
| zRank | Returns the rank of member in the sorted set stored at key |
| zIncrBy | Increments the score of member in the sorted set stored at key by value |
| zScan | Iterates elements of sorted set types and their associated scores. |
| zRemByRangeByLex | When all elements in a sorted set are inserted with the same score, this command removes the elements at key between the lexicographical range specified by min and max. |
| zRemByRangeByRank | Removes all elements in the sorted set stored at key with rank between start and stop. |
| zRemByRangeByScore | Removes all elements in the sorted set stored at key with a score between min and max (inclusive). |