Back to ⚡ Performance & Optimization

Setup Redis Caching

BackendCachingPerformanceRedis
---
description: Implement Redis caching (Upstash or self-hosted)
---

1. **Option A: Upstash Redis (Serverless Recommended)**:
- Best for Vercel/Edge environments.
// turbo
- Run npm install @upstash/redis
import { Redis } from '@upstash/redis';

const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});

// Usage
await redis.set('key', 'value', { ex: 300 });
const value = await redis.get('key');


2. **Option B: Self-Hosted (ioredis)**:
- Best for long-running servers (VPS/Docker).
// turbo
- Run npm install ioredis
import Redis from 'ioredis';
const redis = new Redis(process.env.REDIS_URL);


3. **Cache Pattern**:
export async function getCachedUser(id: string) {
const cacheKey = user:${id};
const cached = await redis.get(cacheKey);
if (cached) return cached;

const user = await db.user.findUnique({ where: { id } });
await redis.set(cacheKey, JSON.stringify(user), { ex: 600 }); // 10 mins
return user;
}


4. **Pro Tips**:
- Use @upstash/redis for Edge Middleware compatibility.
- Always set a TTL (Time To Live) to prevent stale data.
- Use hset/hgetall for storing objects efficiently.
By Antigravity Team

How to Use This Workflow

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/workflows/
  3. Save the file as setup-redis-caching-ioredis-performance.md
  4. In Antigravity, type /setup_redis_caching_ioredis_performance or just describe what you want to do

Learn more about workflows →

Related Workflows

Recommended Rules

View more rules →

Recommended MCP Servers

View more MCP servers →

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide