# @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 UPSTASH_REDIS_REST_URL
npx wrangler secret put UPSTASH_REDIS_REST_TOKEN

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.