· 3 min read
Understanding API gateways
I've been reading up on API gateways lately. If you're running microservices, most requests pass through one before they reach a service, so I wanted to write down how they work.
The hotel analogy
Think of an API gateway like a luxury hotel's front desk.
Just like hotel guests don't need to know exactly where the housekeeping room or maintenance room is located, your clients don't need to know about the internal structure of your microservice architecture. The gateway handles that coordination for you.
An API gateway is a single entry point for all client requests. It routes each request to the right backend service and handles centralized middleware tasks like authentication, routing, and request handling.
Without an API gateway, the client would need to know exactly where to go to make a request. The gateway is meant to be a thin, fairly simple component.
How a request flows
A request goes from the client, through the gateway's steps, to the backend services.
What the gateway does
1. Request validation
Before doing anything else, the API gateway checks if incoming requests are properly formatted and contain all the required info (headers, URLs, body, etc.). If something is wrong, the gateway can quickly reject it and send back a helpful error message.
2. Middleware
You can set up the gateway to handle middleware tasks so your services don't have to. Common examples:
- Authentication (JWT, etc.)
- Rate limiting
- SSL termination
- Logging and traffic monitoring
- Handling CORS
- Whitelisting/Blacklisting
3. Request routing
The gateway decides which backend service handles each incoming request. A typical routing config looks something like this:
routes:
- path: /users/*
service: user-service
port: 8080
- path: /orders/*
service: order-service
port: 8081
- path: /payments/*
service: payment-service
port: 8082
4. Transforming
Most services talk over HTTP, but sometimes a backend service uses a different protocol like gRPC for internal communication. The gateway can convert the request into the format that service expects (JSON to Protobuf, for example) and convert it back again.
5. Caching
Before sending the response back to the client, the gateway can also cache it, either in memory or in a distributed cache like Redis.
Gateways scale out like any stateless service
API gateways are stateless and can be scaled horizontally. You can also run the gateway closer to the user, the way you would with a CDN.
Popular gateways
If you're looking for tools, these are the common ones:
- Cloud: AWS API Gateway, Azure API Management, Google Cloud Endpoints.
- Open source: Express Gateway, Kong, Tyk.
The short version
Use an API gateway when you have multiple microservices. Without it, everything is tightly coupled, and the client would need to know how to communicate with multiple services directly.
Used LLMs to correct grammar, typos etc