Setup & connection

Configure Redis and connect with a URL or host/port.

Setup & connection

Create a distributed limiter with Redis options. You must call await limiter.ready() before using it so the connection is established.

Using a URL

Handy with env vars (e.g. REDIS_URL=redis://localhost:6379):

import { DistributedRateLimiter } from 'rate-queue';

const limiter = new DistributedRateLimiter({
  id: 'api-limiter',   // same ID on every server = shared limit
  maxConcurrent: 10,
  redis: {
    url: process.env.REDIS_URL || 'redis://localhost:6379',
    keyPrefix: 'myapp:ratelimit',
  },
});

await limiter.ready();

const result = await limiter.schedule(() => fetch('https://api.example.com/data'));

Supported URL forms: redis://host:port, rediss://... (TLS), and with auth: redis://:password@host:6379.

Using host and port

const limiter = new DistributedRateLimiter({
  id: 'api-limiter',
  maxConcurrent: 10,
  redis: {
    host: 'localhost',
    port: 6379,
    password: 'optional',
    keyPrefix: 'myapp:ratelimit',
  },
});
await limiter.ready();

Using an existing Redis client

If you already use ioredis, pass it in so the limiter doesn’t create a second connection:

import Redis from 'ioredis';
import { DistributedRateLimiter } from 'rate-queue';

const redis = new Redis(process.env.REDIS_URL);

const limiter = new DistributedRateLimiter({
  id: 'api-limiter',
  maxConcurrent: 10,
  redis: { client: redis },
});
await limiter.ready();

Next: When Redis is down.