> ## 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: RedisDriver and MemoryDriver reference

> Complete reference for RedisDriver and MemoryDriver — constructors, options, and methods for the express-quota-limiter storage backends.

`express-quota-limiter` ships with two built-in storage drivers. `RedisDriver` is designed for production use — it stores quota state in Redis using atomic pipeline operations so there are no race conditions. `MemoryDriver` keeps state in process memory and requires no external dependencies, making it ideal for local development and testing.

## RedisDriver

`RedisDriver` uses a Redis MULTI/EXEC pipeline to atomically initialize a key (only if it does not already exist) and decrement it in a single round-trip. It works with both **ioredis** and **node-redis v4**.

### Constructor

```typescript theme={null}
new RedisDriver(client, options?)
```

**client** `any` *required*

An active Redis client instance. Accepts either an `ioredis` client or a `node-redis` v4 client. The driver detects which client you have passed and uses the correct pipeline API automatically.

**options.ttlSeconds** `number` (default: 2592000)

The TTL in seconds applied when a quota key is first created. The default is `2592000` (30 days), which matches a typical monthly quota window. The TTL is only set on initial key creation — it is not renewed on every request.

### Usage examples

```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
});
```

```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, {
  ttlSeconds: 60 * 60 * 24 * 30, // 30 days
});
```

### Methods

#### `decrement(key, limit?, weight?)`

Atomically initializes the key at `limit` (if missing) and decrements by `weight`. Returns a `QuotaResult` with `{ success, remaining }`.

| Parameter | Type | Default | Description |
| - | - | - | - |
| `key` | `string` | — | The quota key to decrement |
| `limit` | `number` | `100` | The quota ceiling, used only when creating a new key |
| `weight` | `number` | `1` | How much quota this operation consumes |

#### `increment(key, weight)`

Increments the stored value for `key` by `weight`. Use this to refund quota — for example, when a background job fails after the middleware has already decremented.

| Parameter | Type | Default | Description |
| - | - | - | - |
| `key` | `string` | — | The quota key to increment |
| `weight` | `number` | `1` | How much quota to add back |

***

## MemoryDriver

`MemoryDriver` stores quota entries in a `Map` inside the current Node.js process. It has no external dependencies and no setup required, which makes it the recommended driver for unit and integration tests.

> [!WARNING]
>
> `MemoryDriver` is not suitable for multi-process or multi-server deployments. Each process maintains its own isolated store, so quota counts will not be shared across workers or instances.

### Constructor

```typescript theme={null}
new MemoryDriver(options?)
```

**options.ttlSeconds** `number` (default: 2592000)

The TTL in seconds for each quota window. When a key expires or does not yet exist, it is re-initialized at the configured `limit` on the next request. The default is `2592000` (30 days).

### Usage example

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

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

### Methods

#### `decrement(key, limit?, weight?)`

Initializes the key at `limit` (if missing or expired) and decrements by `weight`. Returns a `QuotaResult` with `{ success, remaining }`.

| Parameter | Type | Default | Description |
| - | - | - | - |
| `key` | `string` | — | The quota key to decrement |
| `limit` | `number` | `100` | The quota ceiling, used only when creating a new key |
| `weight` | `number` | `1` | How much quota this operation consumes |

#### `increment(key, weight)`

Increments the stored value for `key` by `weight`. Has no effect if the key does not exist.

| Parameter | Type | Default | Description |
| - | - | - | - |
| `key` | `string` | — | The quota key to increment |
| `weight` | `number` | `1` | How much quota to add back |

#### `clear()`

Removes all entries from the in-memory store. Call this between tests to ensure each test starts with a clean state.

```typescript theme={null}
afterEach(() => driver.clear());
```

#### `inspect(key)`

Returns the raw store entry for the given key, or `undefined` if the key does not exist. The returned object has the shape `{ remaining: number, expiresAt: number }` where `expiresAt` is a Unix millisecond timestamp.

```typescript theme={null}
const entry = driver.inspect("quota:emails:tenant_42");
// { remaining: 482, expiresAt: 1746662400000 }
```

> [!TIP]
>
> Use `inspect` in tests to assert on exact quota state without making an HTTP request to your Express app.

***

## Custom drivers

You can implement your own driver for any storage backend by satisfying the `QuotaStorage` interface:

```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 atomic implementation
  }

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

See the [TypeScript types reference](https://express-quota-limiter.mintlify.site/reference/types.md) for the full `QuotaStorage` interface definition.


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