Walk into a large office building and you don’t wander the corridors looking for the right team. You go to the reception desk. The receptionist checks your ID, gives you a visitor badge, and tells you “Payments is on the 3rd floor, room 301”. Every visitor goes through the same desk, so security is checked once, in one place.
When a system is split into many backend services, it needs the same thing. That’s an API gateway. This guide explains what it does, how it differs from a load balancer, and when it’s worth adding.
What is an API gateway?
An API gateway is a server that sits between clients (mobile apps, websites, partner companies) and your backend services. Every request from outside goes to the gateway first. The gateway decides which service should handle it, forwards it there, and passes the answer back.
Without a gateway, a mobile app might need to know the addresses of the orders service, the payments service, the users service and the search service, and each of those services would have to check sign-ins, enforce limits and write logs on its own. With a gateway, the app knows one address, and the shared jobs happen once.
What an API gateway does
The core job is routing: looking at each request (usually its path, like /orders/42 or /payments) and sending it to the right service. On top of that, gateways usually handle the jobs every service would otherwise repeat:
- Authentication. Check the caller’s token (often a JWT) or API key, and reject unknown callers before they reach any service.
- Rate limiting. Allow, say, 100 requests per minute per user, and reply
429 Too Many Requestsbeyond that. See traffic control. - TLS termination. Handle HTTPS encryption at the edge, so internal services don’t each need certificates.
- Logging and metrics. Record every request in one place: who called, what, how long it took, whether it failed. Great for observability.
- Request shaping. Add or remove headers, translate between formats, or combine answers from several services into one response.
- Versioning and rollouts. Send
/v1/and/v2/to different code, or send 5% of traffic to a new release. See API versioning. - Caching. Store answers to common requests for a short while.
API gateway vs load balancer vs reverse proxy
These three get mixed up, because they all sit in front of servers and forward requests.
| Main question it answers | Typical extras | |
|---|---|---|
| Reverse proxy | “Forward this to a server behind me” | HTTPS, compression, caching |
| Load balancer | “Which copy of this service should get the request?” | Health checks, spreading load evenly |
| API gateway | “Which service should get this, and is the caller allowed?” | Sign-in checks, rate limits, API keys, request shaping |
An API gateway is really a specialised reverse proxy that understands APIs. In practice they often stack: the gateway picks the service, and a load balancer picks one healthy copy of that service. Many products can play more than one of these roles.
Common gateway products include Kong, Amazon API Gateway, Azure API Management, Google’s Apigee, and Spring Cloud Gateway. Proxies like NGINX and Envoy are often configured to act as gateways too.
Backend for frontend (BFF)
A mobile app and a website often want different things. The phone wants small answers to save data; the web page may want everything at once. One pattern is to run a separate gateway per type of client, called a backend for frontend. The mobile BFF combines and trims answers for phones, and the web BFF does it for browsers, while the services behind them stay the same.
When do you need an API gateway?
You probably do when:
- You have several backend services (microservices) that outside clients call.
- You offer a public API to partners, with keys, plans and usage limits.
- You keep writing the same sign-in and limit code in every service.
You may not when:
- You have one application (a monolith). A simple load balancer or reverse proxy in front of it is enough.
The downsides
- One more hop. Every request takes a little longer (usually a few milliseconds).
- A possible single point of failure. If the gateway is down, everything is down. Run several copies behind a load balancer, across zones. See failover.
- A possible bottleneck. All traffic passes through it, so it must scale with the whole system.
Common misconceptions
“An API gateway replaces the load balancer.” Often they work together: the gateway picks the service, a load balancer picks a healthy copy. Some products do both.
“With a gateway, services don’t need any security.” The gateway checks who the caller is, but services should still check what that caller is allowed to do with their data. Defence in depth.
“Gateways are only for microservices.” They are most useful there, but a public API in front of even one service still benefits from keys, limits and analytics.
Key takeaways
- An API gateway is the single front door for clients calling many backend services.
- It routes each request to the right service and handles shared jobs: sign-in checks, rate limits, HTTPS, logs.
- A load balancer picks a copy of one service; a gateway picks the service and checks the caller.
- Use one when you have many services or a public API; skip it for a single app.
- Keep it thin and run it with redundancy.
Frequently asked questions
What is an API gateway in simple words?
An API gateway is one entry point that sits in front of many backend services. Apps send every request to it, and it checks the request and forwards it to the right service.
What is the difference between an API gateway and a load balancer?
A load balancer spreads requests across several copies of the same service. An API gateway decides which service a request is for and handles shared jobs like sign-in checks and rate limits. They are often used together.
Is an API gateway a reverse proxy?
Yes, an API gateway is a kind of reverse proxy with extra features for APIs, such as authentication, rate limiting, API keys and request transformation.
Do microservices need an API gateway?
Usually, if outside clients call them. Without a gateway, every client must know every service’s address, and every service repeats the same security and limit checks.
Is an API gateway a single point of failure?
It can be, because all traffic passes through it. That’s why gateways are run as several copies behind a load balancer, often across different zones or regions.
Comments