Stacktape

Sign up

Stacktape

Sign up



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).
  • 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.com
apiKey: $Secret('upstash-api-key')
resources:
myUpstash:
type: upstash-redis

Basic usage

Copy

providerConfig:
upstash:
accountEmail: xxxxx.yyyy@example.com
apiKey: $Secret('upstash-api-key')
resources:
myUpstash:
type: upstash-redis
myFunction:
type: function
properties:
packaging:
type: stacktape-lambda-buildpack
properties:
entryfilePath: lambdas/upstash.ts
environment:
- name: UPSTASH_REDIS_REST_URL
value: $ResourceParam('myUpstash', 'restUrl')
- name: UPSTASH_REDIS_REST_TOKEN
value: $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 data
let { 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

UpstashRedis  API reference
type
Required
properties.enableEviction
overrides

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.com
apiKey: $Secret('upstash-api-key')
resources:
myUpstash:
type: upstash-redis
properties:
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.

host
  • Hostname (address) of the Upstash Redis database.

  • Usage: $ResourceParam('<<resource-name>>', 'host')
port
  • Port of the Upstash Redis database.

  • Usage: $ResourceParam('<<resource-name>>', 'port')
password
  • Autogenerated password that can be used to authenticate towards the database.

  • Usage: $ResourceParam('<<resource-name>>', 'password')
restToken
  • Rest token which can be used when reading/writing from/to the database using the REST API

  • Usage: $ResourceParam('<<resource-name>>', 'restToken')
readOnlyRestToken
  • Rest token which can be used only for reading from the database using the REST API

  • Usage: $ResourceParam('<<resource-name>>', 'readOnlyRestToken')
restUrl
  • Rest URL which can be used when communicating with the database using the REST API

  • Usage: $ResourceParam('<<resource-name>>', 'restUrl')
redisUrl
  • Standard redis url (including password) which can be used for connecting using cli.

  • Usage: $ResourceParam('<<resource-name>>', 'redisUrl')

Need help? Ask a question on Discord or info@stacktape.com.