Databases

Set up Redis caching

4 min readUpdated April 2026

StackBlaze managed Redis runs as a Kubernetes Deployment with a stable ClusterIP Service providing the internal hostname redis.internal:6379. Because it runs inside your project namespace, 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 7

redis.internal:6379

Deployment

Session Store

connect-redis

TTL-keyed hashes

Job Queue

BullMQ

sorted sets

Auto-injected connection string

.env (injected)

REDIS_URL=redis://redis.internal: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

  • Kubernetes Deployment: Redis runs as a regular Deployment (not StatefulSet) since it can be reconstructed from a persistent volume or warm-started. One replica by default.
  • ClusterIP Service: provides the stable redis.internal DNS entry. Traffic never leaves your namespace.
  • In-memory by default: data is lost if the Redis pod restarts unless you enable AOF (append-only file) persistence in the service configuration. For session data with short TTLs this is usually acceptable. For job queues, enable AOF.
  • Memory limit: set a maxmemory policy (e.g. allkeys-lru) in the Redis config to evict stale cache keys automatically when the limit is reached.

Step by step

01

Add a Redis instance

In the dashboard click "New service" → "Database" → "Redis". Choose a version (7.x) and memory limit. StackBlaze provisions a Kubernetes Deployment (Redis is stateless enough not to need a StatefulSet for most use cases) and a ClusterIP Service.

02

Attach to a service

Open your web service or worker, go to "Environment", click "Attach database" and select the Redis instance. REDIS_URL is injected as a secret automatically: redis://redis.internal:6379. No password unless you enable AUTH in the Redis config.

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 within the Kubernetes cluster so latency is sub-millisecond. Redis is not persisted by default, for persistence enable AOF or RDB snapshots in the service config.