Part 4 of 9 · 2 min read
The API gateway
One public door in front of many internal services, and why clients should not know the internals.
Think of a hotel front desk. Guests do not walk to the kitchen, the laundry and the maintenance office. They ask the desk. The desk knows who does what, and the guests never need to learn the building.
When an application is split into many services, the same need appears. The book "Understanding Distributed Systems" lists the problems of letting clients call services directly.
- A screen may need several requests to different services. On mobile devices every request uses battery.
- Clients must know the internal details, such as the names of the services. Changing the architecture then requires every client to be updated.
- Apps installed from a store cannot be forced to update. Once a public API exists, it must be kept for a very long time.
The solution is a layer of indirection.
API gateway
An API gateway is a service that exposes the public API and forwards calls to the internal services. Clients talk to the gateway and do not know that the services exist.
Example: a mobile app calls GET /home on the gateway. The gateway asks the product service, the cart service and the offers service, and returns one answer.
The book gives the gateway three main features, and a fourth group of jobs.
- Routing. A map ties public paths to internal services.
- Composition. One public call fans out into several internal calls.
- Translation. The gateway changes one protocol into another.
- Cross-cutting jobs. Authentication, caching, rate limits and similar work that every service would need.
Mobile app ---+
Web app ---+---> [ API gateway ] ---> product service
Partner ---+ | ---> cart service
| ---> offers service
(finds services via the registry)
An API gateway is often a layer 7 reverse proxy (page 3). The book notes that the responsibilities of a layer 7 balancer and a gateway overlap, since both are HTTP proxies. The difference is in the job. A balancer spreads load over copies of one service. A gateway knows about many different services and about the public API.
Read it as
The load balancer answers "which copy?". The gateway answers "which service, and what to combine?".
Check yourself:
- Name two problems of letting clients call internal services directly.
- What is the difference in purpose between a load balancer and a gateway?