LESSON 02 · Placement

Choose where requests are checked

The gate must run before the expensive work and share policy across instances.

SYSTEM VIEW

The decision path

Diagram view
Swipe or scroll to explore the full diagram
Check before expensive workThe gateway or middleware consults rules and shared counters before the API server receives allowed traffic.Check before expensive workrequestload ruledecideallowClientsends requestGatewayenforce quotaRulespolicy cacheCountersshared fast stateAPI serviceallowed workClient pacing helps, but server-side enforcement owns the guarantee.
The gateway or middleware consults rules and shared counters before the API server receives allowed traffic.
Trace the quota decision before a request reaches the API.
THE IDEA

How it works

Client-side throttling can improve behavior, but an untrusted client can bypass it. A gateway, a proxy in front of your services, or middleware, code that runs before each handler, can reject requests before application work; an application-level limiter can use richer business context, such as a customer’s plan. Amazon API Gateway and NGINX both offer rate limiting without custom code, which is a good reason to start there. Existing gateway features, control over custom rules, and the cost of adding a new hop drive the choice.

A typical path is client → load balancer → limiter → API service. The limiter reads the current rule and counter, makes one decision, then forwards an allowed request or returns a throttle response. Keep policy configuration separate from fast-changing counters: rules change rarely and can be cached, while counters change on every request. A centrally shared counter store lets many stateless limiter instances enforce one logical quota.

Placement has a measurable payoff. In an example where an abusive client sends 5,000 requests per second and each would use 20 ms of application CPU, rejecting them at the gate saves 100 CPU cores of work (5,000 × 0.02 s). The price is a store round trip on allowed requests too, and one more dependency that can fail. Authenticate before any per-key limit, since an unverified key could spend someone else’s quota, and keep a coarse IP limit in front of authentication to protect it.

DECISION POINT

A mobile app already waits between calls. Why does the API still need its own limiter?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Place enforcement before the resource it protects.
  • Separate rule distribution from counter updates.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

Why enforce a public API quota before application work?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.