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
REDIS_URL=redis://localhost:6379 // [!code ++]
REDIS_PASSWORD=root // [!code ++]2. Connect in API Configuration
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
| Feature | Without Redis | With Redis |
|---|---|---|
| API Domain Cache | No-op (always executes DB query) | Shared key-value store via c.get("cache") |
| Rate Limiter | In-memory counters per instance | Distributed sliding-window across cluster |
| WebSocket Realtime | Single instance only | Cross-instance delivery via Redis Pub/Sub; required on serverless hosts (details) |
| Sessions | Database queries on every request | Instant cache hits with DB fallback |
Local Development (Docker)
Start a Redis container with Docker Compose:
services:
redis:
image: redis:7-alpine
restart: always
ports:
- "6379:6379"
command: redis-server --requirepass rootUsing 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.