AlgoViz

AWS API Gateway

medium 28 min

Amazon 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

Our systemMobile appAPI GatewayGET /usersAuthorizerStage cacheLambdaDynamoDB

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

Our systemUsersRoute 53API, region AAPI, region BTable replica ATable replica B

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

    critical

    The gateway gives up at its timeout, the work goes on, and clients retry.

    Wrong
    GET /export      # 90 seconds
    Right
    POST /exports    # 202 and a job IDGET /exports/42  # state, then a URL
  • An API key as a login

    critical

    Keys are for counting. They are long-lived, shared and easy to leak.

    Wrong
    x-api-key: partner-key   # the only check
    Right
    JWT or IAM proves the callerthe API key only selects the plan
  • A cache key without the user

    critical

    The first user's answer is returned to the second.

    Wrong
    cache GET /me by path
    Right
    do not cache GET /meor make the user part of the key
  • A narrow cached policy

    major

    It is reused for every method, so other calls get 403.

    Wrong
    Resource: event.methodArn
    Right
    Resource: everything this caller may do
  • No deployment

    minor

    A REST stage serves a snapshot until you make a new one.

    Wrong
    update-integration ...
    Right
    update-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.