Web Development Bug Report

A per-IP rate limit locked out a whole customer office behind one NAT address

On a Monday morning, 400 employees at one customer got 429 errors in our app at the same time. They all shared one public IP, and our API limit was 300 requests per minute per IP. Why IP is a poor identity, and the tiered limits keyed by token, account and IP that replaced it.

Owen Gallagher Owen Gallagher @owengallagher · November 24, 2026 · 6 min read · 52 Views
Banner: a per-IP rate limit blocking many users who share one address

At 09:05 on a Monday, our support channel lit up with messages from one enterprise customer: the web app showed "Too many requests, please try again later" for everyone in their head office. Their IT team thought we were under attack. We were not. We were rate-limiting them.

The API gateway's rate limiter logs:

09:03:58 ratelimit key=ip:198.51.100.40 limit=300/min count=301 action=reject
09:03:58 ratelimit key=ip:198.51.100.40 limit=300/min count=302 action=reject
...
09:04:59 ratelimit key=ip:198.51.100.40 rejected_in_window=1844

One IP address, 2,100 requests in a minute. The customer confirmed that their entire office, about 400 people, reached the internet through a single NAT gateway with that public address. On Monday morning, everyone logged in at roughly the same time, and our single-page app makes five to eight API calls on load. Four hundred people times six calls exceeds 300 in a minute easily.

The rule had been added a year earlier after a scraping incident:

rate_limits:
  - name: api-per-ip
    match: "/api/*"
    key: client_ip
    limit: 300
    window: 60s

It was a reasonable defence against one scraper and a bad rule for anyone sharing an address.

An IP address is not a user

Many legitimate users share a public IP:

  • Corporate NAT: an office of hundreds or thousands behind one or a few addresses.
  • Carrier-grade NAT (CGNAT): mobile operators and many home ISPs put large numbers of customers behind shared IPv4 addresses.
  • VPNs and proxies: whole companies egress through a VPN concentrator or a security proxy.
  • Universities and public Wi-Fi.

Meanwhile, a single attacker can rotate through many IPs (cloud instances, residential proxy networks). So IP limits both block groups of honest users and fail to stop distributed abuse. They are still useful as a coarse outer layer, but not as the main identity for limits.

IPv6 makes it different again: a single customer usually has a whole /64 or larger prefix, so per-address limits can be bypassed by one host, and limits should be keyed by prefix instead.

What we were trying to protect against

When we listed the actual threats the original rule was meant to address, they were:

  1. Unauthenticated scraping of public endpoints.
  2. Credential stuffing on the login endpoint.
  3. One authenticated client overwhelming the API (a buggy integration in a loop).

Each wants a different key. Only the first is really about IP.

Environment

Item Value
Gateway Kong-style gateway with a Redis-backed limiter
Rule 300 requests per minute per IP on all /api/* routes
Affected about 400 users behind one NAT IP, 25 minutes
App behaviour 5 to 8 API calls per page load

1. Raise the IP limit to an abuse ceiling, not a usage limit

We kept a per-IP limit for unauthenticated traffic only, and set it high enough that only clearly abusive volumes hit it:

rate_limits:
  - name: anon-per-ip
    match: "/api/public/*"
    when: unauthenticated
    key: client_ip_prefix        # /32 for IPv4, /64 for IPv6
    limit: 3000
    window: 60s

Authenticated requests are no longer limited by IP at all at this layer.

2. Key authenticated limits by token and account

For logged-in users and API clients, the identity is the credential:

  - name: per-token
    match: "/api/*"
    when: authenticated
    key: token_id
    limit: 600
    window: 60s
    algorithm: token_bucket
    burst: 200

  - name: per-account
    match: "/api/*"
    when: authenticated
    key: account_id
    limit_from: plan             # e.g. 6,000/min standard, 30,000/min enterprise
    window: 60s
  • Per token (or per session): one browser or one integration cannot monopolise the API. A token bucket with a burst allows the spike of calls on page load without allowing sustained floods.
  • Per account: the whole customer has a budget sized to their plan. The 400-person office now shares an enterprise allowance that fits their usage, instead of a limit designed for one scraper.

The token bucket itself is a small Lua script in Redis, so the read-modify-write is atomic:

-- KEYS[1] bucket key; ARGV: capacity, refill_per_sec, now_ms, cost
local cap, rate, now, cost = tonumber(ARGV[1]), tonumber(ARGV[2]), tonumber(ARGV[3]), tonumber(ARGV[4])
local b = redis.call('HMGET', KEYS[1], 'tokens', 'ts')
local tokens = tonumber(b[1]) or cap
local ts = tonumber(b[2]) or now
tokens = math.min(cap, tokens + (now - ts) / 1000 * rate)
local allowed = tokens >= cost
if allowed then tokens = tokens - cost end
redis.call('HSET', KEYS[1], 'tokens', tokens, 'ts', now)
redis.call('PEXPIRE', KEYS[1], math.ceil(cap / rate * 1000))
return { allowed and 1 or 0, tokens }

3. Special rules for login

Login is unauthenticated by definition, so it needs its own controls:

  • Per username: 10 failed attempts per hour, then a challenge for that username, regardless of IP.
  • Per IP prefix: a high ceiling (300 failures per 10 minutes) to slow down a single source attacking many usernames.
  • A global anomaly alert on the failure rate.

This combination does not lock out an office on a Monday morning, because successful logins do not count and each person has their own username.

4. Tell clients what is happening

Every rate-limited response now includes the standard headers so client code and humans can react sensibly:

HTTP/1.1 429 Too Many Requests
Retry-After: 12
RateLimit-Policy: "per-token";q=600;w=60
RateLimit: "per-token";r=0;t=12
Content-Type: application/problem+json

{"type": "https://developer.example.com/problems/rate-limited",
 "title": "Too many requests for this token",
 "retry_after_seconds": 12}

Retry-After is defined in RFC 9110. The RateLimit and RateLimit-Policy fields follow the IETF HTTPAPI working group draft. Our web app now backs off and retries after the given time instead of showing an error immediately.

5. Allow customers to register egress IPs

Enterprise customers can register their egress IP ranges. Traffic from those ranges is attributed to their account for anonymous endpoints as well, and excluded from anonymous per-IP limits.

1. A Monday morning simulation. In staging, 500 simulated users with separate sessions, all from one source IP, loaded the app within 60 seconds:

Rule set 429 responses Users who saw an error
Old (300/min per IP) 2,310 431
New (token + account + anon IP ceiling) 0 0

2. Abuse still blocked. A single token looping on one endpoint at 50 requests per second was limited within seconds, with correct Retry-After values. An unauthenticated scraper from one IP was blocked at the anonymous ceiling. A scraper rotating IPs without credentials is now handled by bot detection at the edge, which IP limits had never really stopped anyway.

3. Production. No customer-wide lockouts since the change. The per-token limit triggers a few times a week, almost always for a misbehaving integration, and the account owner gets an email with the details.

4. Security implications. Removing the IP limit for authenticated traffic shifts protection to the credential. That made token theft more important to detect, so we added alerts when one token is used from many different networks within a short time.

Takeaways

  • Many honest users share one IP through NAT, CGNAT and VPNs. Per-IP limits block them together.
  • Key limits by what identifies the client: token or session, then account, with IP only as an abuse ceiling.
  • Use token buckets with burst to allow normal spikes, and size account limits to the plan.
  • Return 429 with Retry-After and rate limit headers so clients can back off gracefully.

References: RFC 6585, 429 Too Many Requests, RFC 9110, Retry-After, IETF draft: RateLimit header fields for HTTP, RFC 6888, Common Requirements for Carrier-Grade NATs.

Add a label

#redis #rate-limiting #token-bucket #api-security #nat
Written by
Owen Gallagher @owengallagher

I am a site reliability engineer on a platform team that runs the edge for dozens of internal services: API gateways, reverse proxies, load balancers and the resilience settings everyone copies from each other. The incidents I learn most from happen when a safety mechanism does the wrong thing. A rate limit that locked out an entire office behind one IP, a circuit breaker that never opened, a liveness probe that restarted every pod during a database blip, two nodes that both decided they were primary. Each post explains what the mechanism was meant to do, why it misfired in our setup, how we changed the configuration and how we drilled the failure afterwards to prove it. I keep the examples close to real configs so you can compare them with your own.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.