Ratelock

Resilience Policies

withRetry

Retry transient failures with exponential backoff.

The withRetry policy automatically retries failed rate limit checks with exponential backoff, handling transient errors like network timeouts or connection drops.

Usage

import { fixedWindow, withRetry } from '@ratelock/redis'

const limiter = await fixedWindow({ ... })
const retried = withRetry(limiter, {
    maxAttempts: 3,       // total attempts (including first)
    baseDelayMs: 100,     // initial delay (default: 100)
    maxDelayMs: 2000,     // maximum delay cap (default: 2000)
})

Configuration Options

Prop

Type

Delay Calculation

Attempt 1: baseDelayMs × 2^0 = 100ms
Attempt 2: baseDelayMs × 2^1 = 200ms
Attempt 3: baseDelayMs × 2^2 = 400ms
...
Capped at maxDelayMs

Built-in Alternative

For most cases, use the built-in retry option at creation time instead:

import { fixedWindow } from '@ratelock/redis'

const limiter = await fixedWindow({
    url: 'redis://localhost:6379',
    limit: 100,
    windowMs: 60_000,
    retry: { maxAttempts: 3 },
})

See Resilience Policies for when to use standalone vs built-in.

How is this guide?

Last updated on

On this page