API Gateway Load Testing: Measure Overhead and Limits
API gateway load testing: measure per-request overhead, test rate limits without drowning in 429s, check upstream pools and quotas, with a numbered plan.
To load test an API gateway, send the same arrival rate to a service directly and through the gateway, then compare latency percentiles and error rates. The difference is the gateway’s overhead. After that, push past the gateway’s own limits (rate limits, connection pools, managed quotas, autoscaling) and record what fails first.
A gateway sits on every request, so a small per-request cost multiplies by your whole traffic. It is also shared: one tenant’s burst consumes capacity for everyone behind it. The Canva API gateway outage of 2024 shows that this layer fails as a unit.
Prerequisites
- A gateway and services you own or are authorised to load, in a production-like environment.
- The gateway’s rate-limit configuration and, for a managed gateway, your provider’s current quota values.
- Access to the gateway’s metrics and logs: upstream latency, connection counts, error codes, instance count.
- A load generator in the same region as the gateway.
What the gateway adds to each request
Every request crosses several steps before it reaches a service. Each one costs CPU, memory or a network round trip.
- TLS termination. The handshake for new connections is expensive, and a connection that is reused avoids it. See HTTP keep-alive and connection reuse.
- Authentication. Validating a JWT locally is cheap. Calling an identity provider or an introspection endpoint per request adds a network hop and a new dependency under load.
- Rate limiting and quotas. Counters often live in a shared store, so the limiter itself becomes a bottleneck.
- Routing. Matching a path against hundreds of routes costs more than matching against ten, depending on the implementation.
- Transformation and plugins. Header rewrites, body validation, request and response transformation, logging, and WAF rules all add per-request work, and body inspection scales with payload size.
- Upstream connection handling. The gateway opens, pools and reuses connections to services, and that pool has limits.
The test should expose each of these. A single “hit the gateway with 5,000 users” run mixes them together.
Measure the overhead: direct versus gateway
The core experiment is a controlled comparison.
- Pick one endpoint and one payload.
- Run it directly against the service at a fixed arrival rate, for example 200 requests per second, and record p50, p95, p99 and the error rate.
- Run the identical request against the same service through the gateway, at the same rate, from the same location.
- Subtract.
Use an arrival rate (a fixed number of new requests per second), not a fixed number of users. With a fixed user count, a slower gateway reduces the request rate, so the comparison measures different loads. An arrival-rate model holds the load constant and lets latency move.
Repeat at several rates, such as 25, 50 and 75 percent of the target. Overhead that is flat across rates is a fixed per-request cost. Overhead that grows with rate means the gateway is queueing or running out of CPU. Run both paths in the same region, otherwise network distance hides the gateway’s contribution.
A k6 sketch that sends the same load to both paths:
import http from 'k6/http';
const target = __ENV.TARGET; // set to the direct URL or the gateway URL
export const options = { scenarios: { steady: { executor: 'constant-arrival-rate', rate: 200, timeUnit: '1s', duration: '5m', preAllocatedVUs: 100, maxVUs: 400, }, }, thresholds: { http_req_duration: ['p(95)<300'], http_req_failed: ['rate<0.01'], },};
export default function () { http.get(`${target}/v1/products/123`, { headers: { Authorization: `Bearer ${__ENV.API_TOKEN}` }, });}Run it twice with a different TARGET. If maxVUs is reached, k6 drops iterations and reports it, which means the load generator, not the system, was the limit. Size it generously.
Rate limits and the 429 problem
A rate limiter makes a load test easy to misread. Once the test exceeds the limit, a growing share of responses are 429s. They are fast, because rejection is cheap, so average latency falls while the error rate climbs. A test that mixes accepted and rejected responses reports a blend that describes neither.
Treat these as two tests.
Capacity test, under the limit. Run with a test identity whose limit is raised, or with the limit set above the target rate in the test environment. Failure criteria include any 429, because a 429 here means the test was misconfigured. This test measures latency and saturation.
Limiter test, over the limit. Deliberately exceed the limit and check behaviour: do 429 responses arrive quickly, include a Retry-After or rate-limit headers, apply per the right key (API key, user, IP), and stop when the client backs off? Check the limiter’s accuracy as well. Send a known rate and count how many get through against the configured limit. Distributed limiters with shared counters sometimes allow slightly more than the limit across nodes, so know your tolerance.
Also decide what identity the load generator presents. If every virtual user shares one API key, one per-key limit applies to the whole test. If your runners share a small set of IP addresses, per-IP limits apply to the whole test as well. Load testing rate-limited APIs goes deeper on shaping traffic against limits.
Upstream pools and connection reuse
Between the gateway and a service there is a connection pool. If it is too small, requests queue in the gateway and latency grows while the service sits idle. If idle timeouts differ, the gateway reuses a connection the service already closed, and a fraction of requests fail with a reset or a 502.
Symptoms to look for during the run: errors that appear only through the gateway, 502 or 504 responses with low service CPU, and latency that grows with the rate while the service’s own latency stays flat. Compare the gateway’s upstream latency metric with the service’s server-side latency. A large gap is queueing or connection setup in the gateway.
Test with connection reuse on, as real clients do, and also with it off to see the worst case for TLS handshakes per second. Both matter: mobile clients and serverless callers often open new connections.
Managed gateway quotas and throttles
Cloud API gateway products enforce limits you do not control from your own configuration: account-level request throttles, per-route or per-stage limits, payload size caps, integration timeouts, and in some products a burst allowance. Providers raise some of them through a support request, with lead time.
Before any test against a managed gateway, read your provider’s current limits page and check the quota values in your account, then request increases early. Do not rely on numbers from a blog post, including this one. A load test that hits an account-level throttle also affects every other API in that account, so run it in an isolated account or stage, or schedule it with the owners of the other workloads.
Autoscaling and self-hosted gateways
A self-hosted gateway (Kong, Envoy-based, NGINX-based and similar) scales by adding instances. Autoscaling reacts after load rises, so the first minutes of a spike hit the old capacity.
Test this directly:
- Start from the normal instance count and apply a step increase, not a gentle ramp. Record how long latency stays above budget before new instances serve traffic.
- Check what happens to in-flight connections when instances are added or removed, since long-lived connections stay with the old ones.
- Check the shared dependencies: the rate-limit store, the config database and the identity provider. These often saturate before the gateway does.
- Scale in under load and confirm that draining connections does not produce errors.
Test plan
- Write the targets. Peak requests per second per route, latency budget, acceptable error rate, and the expected rate-limit configuration.
- Build a production-like setup. Same gateway version, plugins, TLS settings, instance size and autoscaling policy as production, with services behind it.
- Baseline direct. Run the constant-arrival-rate test against a service directly at 25, 50 and 75 percent of target and record the percentiles.
- Run the same through the gateway. Same rates, same location. Calculate overhead at each level.
- Capacity test under the limit. Ramp to the target and beyond, with the limiter set above the test rate. Fail the run on any 429.
- Limiter test over the limit. Exceed the configured limit with a known rate, and check the 429 rate, headers, keying and recovery after backing off.
- Connection test. Repeat the capacity run with connection reuse disabled, and watch for upstream resets and 502 responses.
- Step-load for autoscaling. Apply a sudden step and record time to recover.
- Check quotas. For managed gateways, confirm in advance which account-level throttles apply, and watch for provider throttle responses during the run.
- Fix, rerun, compare.
Failure criteria
Set pass and fail before the run, per route:
- p95 and p99 latency through the gateway within the budget at target rate.
- Gateway overhead (via gateway minus direct) within an agreed figure at p95.
- Error rate under your threshold, with zero 429 responses in the capacity test.
- 429 responses in the limiter test within an agreed tolerance of the configured limit.
- Recovery to baseline latency within a set time after the autoscaling step.
- No 502 or 504 responses attributable to upstream pool or timeout settings.
Worked example
These numbers are hypothetical, to show how to read the results.
An orders API has a target of 300 requests per second, with a p95 budget of 250 ms.
Direct to the service at 300 requests per second, p95 is 90 ms. Through the gateway at the same rate, p95 is 120 ms. The overhead is 30 ms at p95. At 75 requests per second it was 12 ms, so overhead grows with load, which points at queueing or CPU in the gateway.
Raising the rate to 450 requests per second, p95 through the gateway climbs to 340 ms while service CPU stays low. The gateway’s upstream pool is capped at 50 connections per instance, and requests wait for a free one. Raising the pool to 150 brings p95 to 135 ms at 450 requests per second.
The limiter test sets 100 requests per minute per API key and sends 150 per minute. About 50 per minute return 429 with a Retry-After header, and the count matches the configured limit within a few requests. A client that backs off recovers on the next window.
The autoscaling step from 100 to 450 requests per second keeps p95 above budget for 70 seconds while new instances start. The team decides to raise the minimum instance count before the daily peak.
How to run it in MaxoPerf
- Create two tests from the same k6 script, one with
TARGETpointing at the service and one at the gateway. MaxoPerf detects the executor from the uploaded file. Run both from the same location so network distance stays equal. - Set the load profile and location plan in the test configuration. For a gateway inside a private network, use a private datacenter (BYOC) location.
- Store API keys and tokens as project secrets and bind them to the tests. Use a separate key for the limiter test.
- Add SLA gates on p95 latency and error rate, so a breach marks the run as failed.
- Compare the two runs’ latency charts side by side, and line them up with the gateway’s own metrics for upstream latency, connections and instance count.
Only test gateways you own or are authorised to load. For the wider picture of testing the services behind the gateway, see how to load test microservices and the API performance testing guide.
Troubleshooting
- Errors through the gateway, none direct. Compare gateway error codes with service logs. 502 and 504 with low service CPU point at the upstream pool or mismatched idle timeouts.
- Overhead grows with rate. The gateway queues or runs out of CPU. Check instance count and per-instance CPU, then the auth and rate-limit dependencies.
- Latency looks great but throughput is lower than set. Most responses are 429. Raise the limit for the test identity and rerun.
- k6 reports dropped iterations. The load generator ran out of virtual users. Raise
maxVUsor add runners. - Throttling you did not configure. A managed provider’s account-level limit applies. Check the provider’s quota page and request an increase.
Questions this article answers
How do you load test an API gateway?
Send the same arrival rate to a backend service directly and through the gateway, then compare latency percentiles and errors. The difference is the gateway's overhead. Then raise the rate past the gateway's own limits to find where it saturates, throttles or starts failing upstream connections.
How much latency does an API gateway add?
It depends on the gateway, the plugins enabled and where it runs relative to the service, so there is no number you should assume. Measure it: the same requests at the same rate, direct and through the gateway, in the same region. Report the difference at p50, p95 and p99.
Should a load test hit the rate limit?
Run two separate tests. One stays under the limit and measures latency and capacity. The other deliberately exceeds it and checks that the limiter returns 429 responses with correct headers, quickly and consistently. A mixed test produces latency numbers that are mostly measurements of rejected requests.
Do managed API gateways have throttling limits?
Yes. Cloud API gateway products generally enforce account-level and per-route throttles and payload or timeout limits, and some are raised on request. Check your provider's current documentation and quota page before the test, and request increases ahead of time.
Why do I see errors through the gateway but not direct?
Common causes are an upstream connection pool that is too small, idle connection timeouts that differ between gateway and service, a rate limit or quota being hit, or gateway instances that have not scaled yet. Compare the gateway's own error codes and logs with the backend's to see which side produced the failure.