DNS rate limits count queries across every instance that shares the ValKey server #73

Open
opened 2026-10-31 12:42:01 -04:00 by claude · 0 comments
Owner
  • Draft: INT-5
  • Priority: medium (a client can spread over N nodes and get N times the limit today)
  • Agent: coder
  • Depends on: #70
  • Decisions: Q-INT-3

Why

The token buckets and subnet windows are in-process dictionaries (src/dotnet/src/Ninlil.Dns.Server.RateLimiting/TokenBucketDnsRateLimiter.cs:66,73).

What changes

Technical design: a hybrid, so the query path never waits on the network.

  • The local token bucket and window stay authoritative for each query.
  • A background loop runs every 250 ms (a constant; an option only if a test shows it is needed). For each active client IP and subnet it adds the local spend since the last flush to INCRBY <prefix>:rl:ip:<addr>:<window> with PEXPIRE, and reads back the cluster total.
  • A client or subnet whose cluster total for the current second-window exceeds QueriesPerSecond × window (or the subnet limits) is marked "over" locally until the window ends. TryAcquireAsync and CheckSubnetLimitAsync then deny it.
  • If ValKey is down, the flag is cleared and limits are local again (fail open, matching MaxTrackedClients, TokenBucketDnsRateLimiter.cs:184-191).
  • The access-rule bypass still runs first.
  • The admin sign-in limiter is #74, not this issue.
  • Files:
    • TokenBucketDnsRateLimiter.cs
    • new ValkeyRateLimitSynchroniser in Ninlil.Dns.Server.RateLimiting (the project references Ninlil.Common.Valkey)
    • src/dotnet/src/Ninlil.Dns.Server.RateLimiting/Extension/ServiceCollectionRateLimitingExtension.cs
    • the summary of IDnsRateLimiter.cs

Acceptance

  • Unit test (fails first): with a fake shared counter reporting a cluster total over the limit, the next TryAcquireAsync for that IP returns false although the local bucket has tokens.
  • Unit test: with the shared store throwing, decisions equal today's.
  • Container test (RequiresDocker): two limiters on one ValKey, each at 60 % of the limit, together get denials.
  • Benchmark Pipeline/ latency is unchanged within noise. Copy the result into the issue.
  • src/dotnet/tests/Ninlil.Dns.Server.RateLimiting.Tests passes.
  • ENVIRONMENT.md updated if an option is added.

Source

  • Owner scope (2026-10-09).
  • Owner decision 2026-10-31 (Q-INT-3): "DNS queries AND admin login limiter"; the DNS half is this issue.
- **Draft:** INT-5 - **Priority:** medium (a client can spread over N nodes and get N times the limit today) - **Agent:** coder - **Depends on:** #70 - **Decisions:** Q-INT-3 ### Why The token buckets and subnet windows are in-process dictionaries (`src/dotnet/src/Ninlil.Dns.Server.RateLimiting/TokenBucketDnsRateLimiter.cs:66,73`). ### What changes Technical design: a hybrid, so the query path never waits on the network. - The local token bucket and window stay authoritative for each query. - A background loop runs every 250 ms (a constant; an option only if a test shows it is needed). For each active client IP and subnet it adds the local spend since the last flush to `INCRBY <prefix>:rl:ip:<addr>:<window>` with `PEXPIRE`, and reads back the cluster total. - A client or subnet whose cluster total for the current second-window exceeds `QueriesPerSecond × window` (or the subnet limits) is marked "over" locally until the window ends. `TryAcquireAsync` and `CheckSubnetLimitAsync` then deny it. - If ValKey is down, the flag is cleared and limits are local again (fail open, matching `MaxTrackedClients`, `TokenBucketDnsRateLimiter.cs:184-191`). - The access-rule bypass still runs first. - The admin sign-in limiter is #74, not this issue. - Files: - `TokenBucketDnsRateLimiter.cs` - new `ValkeyRateLimitSynchroniser` in `Ninlil.Dns.Server.RateLimiting` (the project references `Ninlil.Common.Valkey`) - `src/dotnet/src/Ninlil.Dns.Server.RateLimiting/Extension/ServiceCollectionRateLimitingExtension.cs` - the summary of `IDnsRateLimiter.cs` ### Acceptance - [ ] Unit test (fails first): with a fake shared counter reporting a cluster total over the limit, the next `TryAcquireAsync` for that IP returns false although the local bucket has tokens. - [ ] Unit test: with the shared store throwing, decisions equal today's. - [ ] Container test (RequiresDocker): two limiters on one ValKey, each at 60 % of the limit, together get denials. - [ ] Benchmark `Pipeline/` latency is unchanged within noise. Copy the result into the issue. - [ ] `src/dotnet/tests/Ninlil.Dns.Server.RateLimiting.Tests` passes. - [ ] `ENVIRONMENT.md` updated if an option is added. ### Source - Owner scope (2026-10-09). - Owner decision 2026-10-31 (Q-INT-3): "DNS queries AND admin login limiter"; the DNS half is this issue.
claude added this to the Release: ValKey milestone 2026-10-31 12:42:01 -04:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
lantea-one/ninlil#73
No description provided.