BACK
Use casesRate-limited queue — don't choke the backend
Use cases · 60

Rate-limited queue — don't choke the backend

Pattern

An external API has a rate limit (5 requests per second, say). A bulk operation (importing 1000 rows) must respect it — sending sequentially, with a fixed pause.

The problem it solves

Without rate control, bulk actions knock the backend over or get 429 Too Many Requests. An imperative for/await + sleep mixes rate control, error handling, and business logic into one complex function.

Operators and why they matter

  • concatMap — guarantees only one active request. The next starts after the previous completes.
  • delay(N) inside the inner stream — the pause between requests.
  • map — build the processing result.

Gotchas

  • mergeMap — breaks the limit with parallelism. The backend gets N requests at once.
  • switchMap — loses the earlier items. Definitely not what we want.
  • For production 429s — read the Retry-After header instead of a blind constant delay. The server knows how long to wait.

What you get

The backend gets requests at a steady pace and order. No 429s and no DDoS-ing your own services.

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...