Upstash Serverless Redis
Overview
Redis is a fast, NoSQL in-memory data store that can provide sub-millisecond latency.
Upstash provides Redis as a serverless service. This enables users to leverage Redis without the need for paying and managing their own servers.
Upstash uses Per-Request pricing so you do NOT need to pay for the servers/instances and you only pay for request you make.
Low latency data, ease of use, and pay-per-request pricing makes Upstash a perfect choice for Serverless applications.
When to use
Advantages
- REST API - access your database using simple REST API
- Secure - with option to enable TLS out of the box
- High availability - with option to enable multi-zone replication
- Pay-per-request - pay only for what you use. Refer to Upstash pricing
- Scalable
Disadvantages
- Separate billing - Even though Stacktape seamlessly integrates Upstash Redis to your stacks, you still need to manage your billing separately.
Provider configuration
You must have an Upstash account. To create one, head over to Upstash registration page.
You need following to configure the provider:
- Email address of your Upstash account (
accountEmail
). - API Key associated with the account (
apiKey
).
- Email address of your Upstash account (
You can obtain API Key in Upstash account management console.
The recommended way to store your credentials is to use secrets.
Copy
providerConfig:upstash:accountEmail: xxxxx.yyyy@example.comapiKey: $Secret('upstash-api-key')resources:myUpstash:type: upstash-redis
Basic usage
Copy
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')
Copy
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;}};
Code of myFunction
function
Deployment region
Currently Upstash Redis supports only 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 are deploying your stack in some other region, Stacktape will try to deploy the database in the region geographically closest to the region of your stack.
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.
Copy
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')