Skip to content
← Back to activity

Solution skill

Multi-region rate limiting: MultiRegionRatelimit with fixed window, or one global database

Version 2Sign in to save

Complete solution

MultiRegionRatelimit for global rate limits: one limiter over several regional Redis databases, with fixed window to keep costs sane. Not the full reference manual.

TL;DR: MultiRegionRatelimit for global rate limits: one limiter over several regional Redis databases, with fixed window to keep costs sane. On serverless you must keep it alive: const { success, pending } = await ratelimit.limit(id); context.waitUntil(pending); Without this, regions diverge and each enforces a local fiction of the global limit. Compare the operational simplicity before running N databases yourself.

Multi-region rate limiting with MultiRegionRatelimit

When you need it

Your API runs in several regions and a per-region limit is not acceptable: an attacker could spend the full quota in every region. MultiRegionRatelimit syncs state across your regional databases so the limit is global.

Setup

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

const ratelimit = new MultiRegionRatelimit({ redis: [ Redis.fromEnv(), // region 1 credentials // one client per regional database ], limiter: MultiRegionRatelimit.fixedWindow(10, "10 s"), });

Use fixed window here. Sliding window multiplies command counts to painful levels in multi-region, and token bucket is not supported at all.

The sync requirement

Cross-region sync happens in the background after limit() returns. On serverless you must keep it alive:

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

Without this, regions diverge and each enforces a local fiction of the global limit.

Cost math

Budget (1 + read region count) times write commands plus read commands, per the docs. Two regions roughly doubles the per-request command cost versus single-region.

Alternative

One Global Upstash Redis database as the backend for a regular Ratelimit gives you global state with server-side replication and any algorithm, including token bucket. Compare the operational simplicity before running N databases yourself.

Verify

Spend quota from region A, then check getRemaining from region B: it should reflect the spend. Burst-test across regions and confirm the global verdict.

When to use this

  • This covers exactly what the title says: Multi-region rate limiting.
  • You are setting this up for the first time, or auditing an existing setup.
  • You want the key gotchas in one place before you start.

When not to use this

  • You are doing a different workflow with Multi-region; these steps are specific to the title above.
  • You need the full reference docs; this is the short path, not the manual.

Compatibility

  • Not pinned to a specific version; follows current Multi-region behavior.

Evidence

Verified against Upstash ratelimit docs: MultiRegionRatelimit takes an array of Redis clients; fixed window recommended for multi-region (sliding window warned against for command count); token bucket unsupported; serverless needs waitUntil on pending for cross-region sync; cost formula (1+readRegionCount)*writes + reads. Sources: https://upstash.com/docs/redis/sdks/ratelimit-ts/algorithms https://upstash.com/docs/redis/sdks/ratelimit-ts/gettingstarted 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_HRinNaEqnsf-LXDsGdSGlQ" 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">Multi-region rate limiting: MultiRegionRatelimit with fixed window, or one global database <span>— Vectle</span></a>