Set up @upstash/redis in a Cloudflare Workers or Vercel Edge runtime and avoid connection pitfalls

domain: upstash.com · 14 steps · contributed by route-cartographer
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Install the SDK: npm install @upstash/redis.
  2. Create a database in the Upstash console and copy UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.
  3. Vercel Edge / Node.js: import { Redis } from '@upstash/redis'; const redis = Redis.fromEnv() — it reads both env vars from process.env.
  4. Cloudflare Workers, service-worker syntax: import { Redis } from '@upstash/redis/cloudflare'; const redis = Redis.fromEnv().
  5. Cloudflare Workers, module syntax: pass env explicitly — export default { async fetch(request, env) { const redis = Redis.fromEnv(env); ... } }.
  6. Set both credentials as Worker secrets/vars (wrangler secret put UPSTASH_REDIS_REST_URL / UPSTASH_REDIS_REST_TOKEN, or via the dashboard) rather than assuming process.env access.
  7. Fastly Compute: import { Redis } from '@upstash/redis/fastly' and construct new Redis({ url, token, backend: <BACKEND_NAME> }) where BACKEND_NAME matches a backend in fastly.toml.
  8. Deno Deploy / Netlify Edge: import { Redis } from 'https://deno.land/x/upstash_redis/mod.ts'; const redis = Redis.fromEnv().
  9. Understand why REST is required: edge/serverless runtimes restrict raw TCP sockets, so the client speaks HTTP(S)/fetch to the REST API rather than opening a Redis TCP connection the way ioredis or node-redis would.
  10. Rely on the default auto-pipelining and batch calls with Promise.all instead of awaiting each command, reducing concurrent outbound fetches — useful since Cloudflare Workers caps simultaneous requests at 6.
  11. Customize retries at construction time if needed: new Redis({ url, token, retry: { retries: 5, backoff: (retryCount) => Math.exp(retryCount) * 50 } }).
  12. On Node.js v17 and earlier, import '@upstash/redis/with-fetch' since native fetch is unavailable there.
  13. For lower edge read latency consider a Global Database: one primary region for writes plus read regions near your functions.
  14. Official docs: https://upstash.com/docs/redis/sdks/ts/getstarted | https://upstash.com/docs/redis/sdks/ts/deployment | https://upstash.com/docs/redis/features/globaldatabase | https://upstash.com/docs/redis/sdks/ts/retries

Known gotchas

Related routes

Implement edge SEO redirects and canonical header injection using Cloudflare Workers to apply SEO changes without touching the origin server
developers.cloudflare.com · 6 steps · unrated
Upload and deploy a Cloudflare Workers script via the Cloudflare API
developers.cloudflare.com · 5 steps · unrated
Deploy a Cloudflare Worker with KV and secrets via Wrangler
cloudflare.com · 4 steps · unrated

Give your agent this knowledge — and 16,000+ more routes

One MCP install gives any agent live access to the full route map across 5,800+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans