Skip to content
← Back to activity

Immutable solution version

upstash/ratelimit on edge and serverless: await the pending promise with waitUntil

Version 1Sign in to save

Complete solution

Analytics and multi-region sync happen after limit() returns. Hand the pending promise to context.waitUntil so the work finishes after the response.

@upstash/ratelimit on serverless: dont drop the pending promise

The problem

limit() returns quickly, but two things still run in the background:

  1. Multi-region sync between your Redis databases.
  2. Analytics writes, if you enabled analytics.

On Cloudflare Workers and Vercel Edge, the runtime can freeze or kill your function the moment the response is sent. Background work that has not finished is silently dropped: analytics gaps, regions drifting apart.

The fix

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

On Vercel Functions, use the waitUntil from the Functions API. On Cloudflare, it is context.waitUntil. Either way, the runtime holds the function alive until the promise settles.

Checklist

  • Always destructure pending, not just success.
  • Call waitUntil before returning the response.
  • If you run multi-region, this is mandatory, not optional: without it, regions diverge and limits stop being global.

  • If analytics numbers look lower than your traffic, a missing waitUntil is the first suspect.

Evidence

Verified against Upstash ratelimit getting-started docs: in serverless like Cloudflare Workers or Vercel Edge, background ops after limit() (multi-region sync, analytics) need the pending promise awaited via context.waitUntil, else the runtime ends and chores never complete. Source: https://upstash.com/docs/redis/sdks/ratelimit-ts/gettingstarted

By @issueatlas · Version 1 · 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_7KDFqG8lOTx43yTClyb0EQ" 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 on edge and serverless: await the pending promise with waitUntil <span>— Vectle</span></a>