Course contentsShow
← System Design
Lesson 962 of 1,91925. Rate Limiting and ThrottlingFree lesson

Rate Limiting at Different Layers

Apply rate limiting at load balancer, API gateway, application, and database layers for defense in depth.

Rate Limiting at Different Layers

What you'll learn: How to apply rate limiting across multiple system layers—from the load balancer to the database—to create robust, defense-in-depth protection.

Why Multiple Layers?

Rate limiting at a single point creates a single point of failure. If that layer is bypassed, misconfigured, or overwhelmed, your entire system is exposed. Defense in depth means implementing rate limits at every major boundary, so if one layer fails, others still protect your resources.

Think of it like home security: you lock the front door, the bedroom door, and keep valuables in a safe. Each layer adds protection.

Rate Limiting Across Layers

Load Balancer Layer

Your first line of defense. Rate limit by IP address or geographic region before traffic even reaches your application servers. This stops basic DDoS attacks and obvious abuse patterns early, saving downstream resources.

API Gateway Layer

Apply authenticated user limits and per-endpoint quotas. Since the gateway sees the full request context (tokens, API keys, user IDs), it can enforce business-specific limits like "100 requests/minute per API key."

Application Layer

Implement fine-grained, context-aware limits. Your application understands business logic: premium vs. free users, internal vs. external calls, expensive vs. cheap operations. Apply different limits based on these factors.

Database Layer

Protect your most critical resource. Limit queries per connection or write operations per second. Even if upstream rate limiters fail, the database won't be overwhelmed by runaway queries.

Coordination Matters

Each layer should enforce limits appropriate to its visibility. The load balancer doesn't know user roles; the database doesn't know API endpoints. Configure them to work together, not redundantly—overlapping limits provide safety nets, not barriers.

Key Takeaway: Apply rate limiting at the load balancer, API gateway, application, and database layers to create defense in depth—each layer protects against different attack vectors and failure modes.