06.06 — lib/redis.ts + Upstash#
Source file:
lib/redis.ts
Code#
import { Redis } from "@upstash/redis";
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
});
export default redis;Why @upstash/redis rather than ioredis#
| Criterion | ioredis | @upstash/redis |
|---|---|---|
| Protocol | TCP / RESP | HTTP / REST |
| Edge runtime | ❌ No | ✅ Yes |
| Connection | Persistent | Per-request |
| Latency (warm) | < 1ms | 50–100ms |
| Latency (cold) | 200ms+ | 50–100ms |
| Cost | Variable | Pay-per-request |
| Setup | Self-hosted Redis | Upstash dashboard (managed) |
Edge doesn’t accept persistent TCP connections — each worker is ephemeral. Upstash REST it is.
Singleton#
const redis = new Redis({...});
export default redis;Created once at module import (cold start), reused for every request the worker serves. With reasonable traffic the worker stays warm.
Env var consumption#
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,UPSTASH_REDIS_REST_URL = "https://us1-something-12345.upstash.io"
UPSTASH_REDIS_REST_TOKEN = "AcdefGHi..."The token is JWT-like, generated by Upstash and scoped to a specific Redis database.
vercel env add#
The README details:
vercel env add API_SECRET
vercel env add UPSTASH_REDIS_REST_URL
vercel env add UPSTASH_REDIS_REST_TOKEN
vercel deployThree vercel env adds, then vercel deploy. No local config needed.
API#
| Redis method | Endpoint | Use |
|---|---|---|
redis.set(key, value, { ex: ttl }) | /api/otp | SET with TTL |
redis.scan(cursor, { match, count }) | /api/otp-history | Cursor-based SCAN |
redis.mget(...keys) | /api/otp-history | MGET (multi-get) |
Error handling#
@upstash/redis throws on network or server errors. The code doesn’t catch them — they bubble up to Vercel’s handler as 500 Internal Server Error.
- On Igoristan: the user sees an OTP error and retries.
- On Ocarina:
transient_errorscatches and retries.
No explicit timeout#
const redis = new Redis({
url: ...,
token: ...,
});No explicit timeout. @upstash/redis defaults to ~5s. The Edge runtime has its own hard limit (10s on Vercel) — so even if the client doesn’t time out, Vercel will.