---
name: add-upstash-redis
description: Adds Upstash Redis over HTTP for a serverless function, with one shared client, cache-aside with a TTL, sliding sessions, and a 429 limit. Use when the user wants Upstash, a Redis cache without a TCP socket, or a session store on the edge.
license: MIT
metadata:
  category: backend
  tags: "redis,nodejs,api"
  author: johnny-bravo
  version: "1.0"
  updated: "2026-11-10"
  status: published
  aliases: "upstash redis, redis over http, cache-aside, serverless redis"
  source_url: "https://github.com/github/awesome-copilot/tree/5269f7ea2d6c3a9bfda5ed878b6726cf509121fd/skills/upstash-redis"
  source_license: MIT
  copyright: "Copyright GitHub, Inc."
  original_author: "Upstash (github/awesome-copilot, upstash-redis)"
  modified: "Adapted by TheSkillVault on 2026-11-10: rewritten as a short build order with smaller original snippets for cache, session and a 429 limit; dropped the long route sample and vendor doc links; added secret handling, gotchas and done-when. Not the original file."
  imported_on: "2026-11-10"
---

# Add Upstash Redis

Upstash Redis speaks HTTP. A short-lived function can call it without a TCP pool. Use it for a cache, a session or a small limit.

## When to use
- A serverless or edge handler needs a shared cache.
- Sessions must survive a cold start.
- You want a 429 limit that many instances share.

## When not to use
- The app already has a long-lived TCP Redis client. Keep that client.
- You need vector search. Use a vector store, not this cache.
- The limit is part of chatbot abuse control. Use add-chatbot-guardrails-and-rate-limits for that policy.

## Steps
1. Create an Upstash Redis database. Copy the REST URL and the REST token into env vars named `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`. Do not commit them.
2. Install `@upstash/redis`. Add `@upstash/ratelimit` only if you need a limit.
3. Create one client per module with `Redis.fromEnv()`. Do not construct it inside the handler.
4. Cache with a TTL. On a miss, read the source of truth, then `set` with `{ ex: seconds }`. On a write, update or delete the same key.
5. For sessions, store an id in an `HttpOnly` cookie and the payload in Redis under `session:<id>`. Refresh the TTL on read. Delete the key on sign-out.
6. For a limit, build one `Ratelimit` at module scope. `slidingWindow` is a sound default. Key it by user id when you have one, otherwise by the platform IP. Return 429 when `success` is false.
7. Call `ping` once in a scratch script. You want the string `PONG`.

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

export const redis = Redis.fromEnv();

export async function getProduct(id: string) {
  const key = `product:${id}`;
  const hit = await redis.get<Product>(key);
  if (hit) return hit;
  const row = await db.products.find(id);
  if (row) await redis.set(key, row, { ex: 600 });
  return row;
}
```

## Gotchas
- The client already encodes objects. Do not `JSON.stringify` before `set`, or the next `get` will surprise you.
- A key with no TTL lives until something deletes it. Always pass `{ ex }`.
- The REST token is a secret. Keep it in `.env` or the host's secret store. Never send it to the browser.
- Do not trust a raw forwarded-for header. Use the platform's client IP when the host provides one.
- On the edge, some limit calls return a `pending` promise. Wait for it before the isolate exits.
- `KEYS` on a large database is a bad idea. Use a known prefix and exact keys.

## Portability
Works with any host that can call HTTPS, including Node and edge workers. If you use a long-running server, a TCP client is the usual equivalent.

## Done when
- `ping` returns `PONG` from the deployed environment.
- A second read of a cached key does not hit the database, and the key has a TTL.
- The REST token is not in git or in client code.
- A limit, if you added one, returns 429 after the budget and succeeds again later.

## Example prompt
"Add Upstash Redis to this serverless route. Cache the product by id for 10 minutes, and keep the REST token in env."

---
Adapted from "upstash-redis" in github/awesome-copilot (MIT, Copyright GitHub, Inc.). See LICENSE.txt.
