◀ Knowledge hub

02, Data & state

Rate limiting and queues at the edge with Upstash

codeAmani Labs Engineering
Cinematic still for Rate limiting and queues at the edge with Upstash

The edge needs state, but not a heavy connection

Code running at the edge or in a serverless function cannot hold a long lived connection to a traditional Redis the way a always on server would. Each invocation is short and may run anywhere. What it needs is state it can reach over plain HTTP, with no connection to manage, and that is the gap Upstash fills.

Rate limiting that protects everything downstream

The first thing most APIs need and most forget is a rate limit. Without one, a single misbehaving client can exhaust your database connections or your model provider budget for everyone. A sliding window limiter keyed by client identity is a few lines and protects the whole system.

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

const limiter = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(10, "10 s"),
});

const { success } = await limiter.limit(ip);
if (!success) {
  return new Response("Too many requests", { status: 429 });
}

Because the limiter speaks HTTP, it works the same from an edge middleware, a serverless route, or a long running worker. There is no connection pool to size and no socket to leak.

Queues for work that should not block the response

The second common need is to move slow work out of the request. When a user submits a form that triggers an email, an embedding, and a webhook, the user should not wait for all three. Push a message to a queue and return immediately; a consumer does the slow part.

await qstash.publishJSON({
  url: "https://api.example.com/jobs/process-intake",
  body: { intakeId },
});

Why this fits the stack

This is the data and state layer doing its quieter job. Postgres holds the durable truth, the vector index holds the embeddings, and the edge key value store holds the fast, ephemeral state: counters, locks, and queued work. Each piece does the thing it is best at, and the edge gets the lightweight state it needs without dragging a heavy connection along.

Qualified conversation

Have a build to de-risk? Let's talk.

Tell us what you are building. We respond within two business days.