Redis is fast but needs a server running 24/7. Upstash is serverless Redis — pay per request, scale to zero, and access from Cloudflare Workers, Vercel Edge, and any serverless environment via HTTP.
What Upstash Gives You for Free
- 10,000 commands/day on free tier
- 256MB storage on free tier
- HTTP/REST API — works in edge runtimes (no TCP needed)
- Global replication — read from nearest region
- Redis-compatible — all commands work
- QStash — serverless message queue included
- Ratelimit SDK — rate limiting in 3 lines
- Vector — serverless vector database for AI/RAG
Quick Start
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!
});
// Works everywhere — Node, Deno, Bun, Cloudflare Workers, Vercel Edge
await redis.set('user:1', JSON.stringify({ name: 'Alice', plan: 'pro' }));
const user = await redis.get('user:1');
Rate Limiting (3 Lines)
import { Ratelimit } from '@upstash/ratelimit';
import { Redis } from '@upstash/redis';
const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(10, '10 s'), // 10 requests per 10 seconds
});
// In your API handler
export async function POST(req: Request) {
const ip = req.headers.get('x-forwarded-for') ?? '127.0.0.1';
const { success, limit, remaining } = await ratelimit.limit(ip);
if (!success) {
return new Response('Rate limited', {
status: 429,
headers: { 'X-RateLimit-Limit': limit.toString(), 'X-RateLimit-Remaining': remaining.toString() }
});
}
// Process request...
}
Caching API Responses
async function getCachedData(key: string, fetcher: () => Promise<any>, ttl = 3600) {
const cached = await redis.get(key);
if (cached) return cached;
const fresh = await fetcher();
await redis.set(key, JSON.stringify(fresh), { ex: ttl });
return fresh;
}
// Usage
const products = await getCachedData(
'products:featured',
() => db.products.findMany({ where: { featured: true } }),
300 // 5 minutes
);
QStash (Serverless Message Queue)
import { Client } from '@upstash/qstash';
const qstash = new Client({ token: process.env.QSTASH_TOKEN! });
// Publish a message (runs your endpoint later)
await qstash.publishJSON({
url: 'https://myapp.com/api/process',
body: { userId: '123', action: 'send-email' },
retries: 3,
delay: '10s' // Run after 10 seconds
});
// Schedule recurring (cron)
await qstash.publishJSON({
url: 'https://myapp.com/api/daily-report',
cron: '0 9 * * *' // Every day at 9 AM
});
Session Storage
// Store session in Redis (expires in 24 hours)
async function createSession(userId: string) {
const sessionId = crypto.randomUUID();
await redis.set(`session:${sessionId}`, userId, { ex: 86400 });
return sessionId;
}
async function getSession(sessionId: string) {
return await redis.get(`session:${sessionId}`);
}
Upstash vs Redis Cloud vs ElastiCache vs Dragonfly
| Feature | Upstash | Redis Cloud | ElastiCache | Dragonfly |
|---|---|---|---|---|
| Serverless | Yes | Partial | No | No |
| Free tier | 10K cmd/day | 30MB | None | Self-hosted |
| Edge support | HTTP API | TCP only | TCP only | TCP only |
| Pricing | Per-request | Per-GB | Per-hour | Self-hosted |
| Setup | 30 seconds | 5 minutes | 30+ minutes | 30+ minutes |
| Global replication | Built-in | Paid add-on | Manual | No |
The Verdict
Upstash makes Redis work in the serverless world. HTTP API for edge runtimes, pay-per-request pricing, built-in rate limiting, and a generous free tier. If you need Redis without managing infrastructure, Upstash is the answer.
Need help building production web scrapers or data pipelines? I build custom solutions. Reach out: spinov001@gmail.com
Check out my awesome-web-scraping collection — 400+ tools for extracting web data.
Top comments (0)