Microservice Pattern : API Gateway
Ishant Gaurav
Posted on May 31, 2021
When we talk about the system design or see those big Mircoservice Architecture diagram, there is a component with name api-gateway which always comes into the picture. In this blog we will see what exactly is the API gateway and why do we need it, when and where to use it.
Before reading this post or learning about the api-gateway, i would suggest you to learn about proxy
and reverse-proxy
first or go through my other blog in which i have shared the information about the same.
Ok, now lets start with the definition of api-gateway
"API gateway is a tool that sits between the client and the collection of backened services. An API gateway acts as a reverse proxy to accept all api calls, aggregate the various services required to fulfill them, and return the appropriate result."
Let's understand the problem we would face if we don't use the API gateway. Let's take a scenario of an basic ecommerce website which has microservice architecture as below:
- Customer Service : handles the new customer addition, updation, deletion etc.
- Product Service: gives all the information about the products.
- Inventory Service: takes care of all the quantity or availability of products.
- Order Service : handles payment and place the order.
As we have microservice architecture having more than one service for front-end to interact with, it would lead to below problems :
- Front-end itself has to play the role of API-Composer, it fetches the result from different services and combine the result.
- Front end will have to call all the each service separately, which mean there will be 4 network calls, which could cause performance issue to the application.
- We would have to implement authorization/authentication mechanism for all the service separately for each individual call from front end to backend.
- We are not able to implement common caching mechanism.
- All the service will have to follow communication protocol like REST/HTTP as client is directly communicating to services.
- From Security point of view, will have to implement HTTPS with all the service.
Now let's see how an api-gateway
can overcome all the above problems for us and what other benefits it provides and come out as an essential component in microservices architecture.
The main responsibility of api-gateway
is to route the request to the required service, perform protocol transformation and combined the results from different services into one.
Apart from the main functionality api-gateway
provides a lot of another benefits as well :
- Caching : Response from the services can be cached in api-gateway to prevent network call.
-
Authorization/Authentication : Instead of verifying the identity of the client and authentication at each service, it can be implemented in
api-gateway
. -
Less Network calls : As we can see now the client will have to make one only network call to
api-gateway
and further call to services will be executed by the api-gateway only which are in same network, hence it will enhance the performance a lot. -
Different protocol: Now the backend service can be implemented in different protocol like
REST/HTTP
andgRPC
and not just client friendly protocol asHTTP
andWebSocket
. - Rate Limiting : Limiting how many requests per second from either a specific client and/or from all clients.
- Metrics collection: Collect metrics on API usage for billing analytics purposes.
- Request logging: Log requests to different services which can help later on in debugging.
- Easy to add another service : Now we can easily add another service in the backend without client being aware of that.
Drawback of using API-Gateway :
- In the microservice architecture, it becomes the one highly available component which needs to be developed, deployed and managed carefully.
- Add more complexity to the application architecture.
This blog was first posted on my blog as https://ishantgaurav131.wordpress.com/. If you find this blog relevant, you can visit my blog for more such content.
This is all from this Post. Thanks for reading, and let me know your thoughts in the comments!
Posted on May 31, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 11, 2024