Implementing Request Throttling

1. Configuring Concurrent Request Limits

SettingPurpose
max_concurrentIn-flight cap per key
queue_sizePending allowed
queue_timeoutReject if waiting too long
overload_action429 or shed

2. Setting Up Queue-Based Throttling

Example: Bounded queue with timeout

Semaphore inflight = new Semaphore(100);
boolean acquired = inflight.tryAcquire(50, TimeUnit.MILLISECONDS);
if (!acquired) {
  response.status(429).header("Retry-After", "1").end();
  return;
}
try { proxy(request, response); } finally { inflight.release(); }

3. Implementing Priority-Based Throttling

PriorityDrop Order
Critical (paid)Last to drop
NormalDrop on heavy load
Bulk (batch)Drop first
Health/adminReserved capacity

4. Using Adaptive Throttling

SignalAction
p99 latency > SLOReduce concurrency
Error rate > 5%Apply backoff
CPU > 80%Shed lowest priority
HealthyGradually raise limits

5. Configuring Backpressure Mechanisms

Client → [Gateway in:1000 rps]
              ↓ slows accept
         [Worker pool full]
              ↓ TCP window shrinks
         [Client OS sees slow ACK]
              ↓ Reactive client throttles
      

6. Setting Throttle Response Codes (429)

Example: 429 response

HTTP/1.1 429 Too Many Requests
Retry-After: 30
RateLimit-Limit: 60
RateLimit-Remaining: 0
RateLimit-Reset: 30
Content-Type: application/problem+json

{ "type":"about:blank", "title":"Rate limit exceeded", "status":429 }

7. Implementing Quota Management

Quota TypeWindow
Daily24h rolling
MonthlyCalendar or 30d
Per-endpointDifferent by route
Soft vs hardWarn vs block

8. Configuring Throttle Retry Headers

HeaderFormat
Retry-AfterSeconds or HTTP date
X-RateLimit-ResetUnix epoch
X-BackoffRecommended jitter ms

9. Setting Up Client-Specific Limits

Example: Per-consumer override

consumers:
  - username: enterprise-corp
    plugins:
      - name: rate-limiting
        config: { minute: 100000 }
  - username: free-tier-user
    plugins:
      - name: rate-limiting
        config: { minute: 60 }

10. Using Dynamic Throttling Rules

TriggerRule Change
IncidentLower all limits 50%
Black FridayRaise premium limits 2x
Abuse patternPer-IP penalty box
Backend degradationAuto-shed by tier