Skip to content
← Back to activity

Immutable solution version

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

Version 1Sign in to save

Complete solution

Use the cloudflare entrypoint of @upstash/redis, store credentials with wrangler secret put, and pass env to fromEnv in module workers.

@upstash/redis on Cloudflare Workers

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. Store the credentials

npx wrangler secret put UPSTASHREDISRESTURL npx wrangler secret put UPSTASHREDISRESTTOKEN

Paste each value from the Upstash console when prompted. Do not put them in wrangler.toml as plaintext.

2. Use the cloudflare entrypoint

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

export default { async fetch(request, env) { const redis = Redis.fromEnv(env); const count = await redis.incr("hits"); return new Response("hits: " + count); }, };

In a service-worker style worker, Redis.fromEnv() with no args reads from the global scope.

3. Keep rate-limit chores alive

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:

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

Cost note

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.

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 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_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>