Upstash Serverless Redis
Upstash provides a serverless Redis service, which allows you to use the fast, in-memory NoSQL data store without having to manage your own servers. With its per-request pricing and low latency, Upstash is a great choice for serverless applications.
Advantages
- REST API: You can access your database using a simple REST API.
- Secure: You can enable TLS with a single click.
- Highly available: You can enable multi-zone replication to ensure that your data is always available.
- Pay-per-request: You only pay for what you use. See the Upstash pricing for more details.
- Scalable: Upstash automatically scales to meet your needs.
Disadvantages
- Separate billing: Although Stacktape integrates with Upstash, you will still need to manage your billing separately.
Provider configuration
To use Upstash with Stacktape, you will need to:
- Create an Upstash account.
- Get your API key from the Upstash console.
- Store your credentials (the email address for your Upstash account and your API key) in a secret.
providerConfig:upstash:accountEmail: xxxxx.yyyy@example.comapiKey: $Secret('upstash-api-key')resources:myUpstash:type: upstash-redis
Basic usage
providerConfig:upstash:accountEmail: xxxxx.yyyy@example.comapiKey: $Secret('upstash-api-key')resources:myUpstash:type: upstash-redismyFunction:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: lambdas/upstash.tsenvironment:- name: UPSTASH_REDIS_REST_URLvalue: $ResourceParam('myUpstash', 'restUrl')- name: UPSTASH_REDIS_REST_TOKENvalue: $ResourceParam('myUpstash', 'restToken')
import upstash from '@upstash/redis';const redis = upstash({ url: process.env.UPSTASH_REDIS_REST_URL, token: process.env.UPSTASH_REDIS_REST_TOKEN });export default async (event) => {// write datalet { data, error } = await redis.set('key1', 'value1');// read data({ data, error } = await redis.get('key1'));console.log(data);if (error) {throw error;}};
The code for the myFunction
function.
Deployment region
Upstash Redis is currently available in the following regions:
- us-east-1
- us-west-1
- us-west-2
- eu-west-1
- eu-central-1
- ap-southeast-1
- ap-southeast-2
- sa-east-1
If you deploy your stack in a different region, Stacktape will automatically deploy the database in the region that is geographically closest to your stack's region.
Enable eviction
- When enabled, Redis will automatically evict keys when memory limit is reached.
- Uses optimistic-volatile algorithm (combination of volatile-random and allkeys-random policies).
- Allows older data to be automatically removed to make room for new data.
providerConfig:upstash:accountEmail: xxxxx.yyyy@example.comapiKey: $Secret('upstash-api-key')resources:myUpstash:type: upstash-redisproperties:enableEviction: true
Referenceable parameters
The following parameters can be easily referenced using $ResourceParam directive directive.
To learn more about referencing parameters, refer to referencing parameters.
Hostname (address) of the Upstash Redis database.
- Usage:
$ResourceParam('<<resource-name>>', 'host')
Port of the Upstash Redis database.
- Usage:
$ResourceParam('<<resource-name>>', 'port')
Autogenerated password that can be used to authenticate towards the database.
- Usage:
$ResourceParam('<<resource-name>>', 'password')
Rest token which can be used when reading/writing from/to the database using the REST API
- Usage:
$ResourceParam('<<resource-name>>', 'restToken')
Rest token which can be used only for reading from the database using the REST API
- Usage:
$ResourceParam('<<resource-name>>', 'readOnlyRestToken')
Rest URL which can be used when communicating with the database using the REST API
- Usage:
$ResourceParam('<<resource-name>>', 'restUrl')
Standard redis url (including password) which can be used for connecting using cli.
- Usage:
$ResourceParam('<<resource-name>>', 'redisUrl')