Skip to content

perf(limit-count): use evalsha with NOSCRIPT fallback for Redis script execution#13363

Merged
nic-6443 merged 2 commits into
apache:masterfrom
nic-6443:feat/limit-count-evalsha
May 14, 2026
Merged

perf(limit-count): use evalsha with NOSCRIPT fallback for Redis script execution#13363
nic-6443 merged 2 commits into
apache:masterfrom
nic-6443:feat/limit-count-evalsha

Conversation

@nic-6443

Copy link
Copy Markdown
Member

The limit-count plugin ships the full Lua script text on every Redis call via red:eval(). At high RPS this wastes both network bandwidth (~200-500 bytes per call) and Redis CPU (script parsing on every call).

This PR replaces eval with evalsha + NOSCRIPT fallback in both the standalone Redis and Redis Cluster backends of the limit-count plugin. The script SHA1 is computed client-side at module load time, avoiding the need for SCRIPT LOAD (which doesn't route correctly in Redis Cluster). On NOSCRIPT (after Redis restart or hitting a new cluster shard), the code falls back to eval which implicitly caches the script for subsequent calls.

This mirrors the pattern already used by the limit-conn plugin, but improves on it by supporting Redis Cluster mode (limit-conn currently disables evalsha for cluster).

Changes:

  • apisix/plugins/limit-count/limit-count-redis.lua: evalsha + NOSCRIPT fallback
  • apisix/plugins/limit-count/limit-count-redis-cluster.lua: evalsha + NOSCRIPT fallback
  • t/plugin/limit-count-redis.t: add test for NOSCRIPT fallback recovery

Copilot AI review requested due to automatic review settings May 12, 2026 09:16
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. performance generate flamegraph for the current PR labels May 12, 2026
@nic-6443
nic-6443 force-pushed the feat/limit-count-evalsha branch 2 times, most recently from a0d683d to 542f505 Compare May 12, 2026 09:37

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Optimizes the limit-count plugin’s Redis Lua execution by switching from EVAL to EVALSHA with a NOSCRIPT fallback, reducing per-request bandwidth and Redis script parsing overhead.

Changes:

  • Use evalsha with NOSCRIPT fallback in the standalone Redis backend.
  • Use evalsha with NOSCRIPT fallback in the Redis Cluster backend.
  • Add a test that flushes Redis script cache and verifies fallback recovery.

Reviewed changes

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

File Description
apisix/plugins/limit-count/limit-count-redis.lua Introduces script SHA computation and evalsha + NOSCRIPT fallback for standalone Redis.
apisix/plugins/limit-count/limit-count-redis-cluster.lua Introduces script SHA computation and evalsha + NOSCRIPT fallback for Redis Cluster.
t/plugin/limit-count-redis.t Adds coverage for NOSCRIPT fallback after flushing Redis script cache.

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

Comment thread t/plugin/limit-count-redis.t
Comment thread t/plugin/limit-count-redis.t
Comment thread apisix/plugins/limit-count/limit-count-redis.lua
…t execution

Replace plain red:eval() with red:evalsha() + NOSCRIPT fallback in
limit-count Redis and Redis Cluster backends. This reduces per-request
network payload from ~200-500 bytes (full script text) to ~40 bytes
(SHA1 digest + arguments) and eliminates Redis-side script parsing
after the first call.

The script SHA1 is computed client-side at module load time using
ngx.sha1_bin(), which works correctly for both standalone Redis and
Redis Cluster topologies. On NOSCRIPT (after Redis restart or when
hitting a new cluster shard), the fallback to eval() implicitly
caches the script for subsequent evalsha calls.
@nic-6443
nic-6443 force-pushed the feat/limit-count-evalsha branch from 0514f7b to faea574 Compare May 12, 2026 10:28
@nic-6443
nic-6443 merged commit 06c15d3 into apache:master May 14, 2026
19 checks passed
@nic-6443
nic-6443 deleted the feat/limit-count-evalsha branch May 14, 2026 08:09
wistefan pushed a commit to wistefan/apisix that referenced this pull request Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance generate flamegraph for the current PR size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants