Set up Redis caching

4 min readUpdated April 2026

StackBlaze managed Redis is a fully Redis-compatible in-memory data store, run for you as a dedicated instance with a stable internal hostname (myapp-redis:6379). Because it sits on your project’s private network, connections never leave the cluster, latency is typically under 1 ms.

Redis is useful for two broad categories: caching (session data, query results, rate-limit counters) and job queues (background workers consuming tasks via a list or sorted set). Both patterns are shown below.

Architecture

TCP:6379sessionsjob queue
App Service

REDIS_URL

injected as secret

Redis

myapp-redis:6379

managed add-on

Session Store

connect-redis

TTL-keyed hashes

Job Queue

BullMQ

sorted sets

Auto-injected connection string

.env (injected)

REDIS_URL=redis://myapp-redis:6379

Session middleware (Express + connect-redis)

session.ts

import session from 'express-session'

import { createClient } from 'redis'

import { RedisStore } from 'connect-redis'

 

const redisClient = createClient({

url: process.env.REDIS_URL,

})

await redisClient.connect()

 

app.use(session({

store: new RedisStore({ client: redisClient }),

secret: process.env.SESSION_SECRET,

resave: false,

saveUninitialized: false,

cookie: { secure: true, maxAge: 86400000 }

}))

BullMQ job queue setup

queue.ts

import { Queue, Worker } from 'bullmq'

 

// Producer, enqueue a job from your web service

const emailQueue = new Queue('emails', {

connection: { url: process.env.REDIS_URL }

})

await emailQueue.add('welcome', { to: 'user@example.com' })

 

// Consumer, your Worker service processes jobs

const worker = new Worker('emails', async job => {

await sendEmail(job.data.to)

}, { connection: { url: process.env.REDIS_URL } })

Under the hood

  • Dedicated managed instance: a fully Redis-compatible data store, run and maintained for you. Start with a single node, or enable replicas for failover-ready high availability.
  • Stable internal hostname: reachable at myapp-redis on your project's private network. Traffic never leaves the cluster.
  • In-memory by default: data is lost on restart unless you enable AOF (append-only file) persistence in the add-on config. For session data with short TTLs this is usually fine. For job queues, enable AOF.
  • Memory limit: set a maxmemory policy (e.g. allkeys-lru) to evict stale cache keys automatically when the limit is reached.

Step by step

01

Add a Redis add-on

From your app, open Add-ons and provision Redis. Choose a version and memory limit. StackBlaze provisions a dedicated, fully Redis-compatible instance reachable on your project's private network, optionally with replicas for high availability.

02

Attach it to your app

Attaching the add-on injects REDIS_URL automatically: redis://myapp-redis:6379. Access is scoped to your project; turn on a password in the add-on config if you want authentication on top of network isolation.

03

Use REDIS_URL in your application

Read REDIS_URL from process.env (Node.js) or os.environ (Python). Every major Redis client, ioredis, node-redis, redis-py, Jedis, accepts a URL string directly. No manual hostname configuration needed.

04

Implement caching or queues

Use Redis for session storage, rate limiting, caching expensive queries, or job queues with BullMQ. The connection stays on your project's private network so latency is sub-millisecond. It is in-memory by default, enable AOF or RDB persistence in the add-on config if you need durability.