Reverse proxies and API gateways
Overview
Between the internet and your services sits a component that receives every request first. Depending on what it does it’s called a reverse proxy or an API gateway. It is where shared concerns such as encryption, authentication and rate limiting are handled once, instead of in every service.
Forward proxy vs reverse proxy
- A forward proxy acts for clients. A company proxy that all employees’ browsers go through is a forward proxy: websites see the proxy, not the employee.
- A reverse proxy acts for servers. Clients think they are talking to your site; in fact they talk to the proxy, which forwards requests to servers the client never sees.
NGINX, HAProxy, Envoy and Caddy are common reverse proxies. A load balancer is a kind of reverse proxy whose main job is spreading load.
What a reverse proxy does
- TLS termination: handle HTTPS certificates and encryption in one place.
- Load balancing across servers behind it.
- Compression of responses (gzip, Brotli).
- Caching of responses that are the same for everyone.
- Buffering slow clients: the proxy receives a slow mobile upload in full, then hands it to the app server quickly, so app threads aren’t tied up waiting.
- Security: hide internal addresses, filter bad requests, absorb some kinds of attack.
The API gateway
When a system has many services, clients shouldn’t need to know all of them. An API gateway is a reverse proxy specialised for APIs. Typical responsibilities:
- Routing:
/users/*to the user service,/orders/*to the order service. - Authentication: validate the access token once and pass the user ID to services in a trusted header.
- Rate limiting and quotas: for example 100 requests per minute per API key (week 6).
- Request IDs, logging and metrics: tag each request with an ID so it can be traced through every service.
- Protocol translation: accept REST or GraphQL from clients and call gRPC services internally.
- Response shaping: combine or trim responses for a client.
Managed options include AWS API Gateway, Google Apigee and Azure API Management; self-hosted ones include Kong, Envoy-based gateways and NGINX.
Keep the gateway thin
It’s tempting to put “just one more rule” in the gateway. Resist it. The gateway should handle cross-cutting concerns, things that are the same for every service. Business rules (“can this user cancel this order?”, “what discount applies?”) belong in the services that own the data. A gateway full of business logic becomes a monolith that every team must change and nobody owns.
Backend for frontend (BFF)
Different clients want different data. A mobile screen may need a compact payload combining three services; a web dashboard may want much more. A backend for frontend is a small gateway-like service per client type (one for iOS/Android, one for web) that aggregates and shapes data for that client. It moves client-specific logic out of both the shared gateway and the core services.
Costs and risks
- Extra hop: a few milliseconds of latency per request. Usually acceptable.
- Single point of failure: run several instances across zones behind a load balancer, or use a managed service.
- Bottleneck: the gateway sees all traffic; it must scale horizontally and stay stateless (rate-limit counters live in a shared store such as Redis).
- Blast radius: a bad gateway config can take down every API at once, so change it carefully and roll out gradually.
In an interview
For a system with several services, draw a gateway (or “load balancer + API gateway”) between clients and services and say briefly what it does: “The gateway terminates TLS, validates the JWT, applies per-user rate limits and routes by path.” That one sentence answers questions about authentication and abuse before they’re asked.
Recap
A reverse proxy stands in front of your servers and handles TLS, compression, caching and load balancing. An API gateway is a reverse proxy for APIs that adds authentication, rate limiting, routing and observability. Keep it thin, stateless and highly available, and leave business rules to the services.
What belongs in the gateway?
A team is building an API gateway in front of five services. Decide whether each responsibility belongs in the gateway or in the services, and why: (a) checking that the request has a valid access token, (b) checking that the user may cancel *this specific* order, (c) limiting each API key to 100 requests per minute, (d) calculating the discount for a cart, (e) routing /orders/* to the order service, (f) logging every request with a request ID.
(a) Token check:
(b) May cancel this order:
(c) 100 req/min per key:
(d) Cart discount:
(e) Routing:
(f) Request logging:(a) Gateway. Validating a token's signature and expiry is the same for every service,
so doing it once at the edge avoids duplicated code and rejects bad requests early.
(b) Service. Whether this user owns this order is business logic that needs the order
data; only the order service knows it. (The gateway passes the user ID along.)
(c) Gateway. Rate limits per client are a cross-cutting concern and protect every
service at once.
(d) Service. Discounts are business logic; putting them in the gateway makes it a
hard-to-change monolith.
(e) Gateway. Routing by path is one of its core jobs.
(f) Gateway (and services). The gateway creates the request ID and logs the request;
services include the same ID in their own logs so one request can be traced end to end.Summary
- A reverse proxy sits in front of servers, receives client requests and forwards them, hiding the servers from clients.
- Reverse proxies commonly terminate TLS, compress, cache, buffer slow clients and balance load.
- An API gateway is a reverse proxy with API features: authentication, rate limiting, routing to services, request IDs and metrics.
- Keep business logic out of the gateway; it should handle cross-cutting concerns only.
- The gateway is on every request's path, so it must be highly available and fast.