API gateway
One front door for several backend services: what a gateway can centralise, and why a monolith usually does not need one.
The reception desk. One front door that sends each request to the right team.
That is an API gateway. Clients talk to one address. The gateway decides which backend service handles each request, and it does the chores that every team would otherwise repeat.
How it works
The gateway sits in front of one or more backend services and is the single entry point for clients.
Client → API gateway → ├── Auth service
├── Users service
├── Orders service
└── Payments service
The jobs a gateway can take on:
- Routing. Send
/ordersto the orders service and/usersto the users service. - Authentication. Check the credential once, at the door, instead of in every service.
- Rate limiting. Apply limits in one place.
- Request transformation. Reshape a request or response so clients see one consistent API.
- Logging. One place to record every request.
- Service discovery. Find where each service currently runs.
The benefit is that cross-cutting concerns live once. A change to the rate limit or the token check is one change, not a change per service.
How it differs from a load balancer
A load balancer spreads traffic across copies of the same thing. A gateway chooses between different things. They often sit together: the gateway routes to the orders service, and a load balancer spreads that traffic across the orders instances.
When it earns its place
The gateway starts to pay when:
- there are several backend services, not one
- clients should see a single consistent entry point
- auth, limits and logging are being copied between services
Edge and internal gateways
A gateway can face the outside world or sit between internal services. The public one handles clients, credentials and limits. An internal one can handle service-to-service routing and shared policy. Mixing both jobs in one component is where trouble starts, because the rules for an anonymous visitor and a trusted internal caller are different. If I ever needed one, I would keep the public entry point thin and put anything specific to a single service inside that service. A gateway that knows too much about each service stops being a front door and becomes a bottleneck for every team.
Where I used it
I haven't shipped this in a public project yet.
The reason is the shape of what I build. IMS is a modular monolith on one Postgres database. There are no separate services to route between, so there is nothing for a gateway to decide. The boundaries exist inside the code as modules instead. I would not add a gateway there, and the first trade-off below is the reason.
Trade-offs
- Not useful for a monolith. With one backend, a plain load balancer is usually enough. A gateway adds a layer and a hop with little to show for it until real services exist.
- A new single point of failure. Everything goes through it. It needs redundancy and health checks of its own.
- It can become a dumping ground. Business logic creeps in because the gateway is convenient. Then you have a hidden service that nobody owns. I keep it to routing and cross-cutting rules only.
- Latency. Every request takes one more hop and some processing.
- Coupling. If the gateway knows every service's route, a service change needs a gateway change. Version the routes and keep the mapping in configuration.
- Debugging. A failed request now has one more place to fail. Request ids that pass through the gateway to every service keep traces readable.
The rule I use: count the backend services first. One service means a load balancer. Several services, with the same checks being copied between them, is the point where a gateway starts to make sense.
Related notes
Seen in
Building something like this?
Tell me what you're building