> ## Documentation Index
> Fetch the complete documentation index at: https://express-quota-limiter.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Storage drivers: Redis and in-memory quota backends

> Understand how RedisDriver and MemoryDriver work, when to use each, and how to implement a custom QuotaStorage backend for any data store.

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:

```typescript theme={null}
interface QuotaStorage {
  decrement(key: string, limit?: number, weight?: number): Promise<QuotaResult>;
  increment(key: string, weight: number): Promise<void>;
}
```

`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](https://express-quota-limiter.mintlify.site/guides/custom-driver.md) 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

```typescript ioredis theme={null}
import Redis from "ioredis";
import { RedisDriver } from "express-quota-limiter";

const redis = new Redis();
const driver = new RedisDriver(redis, {
  ttlSeconds: 60 * 60 * 24 * 30, // 30 days (default)
});
```

```typescript node-redis v4 theme={null}
import { createClient } from "redis";
import { RedisDriver } from "express-quota-limiter";

const redis = createClient();
await redis.connect();

const driver = new RedisDriver(redis);
```

### `RedisDriverOptions`

| Option | Type | Default | Description |
| - | - | - | - |
| `ttlSeconds` | `number` | `2592000` (30 days) | TTL applied when a quota key is first created |

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

```typescript theme={null}
import { MemoryDriver } from "express-quota-limiter";

const driver = new MemoryDriver({ ttlSeconds: 3600 }); // 1-hour window
```

### 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`.

```typescript theme={null}
import { createQuotaLimiter, MemoryDriver } from "express-quota-limiter";

const driver = new MemoryDriver();
const middleware = createQuotaLimiter({
  storage: driver,
  limit: 5,
  keyGenerator: (req) => `test:${req.userId}`,
});

afterEach(() => driver.clear());
```

### `MemoryDriverOptions`

| Option | Type | Default | Description |
| - | - | - | - |
| `ttlSeconds` | `number` | `2592000` (30 days) | TTL for each quota window |

***

> [!WARNING]
>
> `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.

> [!TIP]
>
> 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:

```typescript theme={null}
import { QuotaStorage, QuotaResult } from "express-quota-limiter";

class MySQLDriver implements QuotaStorage {
  async decrement(key: string, limit: number = 100, weight: number = 1): Promise<QuotaResult> {
    // your implementation
  }

  async increment(key: string, weight: number = 1): Promise<void> {
    // your implementation
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.