API Reference

Bottleneck

Rate limiter and task scheduler that caps concurrent jobs, spaces them with minTime, and supports Redis clustering.

Official Documentation ↗
CompatibilityThis documentation covers version v2. Check the official docs for the latest API surface.

Installation

npmnpm install bottleneck
pnpmpnpm add bottleneck

Methods

Rate limiter and task scheduler that caps concurrent jobs, spaces them with minTime, and supports Redis clustering.

Overview

Bottleneck schedules jobs under rate and concurrency rules and waits out the delays for you. Beyond simple caps it provides token buckets with reservoir refresh, priorities, weights for jobs that should consume multiple slots, and expiration to skip stale queued work.

In cluster mode, limiters coordinate through Redis or IORedis so a fleet of processes shares one real quota. Events such as idle and depleted make it a monitoring point, and counts expose queue length for backpressure decisions upstream.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • new Bottleneck — Creates a limiter; maxConcurrent caps parallel jobs, minTime enforces spacing, and reservoir sets a refillable token count.
  • schedule — Queues a promise-returning job under the limiter rules and resolves with the job result in submission order.
  • submit — Callback-style counterpart of schedule for legacy APIs whose functions signal completion via a callback.
  • reservoir / reservoirRefresh — Token-bucket mode: a fixed burst of jobs refilled every interval, matching APIs that allow N calls per minute.
  • events — Emits lifecycle signals: error on internal failures, empty when the queue drains, and idle when nothing is queued or running.

Usage Notes

  • Model the actual API contract: hourly quotas map to reservoir refresh, and per-second limits map to minTime.
  • Give interactive work a higher priority and batch jobs a lower one so nightly imports never block user-facing calls.
  • In cluster mode, reuse one Redis connection pool across limiters and watch the error event to detect coordination loss.

Common Pitfalls

  • highWater with rejectDrop strategy silently drops overflowing jobs; defaults are safe but understand both before enabling either.
  • A limiter per request defeats itself; create limiters per upstream and share them at module scope.
  • Priorities only reorder the waiting queue; they cannot preempt jobs already running.

FAQ

When do I need the Redis cluster mode?
A single-process limiter only bounds that process. When several instances hit the same API quota, create limiters that share a connection with io and client options pointing at the same Redis so counts and reservoirs are coordinated cluster-wide.
How do maxConcurrent and minTime combine?
Both apply simultaneously: at most maxConcurrent jobs run, and job starts are spaced at least minTime milliseconds apart. A burst of 10 with maxConcurrent 5 and minTime 200 finishes as two parallel lanes starting every 200ms.
What happens to jobs when the limiter throws?
Jobs whose underlying function rejects propagate that rejection to the schedule promise; the limiter itself stays healthy. Listen to the error event for internal problems such as Redis connectivity in cluster mode.