Reverse proxy, API gateway and BFF
A phone app needs three services for one screen. Let it call them all, put one front door in front, teach that door about tokens and limits, then give the phone a backend of its own. Then send a request with no token, and a burst.
What you'll take away
- Tell a reverse proxy, an API gateway and a BFF apart by what each one does
- Explain why a gateway answers 401 at the door, yet services still check permissions
- Set a rate limit per client that answers
429with aRetry-Afterheader - Know when a BFF pays off: one call over mobile, only what the screen shows
The 1-minute version
Tap a point and the picture below plays that step.
100 ms a trip over the mobile network, 5 ms inside the data centre, a limit of 5 a second per client: the model's numbers
How is an API gateway different from a reverse proxy?
The picture: your network is a walled compound with three services inside; the phone is outside, on the internet. With nothing in front the wall has a gap and every service faces the street (amber outline); a reverse proxy or a gateway is a gate in the wall, a BFF a small building just inside. Blue lights are the app's calls, violet the BFF's trips inside, red the bad requests; the phone's label adds up its calls, its wait and what it downloaded. Every number comes from a model, not a real network.
Compare them all: Reverse proxy, API gateway and BFF
Every tile gets the same three moments: the app opens a home screen that needs three services, a request arrives with no token, and one client fires 10 requests against a limit of 5. See how long the app waits, and where the bad ones are stopped.
Use it when A small system with one service, or internal services talking behind the scenes
Skip it when Several services called from outside: each does its own TLS, auth and rate limits, and each faces the internet
A model: 100 ms a mobile round trip, 5 ms inside the data centre, made-up payload sizes. Press any tile to see it large above, with when it fits.
Who does what
| Reverse proxy | API gateway | BFF | |
|---|---|---|---|
| One door, TLS, routes | Yes | Yes | Behind the gateway |
| Load balancing, health checks | Yes | Often | No |
| Token checks, limits per client | Little (per IP) | Yes | No |
| Shapes data for one screen | No | A little | Yes |
A gateway knows who, not what's allowed
The gateway can tell the services who the caller is. Whether that person may see order 1001 is each service's own rule, so services still check permissions, and they shouldn't be reachable from outside at all.
Keep the door thin
Every request passes through it, so run several copies and keep business logic out. When a phone screen and a web page need very different data, a BFF per client keeps that shaping out of the gateway too.
In real code
# A reverse proxy in front of three services: one door, routes by path, and a rate limit per client IP.
# (TLS would end here too: listen 443 ssl with ssl_certificate lines; left out so `nginx -t` runs without cert files.)
limit_req_zone $binary_remote_addr zone=per_ip:10m rate=5r/s;
upstream profile { server 10.0.1.10:8080; server 10.0.1.11:8080; }
upstream orders { server 10.0.2.10:8080; server 10.0.2.11:8080; }
upstream recs { server 10.0.3.10:8080; }
server {
listen 80;
limit_req zone=per_ip burst=5 nodelay; # past the limit: 429 below
limit_req_status 429;
location /api/profile/ { proxy_pass http://profile/; }
location /api/orders/ { proxy_pass http://orders/; }
location /api/recs/ { proxy_pass http://recs/; }
location / { return 404; } # nothing else gets through
}
nginx -t (this file goes in /etc/nginx/conf.d/nginx.conf)It printed: nginx: configuration file test is successfulOne door in front of three services: routes by path, a rate limit per client IP (limit_req, 429 past it), and 404 for anything else. TLS would end here too; those lines are left out so nginx -t runs without certificate files. Checked with nginx -t in Docker.
Common questions
How is an API gateway different from a reverse proxy?
Both take requests and pass them on. A reverse proxy handles TLS, routing and load balancing; a gateway adds the API work every service would otherwise repeat, like token checks and rate limits per client, done in one place.
Why not let the app call each service directly?
You can, but then every service does its own TLS and token checks, and every one of them faces the internet. With one front door they hide behind it, and you can move or split them without the app knowing.
A client goes over its rate limit. What should the gateway answer?
429 Too Many Requests, with a Retry-After header saying how long to wait. A good client waits instead of hammering away. In the picture one client fires 10 in a second against a limit of 5: 5 get through, the rest get 429.
The gateway checks tokens. Do the services still need to check anything?
Yes. The gateway can say who the caller is, but whether that person may see order 1001 is each service's own rule. And services shouldn't be reachable from outside at all, or anyone can walk around the door.
Do I need a BFF? When is it worth it?
When the screens really differ, like a phone app needing very different data from the web page. A BFF gathers and trims data for one kind of client and saves trips over the mobile network, but it's one more service per client to look after.
If the gateway goes down, does everything stop?
Yes, every request passes through it. So run several copies behind a load balancer and keep business logic out, or it becomes the bottleneck every team has to queue up to change.
How does a load balancer differ from an API gateway?
A load balancer spreads requests over copies of one service and checks their health, at TCP (L4) or HTTP (L7). A gateway works at the API level: which service, whose token, how much quota is left. Many gateways balance load as well.
Next lesson
API securityLevel: Intermediate
The gateway knows who is calling, not what they may do; next, 401 vs 403, RBAC and ABAC, and rate limiters.