bug: limit-count Redis Counter Decrementing
Current Behavior
The limit-count plugin with Redis policy shows the counter decrementing instead of incrementing. While the rate limiting functionality works (returns 429 when limit exceeded), the counter value in Redis is incorrect.
Expected Behavior
Expected Behavior Counter should increment: 1, 2, 3, 4, 5, then return 429
Actual Behavior Counter decrements: 5, 4, 3, 2, 1, 0, -1 (returns 429 but counter is wrong)
Error Logs
Test Output Request 1: HTTP 200 | Counter: 4 Request 2: HTTP 200 | Counter: 3 Request 3: HTTP 200 | Counter: 2 Request 4: HTTP 200 | Counter: 1 Request 5: HTTP 200 | Counter: 0 Request 6: HTTP 429 | Counter: -1
Additional Notes
- The rate limiting functionality works (HTTP 429 is returned correctly when limit is exceeded)
- Only the counter visualization in Redis is incorrect
- This appears to be a display issue - the internal logic may be working correctly
Steps to Reproduce
- Configure limit-count plugin with Redis policy: "limit-count": { "count": 5, "time_window": 60, "key": "consumer_name", "key_type": "var", "policy": "redis", "redis_host": "redis", "redis_port": 6379, "redis_database": 0, "rejected_code": 429 }
- Configure key-auth plugin for authentication
- Make authenticated requests to the route
- Check Redis counter: GET plugin-limit-countroute1:{id}:alice
Environment
Environment
- APISIX version: 3.9.0-debian (Docker image: apache/apisix:3.9.0-debian)
- OS: Debian
- Redis version: 7-alpine
- Deployment: Docker Compose
I investigated the implementation in limit-count-redis.lua.
The Redis script intentionally stores the remaining quota instead of the consumed request count.
For example, with a limit of 5:
Request 1 → Redis value = 4 Request 2 → Redis value = 3 Request 3 → Redis value = 2 Request 4 → Redis value = 1 Request 5 → Redis value = 0
This behavior comes from the Lua script:
redis.call('set', KEYS[1], ARGV[1] - ARGV[3], 'EX', ARGV[2])
redis.call('incrby', KEYS[1], 0 - ARGV[3])
So Redis is tracking remaining quota, not consumed requests.
Rate limiting itself appears to work correctly.
Could you confirm whether this behavior is intentional, or if Redis should instead track the consumed request count?
The limit-count plugin stores remaining quota in Redis, not consumed count. See the Redis Lua script in apisix/plugins/limit-count/limit-count-redis.lua (lines 32-39): on new window it sets key = limit - cost, on subsequent requests it decrements via INCRBY key (0 - cost). The X-RateLimit-Remaining header directly exposes this value. This is intentional design, documented in our docs as 'the remaining quota'. Closing as this is working as expected.