Skip to main content
A storage driver is the component responsible for persisting and decrementing quota counters. Every createQuotaLimiter call requires a storage option that satisfies the QuotaStorage interface. The library ships with two built-in drivers—RedisDriver for production and MemoryDriver for development and tests—and the interface is simple enough to implement for any backend.

The QuotaStorage interface

Both built-in drivers implement this interface:
decrement is called on every incoming request. It receives the quota key, the resolved limit, and the request weight, and returns { success, remaining }. increment is used for manual quota adjustments — see the custom driver guide for details.

RedisDriver

RedisDriver is the recommended driver for production. It works with both ioredis and node-redis v4 — you inject whichever client you already use.

Atomic operations

The driver uses a Redis MULTI/EXEC pipeline to perform two operations atomically on every request:
  1. SET key limit NX EX ttl — initialises the key to the limit value only if it does not already exist.
  2. DECRBY key weight — subtracts the request weight from the counter.
Running these in a pipeline eliminates race conditions without requiring a Lua script.

Instantiation

RedisDriverOptions

The TTL is set only on key creation. If the key already exists, its expiry is not extended.

MemoryDriver

MemoryDriver stores quota counters in a Map inside the running process. It has no external dependencies and needs no infrastructure to run.

Instantiation

Test helpers

MemoryDriver exposes two additional methods useful in tests:
  • driver.clear() — wipes all quota data. Call this in afterEach to reset state between tests.
  • driver.inspect(key) — returns the raw { remaining, expiresAt } entry for a key, or undefined.

MemoryDriverOptions


MemoryDriver is not suitable for multi-process or multi-server deployments. Each process maintains its own isolated store, so quota counters will not be shared across instances. Use RedisDriver whenever your application runs on more than one process or server.
Use MemoryDriver in your test suite and RedisDriver in production. This pattern requires no infrastructure in CI, and the QuotaStorage interface is identical between both, so your application code does not need to change.

Custom driver

Implement QuotaStorage to use any backend — SQL, DynamoDB, Firestore, or anything else: