Upstash 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
- eu-west-1
- ap-northeast-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 TLS
- If you enable TLS on existing database, the database will experience a downtime about 1-2 seconds (NO data will be lost).
- Enabling TLS can cause performance downgrade. If high throughput is essential for your app, consider reading this thread before using TLS.
Once TLS is enabled, it cannot be disabled.
Copy
providerConfig:upstash:accountEmail: xxxxx.yyyy@example.comapiKey: $Secret('upstash-api-key')resources:myUpstash:type: upstash-redisproperties:enableTls: true
Enable multizone replication
- When enabled the data is replicated to multiple availability zones.
- Enabling multi zone replication will increase per-request-cost to $0.4 per 100K requests (from original $0.2).
- For more information refer to Upstash docs.
Once multizone replication is enabled, it cannot be disabled
Copy
providerConfig:upstash:accountEmail: xxxxx.yyyy@example.comapiKey: $Secret('upstash-api-key')resources:myUpstash:type: upstash-redisproperties:enableMultizoneReplication: true
Enable strong consistency
- Strong consistency can only be enabled during database creation.
- When strong consistency is enabled, persistence to disk and replication to the majority of replicas are performed before returning response to the client.
- Enabling strong consistency mode may increase the latency of writes.
- For more information refer to Upstash docs
Copy
providerConfig:upstash:accountEmail: xxxxx.yyyy@example.comapiKey: $Secret('upstash-api-key')resources:myUpstash:type: upstash-redisproperties:enableStrongConsistency: 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')