Solution skill
upstash/ratelimit setup: pick fixed window, sliding window, or token bucket
Complete solution
Shows how to fix upstash/ratelimit setup: pick fixed window, sliding window, or token bucket. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
npm install @upstash/ratelimit @upstash/redis import { Ratelimit } from "@upstash/ratelimit"; import { Redis } from "@upstash/redis"; const ratelimit = new Ratelimit({ redis: Redis.fromEnv(), limiter: Ratelimit.slidingWindow(10, "10 s"), analytics: true, prefix: "@upstash/ratelimit", }); const { success } = await ratelimit.limit("api"); if (!success) { return new Response("Too many requests", { status: 429 }); } Use a user ID, API key, or IP as the identifier to limit per caller, or a constant string to limit globally.
Steps
- npm install @upstash/ratelimit @upstash/redis
- import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
const ratelimit = new Ratelimit({ redis: Redis.fromEnv(), limiter: Ratelimit.slidingWindow(10, "10 s"), analytics: true, prefix: "@upstash/ratelimit",
});
- const { success } = await ratelimit.limit("api");
if (!success) { return new Response("Too many requests", { status: 429 }); }
- Use a user ID, API key, or IP as the identifier to limit per caller, or
a constant string to limit globally.
- Fixed window (10 per 10s): cheapest (2-3 commands per limit call).
Bursts can leak through at window boundaries.
Sliding window: smoother, fixes the boundary burst, costs more (4-5 commands per call) and is only an approximation.
Token bucket: constant-rate processing with configurable burst via maxTokens. Most expensive computationally.
- All three support dynamic limits. Token bucket is not supported in the
multi-region setup.
- Each limit() call runs Lua plus bookkeeping commands. With analytics
on, add one more command per call. At 1M requests a day on sliding window with analytics, that is roughly 5M commands a day: budget for it or use fixed window.
When to use
You are seeing this: Most expensive computationally. Use this skill when you run into "upstash/ratelimit setup: pick fixed window, sliding window, or token bucket".
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 ratelimit docs: npm install @upstash/ratelimit; Ratelimit with redis, limiter (fixedWindow/slidingWindow/tokenBucket), analytics flag, prefix option default @upstash/ratelimit; identifier per user/IP/key; algorithms page details pros/cons; costs page gives exact command counts per limit() call. Sources: https://upstash.com/docs/redis/sdks/ratelimit-ts/gettingstarted https://upstash.com/docs/redis/sdks/ratelimit-ts/algorithms https://upstash.com/docs/redis/sdks/ratelimit-ts/costs
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_IgUa_OH46gUaLd9zj0QK5Q" 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/ratelimit setup: pick fixed window, sliding window, or token bucket <span>— Vectle</span></a>