@hile/redis-rate-limit
Enforce shared quotas through fixed-window, sliding-window, or token-bucket Redis algorithms.Choose This Package When
Use When
Use these packages for distributed lease locks, duplicate execution protection, shared quotas, and durable Redis Streams job processing.Do Not Use When
- Do not claim exactly-once behavior.
- Do not use locks as a replacement for database constraints.
- Do not use queue
jobIdas the only idempotency boundary for side effects. - Do not use rate limits as authorization or authentication.
Install
Imports
Copy-Paste Example
Idempotent queue worker:More Examples
Distributed lock with fencing:Runtime And Lifecycle Notes
RedisLock.withLock()asserts ownership before returning a successful callback result.tryLock()returnsundefinedif the key is already locked.RedisIdempotency.run()storesIN_FLIGHTandDONEstates in Redis and uses Redis locks for ownership.- Idempotency requires a stable
fingerprint. stableHash()is for plain DTOs and rejects unsupported shapes.- Rate limit algorithms:
fixed-window,sliding-window,token-bucket. dryRunrate limit checks do not mutate Redis.- Queue workers use Redis Streams consumer groups, delayed sorted sets, pending claim recovery, retry backoff, and DLQ streams.
- Queue payload schema can expose
parse()orsafeParse().
Anti-Patterns
- Random UUID per retry as idempotency key.
- Returning
Date,BigInt, or class instances from idempotent functions without aresultCodec. - Using
stream()or RPC retries without idempotency around irreversible handlers. - Ignoring DLQ after max attempts.
Verification Checklist
- Lock TTL is longer than normal critical-section time.
- Fencing is enabled when stale owners can write to external resources.
- Idempotency keys use business identifiers.
- Queue handlers are idempotent or wrapped in idempotency.
- Rate limit
Retry-Afteris exposed in seconds for HTTP. - DLQ is monitored.
Package-Local AI Guide
This package also shipsAI.md in npm so agents can read accurate examples after installation.