Ratelock

Getting Started

Installation

Install RateLock packages for your project.

RateLock is distributed as multiple npm packages. Install only the storage engine you need - each one includes all 4 rate limiting strategies.

Choose an Engine

In-Memory (zero dependencies)

npm install @ratelock/local

Redis

npm install @ratelock/redis redis

Or with ioredis:

npm install @ratelock/redis ioredis

With Bun's native client

On Bun >= 1.4, no extra dependency is needed — the driver is auto-detected:

import { RedisClient } from 'bun'
import { fixedWindow } from '@ratelock/redis'

const limiter = await fixedWindow({
    client: new RedisClient('redis://localhost:6379'),
    limit: 100,
    windowMs: 60_000,
})

Or by URL with driver: 'bun'.

PostgreSQL

npm install @ratelock/postgres postgres

Or with pg:

npm install @ratelock/postgres pg

Each engine includes all 4 strategies and built-in resilience policies.

Using Multiple Engines

You can use different engines for different rate limiters in the same app:

import { fixedWindow as localFixedWindow } from '@ratelock/local'
import { fixedWindow as redisFixedWindow } from '@ratelock/redis'

// Local engine for lightweight, in-memory checks
const localLimiter = await localFixedWindow({ limit: 100, windowMs: 60_000 })

// Redis engine for distributed, highly-available checks
const redisLimiter = await redisFixedWindow({
    url: 'redis://localhost:6379',
    limit: 1000,
    windowMs: 60_000,
})

Requirements

  • Node.js 20+ or Bun 1.1+ (all packages)
  • TypeScript 5+ (recommended, not required)
  • Redis 6+ (for @ratelock/redis)
  • PostgreSQL 14+ (for @ratelock/postgres)

How is this guide?

Last updated on

On this page