examples: add fixed-window rate-limiter using INCR and EXPIRE by codeXsidd · Pull Request #3453 · redis/node-redis · GitHub
Skip to content

examples: add fixed-window rate-limiter using INCR and EXPIRE - #3453

Merged
nkaradzhov merged 2 commits into
redis:masterfrom
codeXsidd:examples/rate-limiter
Sep 18, 2026
Merged

nkaradzhov merged 2 commits into
redis:masterfrom
codeXsidd:examples/rate-limiter

Conversation

@codeXsidd

@codeXsidd codeXsidd commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds a practical example recipe demonstrating a fixed-window rate limiter using Redis INCR and EXPIRE.

  • Uses an atomic client.multi() transaction (INCR + EXPIRE) to prevent the key leak race condition if a process crashes between commands.
  • Uses client.close() for proper client lifecycle management.
  • Documents the pattern and references the official Redis INCR Rate Limiter documentation.
  • Updates examples/README.md index table with the new recipe.

Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

Copilot AI lite review requested due to automatic review settings September 16, 2026 17:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4090d3595f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread examples/rate-limiter.js Outdated
Comment thread examples/rate-limiter.js Outdated

@nkaradzhov nkaradzhov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution — a rate-limiter recipe is a great fit for examples.

One correctness issue before we can merge:

The INCR / conditional EXPIRE pair has a leak race. If the process crashes between the INCR and the EXPIRE, the key is created with no TTL. Because the key name embeds the window stamp, it will never be written again — so it stays in Redis forever. This is the exact race the Redis docs warn about (https://redis.io/docs/latest/commands/incr/#pattern-rate-limiter-2) in this pattern.

Since your key is already scoped to the window, the fix is to drop the if (currentCount === 1) check and always send both commands atomically:

const [currentCount] = await client.multi()
  .incr(rateLimitKey)
  .expire(rateLimitKey, WINDOW_SIZE_IN_SECONDS)
  .exec();

Re-running EXPIRE on every request is harmless here (the window stamp in the key does the real reset), and this also saves a round trip. Alternatively, on Redis ≥ 7 you can keep the two calls and use client.expire(rateLimitKey, WINDOW_SIZE_IN_SECONDS, 'NX').

Two small things:

  • client.quit() is deprecated in v5 — use client.close() (or client.destroy(), as the other examples do).
  • Could you add a link to the rate-limiter pattern on the INCR page (https://redis.io/docs/latest/commands/incr/#pattern-rate-limiter) in the header comment? We'd like the examples to show the idiomatic node-redis code and point to redis.io for the algorithm discussion.

Also, please fill in the PR description. Thanks again!

@codeXsidd

Copy link
Copy Markdown
Contributor Author

@codeXsidd
codeXsidd requested review from nkaradzhov and a lite review from Copilot September 17, 2026 14:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codeXsidd
codeXsidd requested a lite review from Copilot September 17, 2026 14:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@nkaradzhov

Copy link
Copy Markdown
Collaborator

@nkaradzhov nkaradzhov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nkaradzhov
nkaradzhov merged commit 28be644 into redis:master Sep 18, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants