In this article, we will learn what is API Gateway and why we need this.
We will learn all about API gateway in the context of microservices.
API gateway is a very deep topic and here it is required to understand the below things but in step-by-step.
- Problem with monolithic
- Why do we need an API gateway?
- What is an API gateway
- Benefits of API gateway
- API gateway design patterns
- Type of API gateway
- How to select the right API gateway based on requirements
- Difference between API gateway vs Load balancer
- Difference between API gateway vs proxy server
What is API Gateway?
Problem statement 1
Consider applications running in a microservice arch so each microservice is deployed individually with its independent URLs.
Here, if the application has a lot of microservices, then it will be very difficult for the UI to maintain all URLs of microservices.
Here API gateway comes into the picture and behaves as the single entry point for the client and distributes requests to the corresponding micro service.
Flow in API Gateway
- Client request: When a request is made from a client, the request first goes to the API gateway, and then the API gateway checks if requesting path and re-routes the request to the corresponding server or microservices.
This diagram shows that all client request goes to the API gateway and then the request goes to microservices.
Use Case 1
Suppose UI has all microservice URLs in their config, in the future if we change the API service URL then all UI code needs to be updated which needs extra effort from UI DEV along with the Backend developer.
Here if we use an API gateway that manages all URLs then the Backend developer is required here we do not need to update the UI so we will save time and cost also.
Use Case 2. Performance Metric
As this is a single entry point for all clients' requests this is the best place to log all requests, here we can measure various metrics like request locations, request count, response time, etc.
Use Case 3. Rate Limiting
With this feature, we can respond back to the client if several requests are more than specific.
For example, suppose we want to implement like, there should be a 1sec interval between 2 requests then we can use this feature and respond back to the client and this will help to refuse of unnecessary requests made by the bot or some software or hacker.
Use Case 4. Authentication
Authentication: as the API gateway is the first entry point for a client we can implement authentication and authorization at this place.
This is no need to apply to auth in each microservice, only request validation is required at the microservice.
API gateway capable of performing below, and these are benefits also,
Use Case 5. Request Composition (Aggregator Pattern)
Consider a scenario where it is requested to call multiple microservices from a single place and needs to combine results from all services and respond.
Here various factors need to be considered if we need to make a Sync call or an async call to microservice to get data.
It is a separate topic we need to discuss later on.
Use Case 6. Protocol Transformation and Payload Transformation
Suppose we want to modify a request from the client as per service acceptance. Then we can do it at this Place (API gateway)
In this article, we learn about API gateways and their few use cases.