Skip to content

Resources / Databases

Redis

A redis resource is a managed Redis-compatible cache on Amazon ElastiCache, for caching, sessions, rate limits, queues and pub/sub with sub-millisecond reads and writes. It runs Valkey, the open-source continuation of Redis that AWS maintains, so Redis clients such as ioredis, node-redis, redis-py and Jedis work unchanged. The cache lives in your stack's VPC, has no public endpoint and encrypts traffic.

The smallest cache costs about $6 a month idle (Valkey Serverless) or $9.40 a month (one cache.t4g.micro node). It's not a system of record: keep the data you can't lose in a relational database or DynamoDB. For Lambda functions outside a VPC, Upstash Redis works over HTTPS.

Choose an engine

engine.type decides which software runs the cache and how AWS runs it. There is no default: the choice changes cost and which command patterns work.

valkey-serverlessvalkeyredis
How it runsServerless, scales automaticallyFixed-size nodesFixed-size Redis OSS nodes
BillingPer GB stored + per requestPer node-hourPer node-hour (25% more than Valkey)
Smallest cost~$6/month idle (100 MB minimum)~$9.40/month (cache.t4g.micro)~$11.70/month (cache.t4g.micro)
High availabilityBuilt in, across Availability ZonesAdd replicas + automatic failoverAdd replicas + automatic failover
Cache creationAbout 2 minutesAbout 5 minutesAbout 5 minutes
Multi-key commandsKeys must share a hash slotUnrestrictedUnrestricted
Use forNew caches with small or spiky dataLarge datasets, any client patternExisting caches only

Prices are for us-east-1, running all month (ElastiCache pricing).

Valkey Serverless

TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: { type: 'valkey-serverless' }  });  return {    resources: { cache }  };});

There are no instances to size. The cache stores up to 5,000 GB and is billed for the data it holds (minimum 100 MB) plus ElastiCache Processing Units (ECPUs) for requests. A GET or SET of up to 1 KB uses 1 ECPU. Storage costs about $61 per GB-month, so above roughly 150 MB of data a small valkey node costs less.

Compatibility. A serverless cache always runs in cluster mode with all keys behind one endpoint. Standard (non-cluster) clients connect unchanged. Commands that touch several keys — MGET, MSET, transactions, and Lua scripts — fail with a CROSSSLOT error unless all their keys share a hash slot. Put a common hash tag in the key names, such as {user:42}:cart and {user:42}:profile. Queue libraries need the same: for BullMQ, use a prefix with braces, for example prefix: '{bull}'. Keyspace notifications are not supported.

Cost limits. Two optional limits cap the bill:

  • maxDataGb caps stored data. At the limit, Valkey evicts the least recently used keys that have a TTL; if no key has a TTL, writes fail with an out-of-memory error. Set TTLs on cached keys when you use it.
  • maxEcpuPerSecond caps request throughput (minimum 1,000). Requests above it are throttled.
TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: {      type: 'valkey-serverless',      properties: {        maxDataGb: 2,        maxEcpuPerSecond: 5000      }    }  });  return {    resources: { cache }  };});

Clients connect on port 6379. Port 6380 serves lower-latency, eventually consistent reads to clients that send READONLY; its connection details are the reader* parameters.

Node-based Valkey

TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: {      type: 'valkey',      properties: { instanceSize: 'cache.t4g.micro' }    }  });  return {    resources: { cache }  };});

This creates a single node running the latest Valkey version. It works with every Redis client and command pattern. Slow-query logging is enabled by default and sent to CloudWatch.

Redis OSS (existing caches)

AWS publishes no Redis OSS versions after 7.1, so new caches must use Valkey. engine.type: redis keeps caches created before Valkey support running on the version they have:

TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: {      type: 'redis',      properties: { instanceSize: 'cache.t4g.micro', version: '6.2' }    }  });  return {    resources: { cache }  };});

Switching to Valkey. Change engine.type from redis to valkey. AWS migrates the cache in place, which took about 20 minutes for a single cache.t4g.micro node: the endpoint and data stay, writes pause for a few seconds during a failover (longer when the cache has no replicas), and the switch cannot be reversed.

Changing engines later

ChangeResult
redis → valkeyIn place; brief write pause; cannot be reversed
valkey → redisNot supported
Serverless ↔ node-basedNot allowed: AWS would replace the cache, with a new endpoint and no data
Version upgradeIn place
Version downgradeNot supported

To move between serverless and node-based, create a new resource with a different name, move your application to it, then remove the old one.

Versions

Without engine.properties.version, a new cache gets the latest version and an existing cache keeps the version it runs. A Stacktape update never upgrades a cache on its own.

  • valkey-serverless: major versions 9, 8, 7.
  • valkey: 9.1, 9.0, 8.2, 8.1, 8.0, 7.2.
  • redis: 7.1, 7.0, 6.2, 6.0 (required).

Instance sizing

For valkey and redis, engine.properties.instanceSize determines the memory, CPU, and cost of every node (primary and replicas). You can change it after creation without data loss.

InstanceFamilyMemoryApprox. Valkey costBest for
cache.t4g.microBurstable (Graviton)0.5 GB~$0.013/hrDev/test, low-traffic apps
cache.t4g.smallBurstable (Graviton)1.37 GB~$0.026/hrSmall production, session stores
cache.m7g.largeGeneral purpose (Graviton)6.38 GB~$0.12/hrModerate production workloads
cache.r7g.largeMemory-optimized (Graviton)13.07 GB~$0.16/hrLarge datasets, heavy caching
  • t (burstable): cheapest, variable CPU performance. Good for development and intermittent traffic.
  • m (general purpose): consistent CPU performance for production workloads.
  • r (memory-optimized): highest memory-to-CPU ratio for large datasets.
  • Graviton suffix (g): ARM-based instances with better price-performance. Prefer t4g/m7g/r7g.

High availability (node-based)

Serverless caches replicate across Availability Zones automatically. Node-based caches have a single primary node by default.

Read replicas and automatic failover

Set numReplicaNodes to add read replicas, and enableAutomaticFailover to promote a replica when the primary fails.

TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: {      type: 'valkey',      properties: {        instanceSize: 'cache.m7g.large',        numReplicaNodes: 2,        enableAutomaticFailover: true      }    }  });  return {    resources: { cache }  };});

Each replica runs the same instance size as the primary, so two replicas triple the cost. Failover causes a brief connectivity interruption; clients should reconnect and retry.

Warning:

Deploy replicas first, then enable enableAutomaticFailover in a separate deployment.

Sharding

enableSharding splits the key space across numShards shards, each with its own primary and replicas. Clients must support cluster mode. Sharding requires numReplicaNodes >= 1 and must be set when the cache is created.

TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: {      type: 'valkey',      properties: {        instanceSize: 'cache.r7g.large',        enableSharding: true,        numShards: 3,        numReplicaNodes: 1      }    }  });  return {    resources: { cache }  };});

Most teams don't need sharding: add replicas to scale reads, or use valkey-serverless, which scales without sharding decisions.

Networking

Caches run inside your stack's VPC and have no public IP addresses.

  • vpc (default): any resource in the same VPC can connect: container workloads, batch jobs, and Lambda functions with joinDefaultVpc: true.
  • scoping-workloads-in-vpc: only resources that list this cache in their connectTo can connect.
TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: { type: 'valkey-serverless' },    accessibility: {      accessibilityMode: 'scoping-workloads-in-vpc'    }  });  return {    resources: { cache }  };});

To connect from your local machine, deploy a bastion host and use stacktape bastion tunnel or stacktape query redis.

Connecting from workloads

Use connectTo to grant a workload access. Stacktape opens the security group rules and injects connection details as environment variables, for example STP_CACHE_HOST and STP_CACHE_PORT for a resource named cache. To pass a complete connection URL, use the connectionString referenceable parameter:

TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: { type: 'valkey-serverless' }  });  const api = new LambdaFunction({    packaging: new JsBundleLambdaPackaging({      entryfilePath: './src/handler.ts'    }),    joinDefaultVpc: true,    connectTo: [cache],    environment: { REDIS_URL: $ResourceParam('cache', 'connectionString') }  });  return {    resources: { cache, api }  };});
Note:

Lambda functions must set joinDefaultVpc: true to reach the cache. Container workloads and batch jobs are already in the VPC.

Example handler

TypeScript
import { createClient } from 'redis';const client = createClient({ url: process.env.REDIS_URL });await client.connect();export const handler = async () => {  await client.set('key', 'value');  const result = await client.get('key');  return { statusCode: 200, body: JSON.stringify({ result }) };};

The connection string uses rediss:// (TLS) and the default user. All traffic is encrypted in transit, so the client library must support TLS.

Backups

Set automatedBackupRetentionDays to keep daily snapshots for that many days (maximum 35 for serverless caches). 0 (the default) disables backups.

TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: { type: 'valkey-serverless' },    automatedBackupRetentionDays: 7  });  return {    resources: { cache }  };});

Logging (node-based)

Node-based caches send slow-query logs to CloudWatch by default, retained for 30 days. View them with stacktape logs. Customize them with engine.properties.logging (format, retentionDays, disabled, and log forwarding). Serverless caches have no slow-query log.

Dev mode

During stacktape dev, the cache runs locally in Docker: Valkey for valkey and valkey-serverless, Redis for redis. For valkey-serverless, the local container runs in cluster mode, so CROSSSLOT errors show up locally the same way they do in AWS. Keyspace notifications still work locally; set dev.remote: true to test against the deployed cache.

TypeScript
export default defineConfig(() => {  const cache = new Redis({    defaultUserPassword: $Secret('redis', 'password'),    engine: { type: 'valkey-serverless' },    dev: {      remote: true    }  });  return {    resources: { cache }  };});

Migrating from redis-cluster

Stacktape v4 renamed redis-cluster to redis and moved node settings under engine.properties. To keep a deployed cache unchanged, set engine.type: redis with the version it runs (6.2 if engineVersion was not set):

YAML
# Stacktape v3cache:  type: redis-cluster  properties:    defaultUserPassword: $Secret('redis', 'password')    instanceSize: cache.t4g.micro# Stacktape v4cache:  type: redis  properties:    defaultUserPassword: $Secret('redis', 'password')    engine:      type: redis      properties:        instanceSize: cache.t4g.micro        version: '6.2'

The cache keeps its endpoint and data. Switch it to Valkey afterwards by changing engine.type to valkey.

Configuration reference

RedisProps5 properties
  • defaultUserPasswordRequiredstring

    Password of the default user. 16-128 printable ASCII characters.

  • engineRequiredValkeyServerlessEngine | ValkeyEngine | RedisOssEngine

    Engine: which software runs the cache and how AWS runs it.

  • accessibilityRedisAccessibility

    Network access control: vpc (default) or scoping-workloads-in-vpc (most restrictive).

  • Days to keep automated daily backups (snapshots). Set to 0 to disable. Maximum 35.

  • devDevModeConfig

    Dev mode: runs locally in Docker by default. Set remote: true to use the deployed cache.

Referenceable parameters

Reference these parameters with the $ResourceParam directive: $ResourceParam('<resource-name>', '<parameter>'). See referenceable parameters.

host

In case of NON-sharded cluster(default), this is a hostname of the primary instance that can be used for both reads and writes. In case of sharded cluster, this is cluster's configuration endpoint that can be used for all operations.

readerHost

Hostname (address) that can be used for reads only. (only available for NON-sharded clusters). If you use multiple replicas, it is advised to use readerHost for read operations to offload the primary host. ReaderHost automatically balances requests between available read replicas.

port

Port of the cluster.

sharding

Indicates whether cluster is sharded. Available values: enabled or disabled.