API limits and versioning
Why limits matter
A gateway can protect a backend against excess demand, but it does not create backend capacity. A rate limit bounds admitted requests over time; a burst allowance permits short spikes; a quota often caps usage over a longer interval or at a specific resource scope. A concurrency limit bounds work in progress, not the same thing as requests per second. A quota on the number of deployed gateways is different from a per-consumer request allowance.
Suppose Byte Books can safely process 80 requests per second with its current backend. Its expected client demand is 60 requests per second, but promotions produce bursts. A global admission target of 60 requests per second leaves some capacity headroom; also reserve capacity for background work, retries and different operation costs. A per-client limit of 10 requests per second prevents one client taking the whole allocation but is insufficient on its own: ten clients can each send ten requests per second.
Work through a token bucket
A token bucket is one way to model a rate limit. Tokens refill at a fixed rate up to a maximum capacity. Each admitted request consumes one token. If no token is available, the model rejects the request. This is a serial, discrete-time teaching model; it does not claim that every provider implements these exact rules or a globally synchronized counter.
Take capacity 5, refill 2 tokens per second, initially full. At t=0 six simultaneous requests arrive: five admitted, one rejected, zero tokens left. At t=1 three arrive: two admitted, one rejected. At t=4 four arrive: the three-second refill adds six tokens but caps at five, so four pass and one token remains. Total admitted 11, rejected 2. A larger bucket permits a larger immediate burst without increasing sustained refill rate.
For a simplified fixed demand interval, 100 requests per second arriving against a strict admission target of 60 leaves 40 rejected per second, or 400 over 10 seconds, assuming no burst, queuing or other admission path. This is not a provider billing estimate. Real distributed counters may allow overshoot and different endpoint scopes; validate against representative traffic.
Quotas and provider differences
AWS API Gateway documents throttling targets and scope differences for HTTP and REST APIs. Treat throttles as traffic-management targets, not guaranteed billing limits. Azure API Management offers rate and quota policies with tier-dependent support; the key-based variants are not available in its Consumption tier according to the reviewed guidance. Google Cloud API Gateway quotas can be applied at the API rather than a single configuration; Apigee offers separate quota and spike-control policies. Review policy scope, key source, window, counter synchronization and response status for the chosen plan. Do not trust a caller-controlled header as a quota identity without validation.
Use a global protection limit plus appropriate authenticated consumer limits, separate expensive operations and measure downstream capacity. Apply timeouts, bound queues and budget for retry traffic. Example sources: AWS HTTP throttling, Azure throttling, Google quotas, Apigee rate limiting.
Retry only when justified
HTTP 429 can indicate a request-rate restriction and may provide Retry-After. A temporary 503 can also carry Retry-After. A safe client should respect the indicated delay, stay within a total time/attempt budget and use backoff with jitter when appropriate. Do not retry validation errors or permission failures blindly. A timeout does not establish that the server performed no work.
GET is safe and idempotent by HTTP semantics; PUT is idempotent in intended effect when the same replacement is repeated. A create-order POST is not automatically idempotent. To support repeat attempts, define a server-side idempotency key protocol with atomic storage, caller scope, a request fingerprint and a retention window; a duplicate with a different payload must not silently overwrite the first outcome. A gateway cannot guarantee exactly-once business effects merely by forwarding an idempotency header.
Example retry policy: allow at most three total attempts, wait at least the supplied Retry-After, stop if the remaining deadline cannot accommodate the wait and a new attempt. A create-order POST without a verified deduplication contract stops for reconciliation after an ambiguous failure. If 1,000 initial requests produce 80 first retries and 20 second retries, there are 1,100 attempts, not 1,000. Protect the backend and count attempts when estimating gateway request charges.
Version contracts deliberately
A version represents a client-visible contract, not simply a build number. Byte Books v1 returns {id, quantity}. Adding optional currency may be compatible with tolerant clients, but strict schema validators can reject unknown fields. Removing quantity, changing its type to a string, renaming it, or making a previously optional input mandatory is breaking. Validate old and new consumers before declaring compatibility.
For an incompatible change, expose a separately managed contract such as /v2/orders/B17, document the migration, keep v1 available during an announced support period, and monitor client usage before retirement. The backend can share business logic while separate adapters maintain each schema. Azure API Management distinguishes revisions from versions; verify the specific semantics before using these features. A deployment stage or revision is not automatically a new external version.
Use contract tests: required input, optional input, response status, field type, authorization boundaries and repeated requests. Roll out a change to an isolated test revision or deployment first; roll back the route only when compatible with changes to data. Never assume a gateway rollback reverses a database migration.
Estimate cost and observe results
Hypothetical rates only: 1.1 million attempts at $1.50 per million plus $2.00 in logs costs $3.65 for those two items. This excludes backend execution, data transfer, private connectivity, taxes, minimum charges and subscription fees. Actual tariffs depend on product, plan and region; rejected requests may still be metered according to the provider's rules. Check actual billing dimensions rather than assuming a rate limit is a spending cap.
Track request rate, admitted and rejected counts, status classes, backend latency, timeout rate and request IDs. Compare client-visible latency with backend time; a rising 429 rate suggests a policy or load problem, while 5xx errors need investigation of dependencies and integration. Use bounded synthetic traces before changing a limit. Practice: calculate the token bucket trace above, explain how to handle a timed-out create-order request, and propose a migration for a required field change. The next chapter introduces queues for asynchronous work.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
API limits and versioning — knowledge check
Choose the best answer for each offline scenario.
A full 5-token bucket receives 6 simultaneous unit-cost requests. How many pass?
Which distinction is correct?
A create-order POST times out with no deduplication contract. What is safest?
Which change is clearly breaking for an existing integer-field contract?
1,000 initial calls generate 80 first and 20 second retries. How many attempts occur?