Hierarchical Rate Limiting
You'll learn how to apply multiple layers of rate limits simultaneously to create a robust, fair protection system.
What Is Hierarchical Rate Limiting?
Hierarchical rate limiting means stacking multiple rate limit policies at different scopes—each acting as an independent safety valve. Instead of just limiting requests per-user, you enforce limits at per-user, per-account (organization), per-region, and global levels all at once. A request must pass through all layers to succeed.
Think of it like airport security: your ID is checked (per-user), your group booking is validated (per-account), the terminal's capacity is enforced (per-region), and the entire airport has a maximum throughput (global). Each gate protects a different resource.
Why Multiple Layers?
A single rate limit scope isn't enough:
- Per-user alone: One wealthy account with 1,000 users could overwhelm your system if each user maxes out their limit simultaneously.
- Per-account alone: Doesn't prevent one rogue user within that account from monopolizing shared quota.
- Global alone: Fair users suffer when bad actors consume all capacity.
By combining layers, you get defense in depth. An individual user might have a 100 req/min limit, their account 5,000 req/min, their region 50,000 req/min, and your global system 500,000 req/min.
Implementation Pattern
Each layer runs its own rate limit check (using algorithms you've learned: token bucket, sliding window, etc.):
if not check_user_limit(user_id): return 429
if not check_account_limit(account_id): return 429
if not check_region_limit(region): return 429
if not check_global_limit(): return 429
return process_request()
The first violation short-circuits the chain.
Key Takeaway: Hierarchical rate limiting creates layered protection by enforcing independent limits at user, account, regional, and global scopes—ensuring fairness and system stability even under complex attack patterns.