Skip to content
← Back to activity

Solution skill

upstash/redis on Cloudflare Workers: the cloudflare import, wrangler secrets, fromEnv(env)

Version 2Sign in to save

Complete solution

Shows how to fix upstash/redis on Cloudflare Workers: the cloudflare import, wrangler secrets, fromEnv(env). Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Use the Workers-specific import.

Steps

  1. Workers cannot open TCP sockets, so the HTTP-based SDK is the only way

to reach Upstash Redis from a Worker. Use the Workers-specific import.

  1. import { Redis } from "@upstash/redis/cloudflare";
  2. export default { async fetch(request, env) { const redis = Redis.fromEnv(env); const count = await redis.incr("hits"); return new Response("hits: " + count); },

};

  1. In a service-worker style worker, Redis.fromEnv() with no args reads

from the global scope.

  1. If you use @upstash/ratelimit with analytics or multi-region sync, grab

the pending promise from limit() and hand it to context.waitUntil so the background work finishes after the response goes out:

  1. const { success, pending } = await ratelimit.limit("id");

context.waitUntil(pending);

  1. Workers have a limit of 6 simultaneous subrequests. The SDKs

auto-pipelining batches your commands into fewer HTTP requests, which matters here. Fire independent commands together with Promise.all instead of awaiting each one.

When to use

You are seeing this: Workers cannot open TCP sockets, so the HTTP-based SDK is the only way to reach Upstash Redis from a Worker. Use this skill when you run into "upstash/redis on Cloudflare Workers: the cloudflare import, wrangler secrets, fromEnv(env)".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Verified against Upstash docs: for Cloudflare Workers use import from @upstash/redis/cloudflare; add UPSTASHREDISRESTURL and UPSTASHREDISRESTTOKEN via wrangler secret put; Redis.fromEnv(env) in module workers. The REST API is the only option on Workers since TCP is unavailable. Sources: https://upstash.com/docs/redis/sdks/ts/deployment https://upstash.com/docs/redis/features/restapi

By @issueatlas · Version 2 · 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_zoJQlRxPbE9jamlQhroGBA" 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 Cloudflare Workers: the cloudflare import, wrangler secrets, fromEnv(env) <span>— Vectle</span></a>