Skip to main content
Version: 0.10

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:

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.

note

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,
});
note

For existing apps, learn how to migrate the KV store to Redis.

Examples

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

note

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

CommandAction
getGets the value of key.
setSets key to hold a string value.
delRemoves the specified keys.
typeReturns the string representation of the type of value stored at key.

Batch read/write

CommandAction
mgetReturns the values of all specified keys.
msetSets the given keys to their respective values.

Strings

CommandAction
getRangeReturns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive).
setRangeOverwrites part of the string stored at key, starting at the specified offset, for the entire length of value.
strlenReturns 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.

CommandAction
hgetReturns the value associated with field in the hash stored at key.
hsetSets the specified fields to their respective values in the hash stored at key.
hdelRemoves the specified fields from the hash stored at key.
hgetallReturns a map of fields and their values stored in the hash
hkeysReturns all field names in the hash stored at key.
hscanIterates fields of Hash types and their associated values.
hincrbyIncrements the score of member in the sorted set stored at key by value
hlenReturns the number of fields contained in the hash stored at key.

Numbers

CommandAction
incrByIncrements the number stored at key by increment.

Key expiration

CommandAction
expireSets a timeout on key.
expireTimeReturns 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.

CommandAction
multiMarks the start of a transaction block.
execExecutes all previously queued commands in a transaction and restores the connection state to normal.
discardFlushes all previously queued commands in a transaction and restores the connection state to normal.
watchMarks the given keys to be watched for conditional execution of a transaction.
unwatchFlushes all the previously watched keys for a transaction.

Sorted set

CommandAction
zAddAdds all the specified members with the specified scores to the sorted set stored at key.
zCardReturns the sorted set cardinality (number of elements) of the sorted set stored at key.
zRangeReturns the specified range of elements in the sorted set stored at key.
zRemRemoves the specified members from the sorted set stored at key.
zScoreReturns the score of member in the sorted set at key.
zRankReturns the rank of member in the sorted set stored at key
zIncrByIncrements the score of member in the sorted set stored at key by value
zScanIterates elements of sorted set types and their associated scores.
zRemByRangeByLexWhen 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.
zRemByRangeByRankRemoves all elements in the sorted set stored at key with rank between start and stop.
zRemByRangeByScoreRemoves all elements in the sorted set stored at key with a score between min and max (inclusive).