Upstash provides serverless Redis and Kafka accessible via HTTP — perfect for edge functions, serverless, and environments where TCP connections aren't available.
Why Upstash?
- HTTP-based — works in Cloudflare Workers, Vercel Edge, Deno Deploy
- Pay per request — no idle charges, scale to zero
- Free tier — 10K commands/day, 256MB storage
- Global replication — read from the nearest region
Quick Start
npm install @upstash/redis
import { Redis } from '@upstash/redis';
const redis = new Redis({
url: 'https://YOUR_REDIS.upstash.io',
token: 'YOUR_TOKEN',
});
// Basic operations
await redis.set('key', 'value');
const value = await redis.get('key');
// JSON
await redis.set('user:1', { name: 'Alice', age: 30 });
const user = await redis.get<{ name: string; age: number }>('user:1');
// Expire
await redis.set('session', 'data', { ex: 3600 }); // 1 hour
Rate Limiting
npm install @upstash/ratelimit
import { Ratelimit } from '@upstash/ratelimit';
const ratelimit = new Ratelimit({
redis,
limiter: Ratelimit.slidingWindow(10, '10s'), // 10 requests per 10 seconds
});
// In your API route
export async function POST(request: Request) {
const ip = request.headers.get('x-forwarded-for') ?? '127.0.0.1';
const { success, limit, remaining } = await ratelimit.limit(ip);
if (!success) {
return new Response('Too Many Requests', {
status: 429,
headers: {
'X-RateLimit-Limit': limit.toString(),
'X-RateLimit-Remaining': remaining.toString(),
},
});
}
// Process request...
}
QStash (Message Queue)
npm install @upstash/qstash
import { Client } from '@upstash/qstash';
const qstash = new Client({ token: 'YOUR_QSTASH_TOKEN' });
// Publish message
await qstash.publishJSON({
url: 'https://yourapp.com/api/process',
body: { orderId: '123', action: 'fulfill' },
retries: 3,
});
// Scheduled message
await qstash.publishJSON({
url: 'https://yourapp.com/api/reminder',
body: { userId: '456' },
delay: 3600, // 1 hour
});
// Cron job
await qstash.publishJSON({
url: 'https://yourapp.com/api/daily-report',
body: {},
cron: '0 9 * * *', // Daily at 9 AM
});
Vector (AI Similarity Search)
npm install @upstash/vector
import { Index } from '@upstash/vector';
const index = new Index({
url: 'YOUR_VECTOR_URL',
token: 'YOUR_VECTOR_TOKEN',
});
// Upsert vectors
await index.upsert([
{ id: 'doc1', vector: [0.1, 0.2, 0.3], metadata: { title: 'Hello' } },
{ id: 'doc2', vector: [0.4, 0.5, 0.6], metadata: { title: 'World' } },
]);
// Query similar
const results = await index.query({
vector: [0.1, 0.2, 0.3],
topK: 5,
includeMetadata: true,
});
REST API (No SDK Needed)
# Set
curl https://YOUR_REDIS.upstash.io/set/mykey/myvalue \
-H 'Authorization: Bearer YOUR_TOKEN'
# Get
curl https://YOUR_REDIS.upstash.io/get/mykey \
-H 'Authorization: Bearer YOUR_TOKEN'
# Pipeline
curl https://YOUR_REDIS.upstash.io/pipeline \
-H 'Authorization: Bearer YOUR_TOKEN' \
-d '[["SET","key1","val1"],["GET","key1"]]'
Need caching for your scraping pipeline? Check out my Apify actors for web scraping with built-in caching. Email spinov001@gmail.com for custom solutions.
Upstash, Redis Cloud, or Dragonfly — which Redis provider do you use? Share below!
Top comments (0)