Stage 2 · Building blocks · Medium · 14 min

Reverse proxies and API gateways

An API gateway is one front door for all your services. See how it ends HTTPS, checks the login token, limits each client and routes each request by its path.

What each interview level is expected to know

Learn first: HTTP, TCP, UDP and connections, Load balancing and health checks

After this lesson you can:

  • List what a gateway does at the front door: HTTPS, login checks, limits and routing.
  • Explain why business rules do not belong in the gateway.
  • Spot a gateway that is a single point of failure, or retries that multiply load.

Start here

See it in 60 seconds

What the front door checks

An API gateway is one front door for every request: it checks the login, limits each client, then sends the request to the right service.

Scene 1 of 6 · One front door

Every request from outside reaches the API gateway first, never a service directly.

AppsAppsAPI gatewayAPI gatewayUsers serviceUsers serviceOrders serviceOrders service
  • Apps — healthy
  • API gateway — healthy
  • Users service — healthy
  • Orders service — healthy
  1. One front door. Every request from outside reaches the API gateway first, never a service directly.
  2. Check the login. The gateway checks the request's login token and passes on who the user is to the service.
  3. Limit each client. It also counts each client's requests: past 10 a second, it answers 429 Too Many Requests.
  4. Route by path. Then it routes by path: /users goes to the users service, and /orders to the orders service.
  5. Stopped at the door. A request with no valid token stops at the gateway with 401 Unauthorized, and no service ever sees it.
  6. One place for rules. Rules in one place keep services simple, but the gateway must stay fast and never go down.
  7. Now you try. Send a request through the gateway and see where it goes. For example: The gateway checks the token and the limit, then sends /users/42 to the users service. The path decides the service; the client never needs to know where that service runs.

Scene 1 of 6: One front door. Every request from outside reaches the API gateway first, never a service directly.

Key points

  1. Outside requests reach the gateway first, so services behind it never face the internet.
  2. The gateway checks the login token once; each service still decides what that user may do.
  3. It limits each client, here to 10 requests a second, and answers 429 past that.
  4. It routes by path, and it must stay fast and always up, since everything passes through it.

Go deeper in the Rate limiting lab →

In plain words

Say an app has a users service, an orders service and a search service. A phone should not need three addresses, and no service should face the internet directly. A reverse proxy is a server that takes each request from outside and passes it to a server inside. An is a reverse proxy that also does shared checks first. Is the login token valid? Is this client over its limit?

Follow one request from the phone. It goes to one address, such as api.shop.example. The gateway checks the token and the client's , and turns the request away at once if either fails. Otherwise it reads the path, such as /orders/7, and passes the request to the orders service. The services behind it can then trust that those checks ran, though many still check who is calling.

The problem it solves

Every public service must end , check logins and limit abuse. With ten services, that is the same work written ten times, and ten chances to get it wrong. Clients must also know where each service lives, so moving one means changing every client.

Why it matters

Without a gateway, every service faces the internet, and every team writes its own login check and rate limit. One mistake in any of them is a way in. A gateway does those shared jobs once, in one place, for every request.

A worked example

A gateway checks a caller's identity and rate limit before forwarding a request, adding about 3 ms of extra work. If the internal service normally answers in 47 ms, the whole request then takes about 47 + 3 = 50 ms. No internal service has to handle raw traffic from outside.

Think of it like…

Think of the reception desk in an office building. Every visitor stops there first. The desk checks who they are, then sends them to the right office.

Where this analogy stops working

One front door that checks everyone is the useful part. But reception lets you walk on alone. A gateway stays in the middle: every request and every reply passes through it. So it adds a little delay, and if it stops, nothing gets in.

Try it

See it working

Live simulationOpen the full lab →

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.

Also try: Rate limiting · Load balancer · Request flow

Mechanics

How it works

Every request from outside goes through the gateway first. It runs the same checks on each one, in order, and only then passes it to a service. The pictures follow one phone app, one gateway and three services: users, orders and search.

  1. End HTTPS at the edge

    The gateway is the one public door to all your services. It decrypts HTTPS there, so the services behind it do not each manage certificates. This is called termination: TLS is the encryption that HTTPS uses. From the gateway to the services, the traffic is then plain , unless you encrypt that hop too.

  2. Check who is calling, and how often

    Pick a case to follow below

    With this case: The request passed the token check and the limit (12 of 100), and the orders service answered 200.

    Phone app, outsidePhone appoutsideAPI gateway, the front door, NOWAPI gatewaythe front doorHTTPS ends hereNOWUsers serviceUsers serviceOrders serviceOrders serviceSearch serviceSearch service
    • Phone app — outside — healthy
    • API gateway — the front door — healthy — NOW — HTTPS ends here
    • Users service — healthy
    • Orders service — healthy
    • Search service — healthy
    • Request
    • Answer
    Step 1 of 4

    Step 1 of 4 · API gateway

    Done: The request reaches the gateway

    What happens
    The phone app sends GET /orders/7 over HTTPS. The gateway decrypts it, so it can read the request.
    Why
    The gateway is the only public address. The services behind it are not on the internet.
    What changes
    The gateway now runs its checks, in order.
    Next
    The login token checks out
    Login token
    –
    not checked yet
    Requests this minute
    11 of 100
    from this user
    Answer
    –
    Reached a service
    no

    Step 1 of 4: API gateway. The request reaches the gateway. The phone app sends GET /orders/7 over HTTPS. The gateway decrypts it, so it can read the request.

    The gateway checks the login token on each request, for example the signature on a . Then it counts the user's requests. In the picture, one request for order 7 meets both checks. Switch between the three cases.

    A request with no valid token gets 401 Unauthorized at the gateway. A user over the limit, here 100 requests a minute, gets 429 Too Many Requests. Either way the request stops at the door, and no service sees it.

    That is the point of one front door. Each check is written once, at the gateway, instead of once in every service. A service behind it can trust that a request was checked, as long as nothing outside can reach that service directly.

  3. Limit and block

    The gateway counts requests per user or per . A caller that goes over the limit gets HTTP 429 (too many requests). Requests over the limit stop here and do not reach a service.

  4. Route by path

    Pick a case to follow below

    With this case: The rule /users sent /users/42 to the users service.

    Phone app, outsidePhone appoutsideAPI gateway, the front door, NOWAPI gatewaythe front doorreading the pathNOWUsers serviceUsers serviceOrders serviceOrders serviceSearch serviceSearch service
    • Phone app — outside — healthy
    • API gateway — the front door — healthy — NOW — reading the path
    • Users service — healthy
    • Orders service — healthy
    • Search service — healthy
    • Request
    • Answer
    Step 1 of 2

    Step 1 of 2 · API gateway

    Done: GET /users/42 passes the checks

    What happens
    The token is valid and the user is under the limit, so the gateway looks at the path.
    Why
    Routing happens after the shared checks, so only allowed requests are forwarded.
    What changes
    The gateway compares the path with its route table.
    Next
    /users/42 goes to the users service
    Path
    /users/42
    Rule that matches
    /users → users
    first match wins
    Answer
    –
    not sent on yet

    Step 1 of 2: API gateway. GET /users/42 passes the checks. The token is valid and the user is under the limit, so the gateway looks at the path.

    What these pictures simplify. These pictures send one request at a time through a gateway with three services. It has a limit of 100 requests a minute and three route rules. A real gateway handles many requests at once and has many more rules. The API gateway lab above runs live traffic.

    The gateway reads the URL path and forwards the request to the right service. In the picture, pick a path and see which rule matches.

    Each rule maps the start of a path to a service: /users to the users service, /orders to the orders service. The app knows one address, so you can move or split a service by changing one rule, not every app.

    A path with no rule gets 404 at the gateway, as /admin does here. So an internal tool stays private unless you add a rule for it.

What the gateway checks, in order
CheckThe questionIf the answer is no
Login tokenIs there a valid, ?401 Unauthorized
Rate limitIs this user under the limit?429 Too Many Requests
RouteDoes a rule match the path?404 Not Found

Use it when

  • Several services behind one public API: clients call one address, and you can move or split a service without changing them.
  • Checks that are the same for every request, such as the login token and rate limits: do them once at the gateway.
  • Releasing a new version step by step: the gateway sends it a small share of traffic first (a ).

Skip it when

  • One app on one server: there is nothing to route between, so a gateway adds a network hop and one more part to run.
  • Calls between your own services: they can call each other directly, or through a . Going out through the public gateway adds delay.

Check yourself

Which job belongs in an API gateway?

Judgement

Trade-offs and how it fails

What you give up

Security
One place to enforce security rules is easier to get right than many. But it is also one place to fail: if the gateway is down, every service is unreachable. Run at least two copies behind a .
Performance
Every request makes one more hop and waits for the gateway's checks, so each one is a little slower. In return, a bad request is turned away before any service spends work on it.

Failure modes

  • Business rules in the gateway. Once it holds discount rules or database queries, every team must change and release it, and it becomes the slow part.
  • A single gateway with no spare: when it stops, every service is cut off, though each one is healthy.
  • Retries that multiply load: the gateway sends each failed request to a slow service again, which can keep that service down. Cap the retries.

In the real world

  • Envoy was built at Lyft as a proxy that sits beside every service, and is now a common base for gateways.
  • Amazon API Gateway is a managed gateway that handles sign-in checks, limits and routing in front of AWS Lambda functions.

Trade-offs to be ready to discuss: Gateway vs service mesh

Check yourself

What is the main risk of sending every request through one gateway?

Interview

What interviewers look for

  • Put a gateway in front of your services, and say what it does. "It ends HTTPS, checks the token, limits each client and routes by path."
  • Say how it stays up: two or more copies behind a load balancer, with rate-limit counts in a shared store such as .
  • Keep it thin: say that business rules stay in the services.

Say it out loud: Why enforce rate limits at the gateway, and not inside each service?

A team wants to put the discount rules for checkout inside the API gateway. Why is that a poor idea?

Expected depth by interview level

Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

JuniorUseful
What a gateway does: routing, auth, rate limits.
MidExpected
Where to terminate TLS and enforce auth and limits.
SeniorExpected
The gateway as a bottleneck and a single point of failure.
StaffUseful
Gateway versus service mesh at platform scale.

Practice

Apply it

Three harder questions

The first two use the idea on a new case. The last asks you to judge a design.

Apply it

A gateway checks the token, then the rate limit, then routes the request. A request for order 7 arrives with an expired token. What happens?

Apply it

A gateway retries each failed request up to 3 times. A slow service fails 1,000 requests a second. Up to how many requests a second can now reach it?

Judge a design

TLS ends at the gateway, so traffic from the gateway to the services is plain HTTP. That inside network is shared with other teams. What should change?

Next: what that token is, and how a system knows who a user is and what they may do.

Go deeper

Sources and further reading

First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • Official docsProxy servers and tunnelingMDN Web DocsThe difference between a forward proxy and a reverse proxy, and the headers they add.
  • Official docsModule ngx_http_proxy_modulenginx documentationThe real settings of a reverse proxy: timeouts, buffering, retries to the next server.