Microservice Pattern : API Gateway
Tags: microservices, design
Published: 2021-05-31 17:49:45

<!-- wp:paragraph -->
<p>When we talk about the system design or see those big<strong> </strong><em> </em><strong>Mircoservice Architecture</strong> diagram, there is a component with name <strong><em>api-gateway </em></strong>which always comes into the picture. In this blog we will see what exactly is the <strong>API gateway</strong> and why do we need it, when and where to use it.</p>
<!-- /wp:paragraph -->



<!-- wp:paragraph -->
<p>Before reading this post or learning about the api-gateway, i would suggest you to learn about <code>proxy</code> and <code>reverse-proxy</code> first or go through my other blog in which i have shared the information about the same.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p id="api-gateway">Ok, now lets start with the definition of api-gateway </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"align":"center","fontSize":"medium","style":{"color":{"text":"#30513b"}}} -->
<p class="has-text-align-center has-text-color has-medium-font-size" style="color:#30513b"><em>"<span style="color:#04517f;" class="has-inline-color">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&nbsp;api&nbsp;calls, aggregate the various services required to fulfill them, and return the appropriate result.</span>"</em></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Customer Service</strong> : handles the new customer addition, updation, deletion etc.</li><li><strong>Product Service:</strong> gives all the information about the products.</li><li><strong>Inventory Service: </strong> takes care of all the quantity or availability of products.</li><li><strong>Order Service</strong> : handles payment and place the order.</li></ol>
<!-- /wp:list -->


<!-- wp:paragraph {"align":"left"} -->
<p class="has-text-align-left"><img class="wp-image-65" style="width:600px;" src="https://ishantgaurav131.files.wordpress.com/2020/08/api-gateway-2-1.png" alt=""></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As we have microservice architecture having more than one service for front-end to interact with, it would lead to below problems :</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Front-end itself has to play the role of API-Composer, it fetches the result from different services and combine the result.</li><li>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.</li><li>We would have to implement authorization/authentication mechanism for all the service separately for each individual call from front end to backend.</li><li> We are not able to implement common caching mechanism.</li><li>All the service will have to follow communication protocol like REST/HTTP  as client is directly communicating to services.</li><li>From Security point of view, will have to implement HTTPS with all the service.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Now let's see how an <code>api-gateway</code> can overcome all the above problems for us and what other benefits it provides and come out as an essential component in microservices architecture.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><img class="wp-image-61" style="width:700px;" src="https://ishantgaurav131.files.wordpress.com/2020/08/api-gateway-1.png" alt=""></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The main responsibility of <code>api-gateway</code> is to route the request to the required service, perform protocol transformation and  combined the results from different services into one. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Apart from the main functionality <code>api-gateway</code> provides a lot of another benefits as well :</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Caching</strong> : Response from the services can be cached in api-gateway to prevent network call.</li><li><strong>Authorization/Authentication</strong> : Instead of verifying the identity of the client and authentication at each service, it can be implemented in <code>api-gateway</code>.</li><li><strong>Less Network calls : </strong>As we can see now the client will have to make one only network call to <code>api-gateway</code> 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.</li><li><strong>Different protocol:</strong> Now the backend service can be implemented in different protocol like <code>REST/HTTP</code> and <code>gRPC</code> and not just client friendly protocol as <code>HTTP</code> and <code>WebSocket</code>. </li><li><strong>Rate Limiting :</strong> Limiting how many requests per second from either a specific client and/or from all clients.</li><li><strong>Metrics collection:</strong>  Collect metrics on API usage for billing analytics purposes. </li><li><strong>Request logging:</strong> Log requests to different services which can help later on in debugging.</li><li><strong>Easy to add another service</strong> : Now we can easily add another service in the backend without client being aware of that.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph {"fontSize":"medium"} -->
<p class="has-medium-font-size"><strong>Drawback of using API-Gateway : </strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>In the microservice architecture, it  becomes the one highly available component which needs to be developed, deployed and managed carefully.</li><li>Add more complexity to the application architecture.</li></ol>
<!-- /wp:list -->

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!