Upstash
Upstash
Serverless Redis/Kafka. Provides a REST API, edge-friendly.
Upstash
Serverless Redis/Kafka. Provides a REST API, edge-friendly.
Upstash is a Redis and Kafka cloud service optimized for serverless environments. It provides an HTTP/REST API, so it can be used easily in edge environments (Vercel Edge Functions, Cloudflare Workers) without a traditional TCP-based Redis client.
Its main features include request-based pricing (you pay only for what you use), automatic scaling, global replication (low latency), and TLS encryption. In addition to Redis, it also provides QStash (message queue) and Vector (vector DB) services.
It is well suited for implementing session management, caching, rate limiting, and more in serverless architectures without cold start issues.
// Upstash Redis with REST API
import { Redis } from '@upstash/redis'
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
})
// Edge Functionμμ μΊμ μ¬μ©
export default async function handler(req) {
const userId = req.query.id
// μΊμ νμΈ
const cached = await redis.get(`user:${userId}`)
if (cached) {
return Response.json(cached)
}
// DB μ‘°ν ν μΊμ μ μ₯ (60μ΄ TTL)
const user = await fetchUserFromDB(userId)
await redis.setex(`user:${userId}`, 60, user)
return Response.json(user)
}
// Rate Limiting with Upstash
import { Ratelimit } from "@upstash/ratelimit"
const ratelimit = new Ratelimit({
redis: redis,
limiter: Ratelimit.slidingWindow(10, "10 s"), // 10μ΄μ 10ν
})
export async function middleware(req) {
const ip = req.ip ?? "127.0.0.1"
const { success } = await ratelimit.limit(ip)
if (!success) {
return new Response("Too Many Requests", { status: 429 })
}
}
Frontend developer: "I'm trying to use Redis in a Vercel Edge Function, but the connection isn't working."
Backend developer: "The Edge runtime doesn't support TCP connections, so a regular Redis client doesn't work. If you use Upstash, it communicates over HTTP REST, so it works."
Frontend developer: "How's the performance?"
Backend developer: "It supports global regions, so latency is under 10ms. And since it's serverless, there's no cold start either."
Interviewer: "How do you manage state in a serverless environment?"
Candidate: "I use Upstash Redis. It provides an HTTP REST API, so it can be used in Edge Functions too, and with request-based pricing, I can maintain the cost efficiency of serverless."
Interviewer: "Do you have experience implementing session management?"
Candidate: "Yes, I stored refresh tokens using a JWT + Upstash combination and handled automatic expiration with TTL. I also implemented rate limiting easily with the @upstash/ratelimit library."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
Reviewer: "It looks like you're creating a new Redis instance on every request here."
Developer: "Isn't that unavoidable since it's serverless?"
Reviewer: "If you create it once at the top of the file, it gets reused on warm starts. Please move it outside the function. Both cost and performance will improve."
Developer: "Oh, I see. I'll move it to the module level."