Skip to content

feat: ai rate limiting redis support#12751

Open
beardnick wants to merge 2 commits intoapache:masterfrom
beardnick:feature/ai-rate-limiting-redis-support
Open

feat: ai rate limiting redis support#12751
beardnick wants to merge 2 commits intoapache:masterfrom
beardnick:feature/ai-rate-limiting-redis-support

Conversation

@beardnick
Copy link
Copy Markdown
Contributor

@beardnick beardnick commented Nov 16, 2025

Description

Which issue(s) this PR fixes:

Fixes #12482

Notice
  • I have updated the limit-count-redis.lua and limit-count-redis-cluster.lua files to ensure they now support rate-limiting during the log phase.
  • I referred to the limit-conn-redis.lua file as a guide while implementing rate-limiting in the log phase.
  • To ensure a clean testing environment, I added the require("lib.test_redis").flush_all() function to every Redis rate-limiting test case. Without this addition, the tests could fail unpredictably.
  • I have adjusted the expected results in limit-req-redis.t and limit-req-redis-cluster.t test cases. After thorough verification, the correct behavior is [200, 403, 403, 403]. Previously, the results appeared as [403, 403, 403, 403] due to Redis not being properly cleaned beforehand.

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. enhancement New feature or request labels Nov 16, 2025
@beardnick beardnick changed the title Feature/ai rate limiting redis support feat: ai rate limiting redis support Nov 16, 2025
@beardnick
Copy link
Copy Markdown
Contributor Author

@Baoyuantop PTAL

@membphis
Copy link
Copy Markdown
Member

@beardnick many thx for your contribution, some CI tests are failed, you can fix them

Copy link
Copy Markdown
Member

@nic-6443 nic-6443 left a comment

Choose a reason for hiding this comment

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

Look like the changes in this PR should be limited to the ai-rate-limiting plugin and minor tweaks to the limit-count plugin. The current PR modifies too much code and test cases for other rate limiting plugins, expanding its scope. We should minimize unnecessary code changes.

@Baoyuantop
Copy link
Copy Markdown
Contributor

Hi @beardnick, please check these comments.

@Baoyuantop Baoyuantop added the wait for update wait for the author's response in this issue/PR label Dec 10, 2025
@Baoyuantop
Copy link
Copy Markdown
Contributor

Hi @beardnick, following up on the previous review comments. Please let us know if you have any updates. Thank you.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the ai-rate-limiting plugin to support Redis / Redis Cluster backed counters (to share token quotas across APISIX replicas and persist across restarts), refactors the limit-count Redis backends to support log-phase token commits via timers, and updates docs/tests accordingly.

Changes:

  • Add policy + Redis/Redis Cluster configuration to ai-rate-limiting and pass those settings through to limit-count.
  • Refactor limit-count Redis implementations (single + cluster) and introduce a shared Lua-script utility.
  • Add Redis / Redis Cluster coverage to ai-rate-limiting tests and document the new configuration options in EN/ZH docs.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
t/plugin/ai-rate-limiting.t Adds test cases for ai-rate-limiting with policy=redis and policy=redis-cluster.
docs/en/latest/plugins/ai-rate-limiting.md Documents Redis/Redis Cluster-backed quota sharing and new config fields.
docs/zh/latest/plugins/ai-rate-limiting.md Same as EN docs, in Chinese.
apisix/plugins/limit-count/util.lua New shared Redis EVAL script helpers for limit-count Redis backends.
apisix/plugins/limit-count/limit-count-redis.lua Adds log-phase support via timers and uses the shared util script.
apisix/plugins/limit-count/limit-count-redis-cluster.lua Same as single-Redis backend, for Redis Cluster.
apisix/plugins/ai-rate-limiting.lua Adds schema fields for Redis policies and forwards Redis configuration into limit-count.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@moonming moonming left a comment

Choose a reason for hiding this comment

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

Hi @beardnick, thank you for the AI rate limiting Redis support! This has received 26 reviews, showing significant community interest.

Since this has extensive review history:

  1. Could you rebase on the latest master?
  2. Please confirm all 26 review comments have been addressed
  3. Provide a summary of the key design decisions made during the review process

The feature itself is important — token-based rate limiting for AI workloads needs a distributed backend like Redis. Looking forward to getting this across the finish line!

@Baoyuantop
Copy link
Copy Markdown
Contributor

Hi @beardnick, for all the review comments above, please reply or mark them as resolved if you have already addressed them.

@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Mar 25, 2026
@Baoyuantop
Copy link
Copy Markdown
Contributor

Hi @beardnick, I re-run CI, but some tests are still failing. Please fix it.

@beardnick
Copy link
Copy Markdown
Contributor Author

Hi @beardnick, I re-run CI, but some tests are still failing. Please fix it.

The failed tests don't seem related to my PR.

@beardnick beardnick force-pushed the feature/ai-rate-limiting-redis-support branch from 5e0096b to 0a59598 Compare March 29, 2026 08:47
@beardnick
Copy link
Copy Markdown
Contributor Author

Hi @beardnick, thank you for the AI rate limiting Redis support! This has received 26 reviews, showing significant community interest.

Since this has extensive review history:

  1. Could you rebase on the latest master?
  2. Please confirm all 26 review comments have been addressed
  3. Provide a summary of the key design decisions made during the review process

The feature itself is important — token-based rate limiting for AI workloads needs a distributed backend like Redis. Looking forward to getting this across the finish line!

  1. I squashed my commits and rebased on master.
  2. They are all addressed.
  3. This PR updates the limit-count-redis plugin to support recording limits during the log phase. However, it will not add response headers as it does in the access phase.

SunnyWenwen pushed a commit to SunnyWenwen/wendy_test that referenced this pull request Mar 31, 2026
RFC-001 proposes a new ai-rate-limiting-redis plugin that clones
ai-rate-limiting and replaces the local memory counter with Redis
single-node backend to resolve the multi-replica rate limiting issue.

Includes:
- Full schema definition (inherited + 10 new Redis properties)
- Redis Lua Script atomic counter design
- Access Phase / Log Phase dual-phase implementation design
- Complete ai-proxy-multi integration analysis and sequence diagram
- 4 key integration caveats (instance name consistency, fallback counter
  behavior, placeholder restore on reject, header behavior on fallback)
- Behavioral comparison table vs original plugin
- Fail-open design rationale for Redis unavailability
- Test plan covering multi-node, fallback, and Redis failure scenarios

Ref: apache/apisix#12482, apache/apisix#12751

https://claude.ai/code/session_01Nw29Vgt1yXKJkGFLJnUpSN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:XXL This PR changes 1000+ lines, ignoring generated files. user responded wait for update wait for the author's response in this issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

help request: How to config ai-rate-limiting plugin's counter to redis, as limit-count plugin

7 participants