Documentation

Advanced

Redis

Add a shared Redis cache to VitNode for faster session lookups, distributed rate limiting, and multi-instance WebSocket broadcasts.

Redis serves as VitNode's shared cache and pub/sub broker across horizontally scaled instances. Redis is optional: without it, VitNode degrades gracefully to in-memory counters and local delivery.

Quick start

1. Configure Environment Variables

.env
REDIS_URL=redis://localhost:6379 // [!code ++]
REDIS_PASSWORD=root // [!code ++]

2. Connect in API Configuration

apps/api/src/vitnode.api.config.ts
import { buildApiConfig } from "@vitnode/core/vitnode.config"

export const vitNodeApiConfig = buildApiConfig({
  redis: process.env.REDIS_URL
    ? { url: process.env.REDIS_URL, password: process.env.REDIS_PASSWORD }
    : undefined,
})

What Redis Powers

FeatureWithout RedisWith Redis
API Domain CacheNo-op (always executes DB query)Shared key-value store via c.get("cache")
Rate LimiterIn-memory counters per instanceDistributed sliding-window across cluster
WebSocket RealtimeSingle instance onlyCross-instance delivery via Redis Pub/Sub; required on serverless hosts (details)
SessionsDatabase queries on every requestInstant cache hits with DB fallback

Local Development (Docker)

Start a Redis container with Docker Compose:

docker-compose.yml
services:
  redis:
    image: redis:7-alpine
    restart: always
    ports:
      - "6379:6379"
    command: redis-server --requirepass root

Using the Cache in API Routes

Use remember with a key, a TTL in seconds, and a loader to cache expensive database lookups:

const posts = await c.get("cache").remember(
  "featured_posts",
  60,
  async () => await fetchFeaturedPostsFromDB(),
)

See Cache API data for invalidation and the full method list.


Check Redis in the AdminCP

Open Debug Panel → Cache in the AdminCP (/admin/core/debug/cache). It shows whether Redis is connected, unreachable or not configured (then the cache lives in memory), and whether realtime events reach every instance. An unreachable Redis also shows up on the Debug Panel's Overview.

Learn More