Proxies, reverse proxies and API gateways
A reverse proxy sits in front of your servers and receives requests on their behalf. An API gateway is a reverse proxy with API features: authentication, rate limiting and routing to the right service.
Why it matters
With many services, every client would need to know every service's address, and every service would repeat the same login checks, rate limits and logging. That's duplicated, inconsistent work.
The idea
Clients talk to one entry point instead of many servers. That entry point can handle TLS, compress responses, cache, check tokens, apply rate limits, and send /orders to the order service and /users to the user service.
A hotel reception. Guests don't walk into the kitchen or the laundry; reception checks who they are and passes each request to the right department.
A reception desk with one person is a bottleneck and a single point of failure. A gateway must itself be replicated and kept fast, or every request suffers.
How it works
- The client sends every request to one address, for example api.example.com.
- The gateway handles TLS, then checks the token (who is this?) and the rate limit (are they allowed this many requests?).
- It looks at the path: /orders/… goes to the order service, /users/… to the user service.
- It forwards the request, waits for the answer and sends it back, logging the request and how long it took.
- Behind it, each service may run several copies, so a load balancer (often built into the gateway) picks one.
Example
Nginx or Envoy as a reverse proxy; a managed API gateway that checks tokens and allows 100 requests per minute per API key before anything reaches the services.
Key points
- Forward proxy: acts for clients (for example, a company's web proxy). Reverse proxy: acts for servers.
- Gateway jobs: routing, authentication, rate limiting, TLS, request logging, API versioning.
- A load balancer spreads traffic across copies of a service; a gateway decides what's allowed and which service it goes to. Many products do both.
- Keep business logic out of the gateway.
Common mistakes
- Putting business rules in the gateway, so it slowly becomes a hidden monolith.
- Running a single gateway instance: a single point of failure.
- Doing heavy work in the gateway that slows down every request.
Not true: An API gateway is a good place to put business rules.
In fact: A gateway should stay thin: routing, authentication, rate limiting. Business rules belong in the services, or the gateway slowly becomes a hidden monolith.
In the interview
I'd put an API gateway at the edge to authenticate requests, apply rate limits and route each path to the right microservice, so services don't each re-implement those concerns. It's replicated, so it isn't a single point of failure.
Part of Unit 1: Networking and APIs in the free System Design course.