AWS API Gateway
medium 28 minAmazon API Gateway is a managed front door for HTTP and WebSocket APIs.
See it run
A request, its limits, the authorizer cache, a canary and a long job.
Operations
One request
A GET with a token, first with a cache miss and then with a hit.
Step 1 of 7
Arrive and route
TLS ends. Stage and path choose the method's settings.
Step 1: Arrive and route
def handle(req): route = live_routes[req.method, req.path] policy = cached_policy(req.token) or authorize(req.token) check_throttles(route, req.key) if key(req) in cache: return cache[key(req)] resp = integrate(route, req) cache[key(req)] = resp return resp- Backend calls
- 1, then 0
- Feature
- REST API
Every check that fails early saves a backend call and its price.
In the real world
The same aws api gateway doing real jobs. Pick one and step through it.
Two Regional APIs with one name.
Step 1 of 3
Normal
Latency routing
Users go to the nearest region. The tables replicate in the background.
Step 1: Normal
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Long work inside the request
criticalThe gateway gives up at its timeout, the work goes on, and clients retry.
WrongGET /export # 90 secondsRightPOST /exports # 202 and a job IDGET /exports/42 # state, then a URLAn API key as a login
criticalKeys are for counting. They are long-lived, shared and easy to leak.
Wrongx-api-key: partner-key # the only checkRightJWT or IAM proves the callerthe API key only selects the planA cache key without the user
criticalThe first user's answer is returned to the second.
Wrongcache GET /me by pathRightdo not cache GET /meor make the user part of the keyA narrow cached policy
majorIt is reused for every method, so other calls get 403.
WrongResource: event.methodArnRightResource: everything this caller may doNo deployment
minorA REST stage serves a snapshot until you make a new one.
Wrongupdate-integration ...Rightupdate-integration ...create-deployment --stage-name prod
Interview tips
How to explain it
- A managed front door.
- Route, check, count, integrate.
- Nothing of ours runs inside it.
Numbers to know
- 10 MB, about 30 seconds.
- 10,000 a second per account and region.
- Caches: 300 seconds by default.
Reach for it when
- Serverless on AWS.
- Limits per customer.
- Small or uneven traffic.
Be careful with
- Shared account limits.
- Retries after 504.
- Price at steady high traffic.