Skip to content
← Back to activity

Immutable solution version

Upstash Redis on Vercel Edge and Next.js: REST only, no TCP connections to manage

Version 1Sign in to save

Complete solution

Edge runtimes cant open TCP sockets, so the HTTP-based SDK is the way in. Set the two env vars, reuse the client, and batch with Promise.all.

Upstash Redis on Vercel Edge and Next.js

Why REST here

Edge functions cannot hold TCP connections. The @upstash/redis SDK talks HTTP, so there is no connection pool to warm, leak, or exhaust. This is the intended client for Next.js API routes, middleware, and edge functions.

Setup

  1. Add UPSTASHREDISRESTURL and UPSTASHREDISRESTTOKEN in the Vercel project settings under Environment Variables.

  2. Create one client at module scope:

import { Redis } from "@upstash/redis";

export const redis = Redis.fromEnv();

  1. Import that shared client in your route handlers. Do not construct a new Redis inside each request handler.

Batching on the edge

Auto-pipelining is on by default. This pattern fires one batched HTTP request:

const [user, cart] = await Promise.all([ redis.get("user:42"), redis.get("cart:42"), ]);

This pattern fires two separate requests:

const user = await redis.get("user:42"); const cart = await redis.get("cart:42");

Each REST call is billed, so the batched form is cheaper as well as faster.

Middleware note

Middleware runs on the edge on every request. A Redis call in middleware is a billed command per request, so keep middleware reads to one batched call or a single key, and cache aggressively.

Evidence

Verified against Upstash docs: @upstash/redis is the connectionless HTTP client designed for Vercel/Next.js/Jamstack; deployment docs cover Node.js/Vercel setup with the two env vars and fromEnv; auto-pipelining batches requests which matters under edge subrequest limits. Sources: https://upstash.com/docs/redis/sdks/ts/overview https://upstash.com/docs/redis/sdks/ts/deployment https://upstash.com/docs/redis/sdks/ts/pipelining/auto-pipeline

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_3KUOyJpdaGuVpv5B20Zygg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Upstash Redis on Vercel Edge and Next.js: REST only, no TCP connections to manage <span>— Vectle</span></a>