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.
- Apps — healthy
- API gateway — healthy
- Users service — healthy
- Orders service — healthy
- One front door. Every request from outside reaches the API gateway first, never a service directly.
- Check the login. The gateway checks the request's login token and passes on who the user is to the service.
- Limit each client. It also counts each client's requests: past 10 a second, it answers 429 Too Many Requests.
- Route by path. Then it routes by path: /users goes to the users service, and /orders to the orders service.
- Stopped at the door. A request with no valid token stops at the gateway with 401 Unauthorized, and no service ever sees it.
- One place for rules. Rules in one place keep services simple, but the gateway must stay fast and never go down.
- 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
- Outside requests reach the gateway first, so services behind it never face the internet.
- The gateway checks the login token once; each service still decides what that user may do.
- It limits each client, here to 10 requests a second, and answers 429 past that.
- It routes by path, and it must stay fast and always up, since everything passes through it.
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 (One front door for all outside requests. It checks logins and limits, then sends each request to the right service.) 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 (A cap on how many requests a client can make in a given time; requests over the cap are delayed or rejected.), 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 (HTTP with every message scrambled by a secret key, after the server proves who it is with a certificate a trusted authority signed.), 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
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.
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 (Transport Layer Security: the encryption behind the S in HTTPS. It hides the data on its way and lets the browser check it reached the real server.) termination: TLS is the encryption that HTTPS uses. From the gateway to the services, the traffic is then plain (The rules an app or browser uses to ask a server for something and get an answer, such as GET /profile/42 and then 200 OK.), unless you encrypt that hop too.
Check who is calling, and how often
Pick a case to follow belowWith this case: The request passed the token check and the limit (12 of 100), and the orders service answered 200.
Phone app, outside Phone app outside API gateway, the front door, NOW API gateway the front door HTTPS ends here NOW Users service Users service Orders service Orders service Search service Search 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 4Step 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 yetRequests this minute11 of 100from this userAnswer–Reached a servicenoStep 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 (A common format for a signed token: the facts about the user, an expiry time, and a signature that servers can check.). 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.
Limit and block
The gateway counts requests per user or per (The number that identifies a machine on a network, such as 203.0.113.7. Packets are sent to it.). 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.
Route by path
Pick a case to follow belowWith this case: The rule /users sent /users/42 to the users service.
Phone app, outside Phone app outside API gateway, the front door, NOW API gateway the front door reading the path NOW Users service Users service Orders service Orders service Search service Search 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 2Step 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/42Rule that matches/users → usersfirst match winsAnswer–not sent on yetStep 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.
| Check | The question | If the answer is no |
|---|---|---|
| Login token | Is there a valid, (A short note that says who the user is and when it expires, with a signature that proves the server made it.)? | 401 Unauthorized |
| Rate limit | Is this user under the limit? | 429 Too Many Requests |
| Route | Does 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 (A release that gives the new version to a small share of the traffic first, such as 10%. If it fails there, few users are hurt and it is pulled back.)).
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 (A small helper program beside every copy of every service. It finds other services and sends the calls, so the service's own code does not have to.). Going out through the public gateway adds delay.
Check yourself
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 (A server that receives every request first and passes each one to one of several servers behind it.).
- 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
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 (A widely used data store that keeps its data in memory, so it answers much faster than a database on disk. It is often used as a cache.).
- 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?
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
Apply it
Judge a design
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.