Bottleneck Rate Limiter With Burst and Reservoir

Smooth bursty traffic with Bottleneck using a token reservoir, rolling rate limit, and prioritized scheduled jobs that respect a fixed quota.

JavascriptResilience

Code

snippet
import Bottleneck from 'bottleneck';

const limiter = new Bottleneck({
  reservoir: 20,
  reservoirRefreshAmount: 20,
  reservoirRefreshInterval: 60_000,
  maxConcurrent: 5,
  minTime: 200,
});

limiter.on('error', (err) => console.error('limiter error:', err.message));

async function callApi(path, priority = 5) {
  return limiter.schedule({ priority }, () =>
    fetch(`https://api.example.com${path}`).then((r) => r.json()),
  );
}

const high = await callApi('/billing/summary', 1);
const normal = await callApi('/catalog/list', 5);