Lab · Building blocks

API gateway

Three clients send requests through one gateway to three services. The gateway checks each API key, applies each client's rate limit, and routes by path. See what it stops, what a retry hides, and why one gateway is a single point of failure.

Try it

Lab time 3.0 s
/users/orders/searchMobile app, 8 requests/sMobile app8 requests/s4/10 tokensPartner, 6 requests/sPartner6 requests/s10/10 tokensBot, off, IDLEBotoffno valid API keyIDLEAPI gateway, 1 of 1 instance upAPI gateway1 of 1 instance uplimit 10/s per clientUsers, 40 ms a callUsers40 ms a call20 callsOrders, 80 ms a callOrders80 ms a call13 callsSearch, 120 ms a callSearch120 ms a call20 calls

The lab is busy, so only some requests are drawn as dots, few enough to follow. The numbers still count every request.

  • Mobile app — 8 requests/s — healthy — 4/10 tokens
  • Partner — 6 requests/s — healthy — 10/10 tokens
  • Bot — off — IDLE — no valid API key
  • API gateway — 1 of 1 instance up — healthy — limit 10/s per client
  • Users — 40 ms a call — healthy — 20 calls
  • Orders — 80 ms a call — healthy — 13 calls
  • Search — 120 ms a call — healthy — 20 calls
  • Request
  • Response
  • Turned away (401, 429, 503) or error
  • Retry
Answered
19/s
Stopped at the gateway
0/s
bad API key (401) or over the limit (429)
Failed after routing
0/s
Orders requests failed
0%
Orders calls per request
1.00
Gateway instances up
1 of 1

Numbers add up:53 sent = 0 stopped (401) + 0 stopped (429) + 52 answered + 0 failed + 0 no gateway + 1 in flightThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Clients
6 requests/s

The mobile app always sends 8 a second.

Sends 4 requests a second.

Rate limit
10/s
Limit

Each client has its own token bucket.

Services
0% of calls

The gateway calls the service a second time after an error.

Gateway
Gateway instances

Each client

ClientSent401429503OKFailed
Mobile app33000320
Partner20000200
Bot000000

401: no valid API key. 429: over the limit. The gateway stopped both. 503: the load balancer found no running gateway to take the request. Only OK and Failed requests reached a service.

Requests per second
AnsweredStopped at the gatewayNo gateway (503)Failed after routing
06.312.518.8251s2s3s
Show data table
TimeAnsweredStopped at the gatewayNo gateway (503)Failed after routing
1s12000
1s14000
2s18000
2s16.5000
3s17.5000
3s19000

What just happened

  1. 0.0sInfo: Each client with a valid API key may send 10 requests a second.
What this model simplifies
  • Time is slowed so you can watch each request. Service times are fixed and illustrative: Users 40 ms, Orders 80 ms, Search 120 ms.
  • Every call here reads data, so a retry is safe. A gateway should not retry a write unless it carries an idempotency key.
  • The gateway checks a token and a limit in 5 ms and never runs short of capacity itself. A real gateway can become a bottleneck.
  • A real gateway also ends TLS, rewrites requests and writes logs. Those are left out because they change no numbers here.
  • Real gateways also limit by IP address before the key check, so a flood of bad requests still costs them a little work.
  • A restarted instance takes traffic at once. A real load balancer waits for a passing health check, and often answers 502 for a crashed instance.
  • The load balancer in front of the gateway instances is not drawn, and it never fails.