Skip to main content

Rate Limiting: Token Bucket vs Leaky Bucket on AWS WAF and API Gateway

Cloud ArchitectureUpdated Palaniappan P2 min read

Quick summary: Token buckets allow bursts and leaky buckets smooth traffic. WAF rate rules and API Gateway usage plans implement neither perfectly, but both matter for layered defense.

Key Takeaways

  • WAF rate rules and API Gateway usage plans implement neither perfectly, but both matter for layered defense
  • June 2026: Layer edge (CloudFront + WAF), API (Gateway throttling), and app (ElastiCache token bucket) limits
  • Never rely on Lambda concurrency alone
  • Do not rely on Lambda concurrency alone
  • Set account-level API Gateway throttle guardrails
A gold token bucket and a charcoal leaky bucket stacked in front of WAF and API Gateway cards
Table of Contents

June 2026: Layer edge (CloudFront + WAF), API (Gateway throttling), and app (ElastiCache token bucket) limits. Attackers hit the cheapest layer first.

Symptom → mechanism → AWS control

Production symptomMechanismAWS control
Burst traffic triggers scale stormNo burst allowanceAPI Gateway throttle burst limit > steady rate
Scrapers exhaust backendPer-IP unlimited requestsWAF rate-based rules, CloudFront geo restrictions
Legitimate clients throttledLeaky bucket too aggressiveToken bucket with per-API-key quotas via Usage Plans

Opinionated take: Layer token-bucket limits at API Gateway for product quotas and WAF rate rules for abuse. Never rely on Lambda concurrency alone.

Algorithms

AlgorithmBehaviorAWS analog
Token bucketAllows bursts up to bucket sizeAPI GW burst limits
Fixed windowCount per windowWAF rate-based rule
Fixed windowSimple counter per minuteWAF classic rate rule

Opinionated take: Combine WAF IP rate limit with per-API-key usage plan. Do not rely on Lambda concurrency alone.

AWS services map

NeedServiceSkip when
Per-API steady + burst limitsAPI Gateway throttling + Usage PlansInternal VPC traffic behind NLB
Per-IP abuse blockingWAF rate-based rulesSingle-tenant B2B with fixed IP allowlist
Edge rate limitingCloudFront + WAF at edgegRPC internal mesh traffic

What to do this week

  1. Set account-level API Gateway throttle guardrails.
  2. Add WAF rate rule on /login and expensive GraphQL paths.
  3. Implement app-level bucket in Redis for partner APIs.

More in This Track

Part of the Engineering Guides library (June 2026).

What this guide doesn’t cover

For API Gateway REST vs HTTP, see the canonical API Gateway post.

Frequently asked questions

Can Lambda reserved concurrency replace WAF and API Gateway throttles?
No. Attackers hit the cheapest layer first. Layer CloudFront plus WAF at the edge, API Gateway usage plans for product quotas, and an app-level Redis bucket for partner APIs. Lambda concurrency is a last backstop, not a rate-limit design.
When should I use a token bucket versus a leaky bucket?
Token buckets allow bursts (API Gateway burst vs steady). Leaky buckets smooth output. WAF rate-based rules are neither: they count requests per IP over a fixed evaluation window (1, 2, 5, or 10 minutes). Legitimate clients need burst headroom; scrapers need per-IP WAF caps. Do not pick one layer and skip the others.
When is a WAF per-IP rule the wrong control?
Single-tenant B2B with a fixed IP allowlist, or internal VPC traffic behind an NLB. Also skip edge WAF on gRPC mesh traffic that never faces the internet.
What could go wrong if burst is set equal to the steady rate?
A 30-second legitimate spike becomes a scale storm or a 429 storm. Set burst above steady on the usage plan, and put abuse caps on /login and expensive GraphQL paths separately.
Palaniappan P
Palaniappan P

AWS Cloud Architect & AI Expert

AWS-certified cloud architect and AI expert with deep expertise in cloud migrations, cost optimization, and generative AI on AWS.

AWS ArchitectureCloud MigrationGenAI on AWSCost OptimizationDevOps

Recommended Reading

Explore All Articles »