Repository navigation
[TT-18135] Rate limits using the Redis rate limiter allow extra requests when Gateways’ system clocks differ - #8823
Conversation
🎯 Recommended Merge TargetsBased on JIRA ticket TT-18135: Rate limits using the Redis rate limiter allow extra requests when Gateways’ system clocks differ Fix Version: Tyk 5.13.9Required:
Fix Version: Tyk 5.17.0
Required:
📋 Workflow
|
|
This PR addresses a bug in the Redis-based sliding log rate limiter where rate limits could be exceeded if different Gateway instances had unsynchronized system clocks. The fix ensures consistent rate limit enforcement across a distributed environment by using the Redis server's clock as the single source of truth. Key changes include:
Files Changed Analysis
Architecture & Impact Assessment
Architectural Flow ChangesequenceDiagram
participant G1 as "Gateway 1 (Clock t1)"
participant G2 as "Gateway 2 (Clock t2)"
participant R as Redis
rect rgb(240, 240, 255)
note over G1, R: Old Flow (Inconsistent Time)
G1->>G1: Get local time (t1)
G1->>R: Prune/Count using t1
G2->>G2: Get local time (t2)
G2->>R: Prune/Count using t2
note right of R: Window is inconsistent if t1 ≠ t2
end
rect rgb(240, 240, 255)
note over G1, R: New Flow (Consistent Time)
G1->>R: Invoke sliding_log_add.lua
activate R
R->>R: Get Redis time (t_redis)
R-->>G1: Return result based on t_redis
deactivate R
G2->>R: Invoke sliding_log_add.lua
activate R
R->>R: Get Redis time (t_redis)
R-->>G2: Return result based on t_redis
deactivate R
note right of R: Both gateways use the same time source
end
Scope Discovery & Context Expansion
Metadata
Powered by Visor from Probelabs Last updated: 2026-11-26T15:26:00.975Z | Triggered by: pr_updated | Commit: 889ac45 💡 TIP: You can chat with Visor using |
Security Issues (1)
✅ Performance Check PassedNo performance issues found – changes LGTM. Quality Issues (1)
Powered by Visor from Probelabs Last updated: 2026-11-26T15:25:26.326Z | Triggered by: pr_updated | Commit: 889ac45 💡 TIP: You can chat with Visor using |
|
API Changes --- prev.txt 2026-11-26 15:24:25.893887443 +0000
+++ current.txt 2026-11-26 15:24:17.143848375 +0000
@@ -8936,7 +8936,8 @@
// Redis Rate Limiter, or the Sentinel Rate Limiter, as both algorithms implement a sliding log.
EnableRateLimitSmoothing bool `json:"enable_rate_limit_smoothing"`
- // An enhancement for the Redis and Sentinel rate limiters, that offers a significant improvement in performance by not using transactions on Redis rate-limit buckets.
+ // Deprecated: ignored since v5.13.0. The Redis and Sentinel rate limiters run a single Lua script,
+ // so there is no transaction left to skip. The option is kept so existing configurations still load.
EnableNonTransactionalRateLimiter bool `json:"enable_non_transactional_rate_limiter"`
// How frequently a distributed rate limiter synchronises information between the Gateway nodes. Default: 2 seconds. |
This comment has been minimized.
This comment has been minimized.
c80cd8c to
c7711d3
Compare
📚 Documentation previewThis PR changes generated documentation (component: cc @TykTechnologies/pms — advisory review requested; this does not block merging. Rendered docs diffdiff -ruN out-base/gateway-config.mdx out-head/gateway-config.mdx
--- out-base/gateway-config.mdx 2026-11-26 15:23:40.573877593 +0000
+++ out-head/gateway-config.mdx 2026-11-26 15:23:42.113646379 +0000
@@ -989,7 +989,8 @@
ENV: <b>TYK_GW_ENABLENONTRANSACTIONALRATELIMITER</b><br />
Type: `bool`<br />
-An enhancement for the Redis and Sentinel rate limiters, that offers a significant improvement in performance by not using transactions on Redis rate-limit buckets.
+Deprecated: ignored since v5.13.0. The Redis and Sentinel rate limiters run a single Lua script,
+so there is no transaction left to skip. The option is kept so existing configurations still load.
### drl_notification_frequency
ENV: <b>TYK_GW_DRLNOTIFICATIONFREQUENCY</b><br />
|
NewSlidingLogRedis runs per request and allocated a logrus.Logger each time only to have WithLogger replace it. Use one package-level instance. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
c7711d3 to
0cccd27
Compare
…limiter-allow-extra-requests-when-gateways-system-clocks-differ
…limiter-allow-extra-requests-when-gateways-system-clocks-differ
|
SentinelOne CNS Hardcoded Secret Detector SentinelOne CNS is a cloud-agnostic, agentless CSPM & CWPP solution that continuously detects and prevents vulnerabilities that have the highest probability of being exploited in Azure, AWS, Google Cloud, and Kubernetes. |
|



Description
The Redis sliding log rate limiter used each Gateway's own
time.Now()to prune and count entries, so a Gateway with a fast clock dropped entries written by slower ones. The Lua script now takes its clock fromredis.call("TIME"), so every Gateway sharing a key counts the same window.sliding_log_add.luatakes onlymax_allowedandper; it returns the Redis time soResetis computed against it.ZADD NXand aZCOUNTjump. This cannot happen in normal operation (scripts run one at a time and take longer than a microsecond); it covers a stepped-back Redis clock and mixed-version clusters. Collisions are logged at debug, running out of suffixes is an explicit error that blocks the request.SlidingLogkeeps onlyDo; the pipeline methods existed for tests only.enable_non_transactional_rate_limiterhas had no effect since v5.13.0 (TT-6075 movedDoto the script) and is now marked deprecated.Related Issue
https://tyktech.atlassian.net/browse/TT-18135
Motivation and Context
With skewed clocks a client could exceed its limit across Gateways,
X-RateLimit-Remainingwent back up andX-RateLimit-Resetmoved backwards.How This Has Been Tested
internal/ratetests moved to a testify suite: success flow, two nodes sharing a key with entries from earlier in the window, unique members, script error path, collision jump. Benchmarks for the happy path and for 0/1/10/100/998 collisions (collision branch costs ~3 µs over the Redis round trip).Not covered:
X-RateLimit-Resetinheaders.gostill builds the epoch from the serving Gateway's clock. Docsrate-limit.mdstill recommendsenable_non_transactional_rate_limiter; separate docs PR.Types of changes
Checklist
🤖 Generated with Claude Code
Ticket Details
TT-18135
Generated at: 2026-11-26 15:23:20