Imagine the product details page for our Sportopia Inc’s online web store. That single page needs to fetch information from multiple services like:
- Product Info Service – basic information about the product such as title, author
- Pricing Service – product price
- Order service – purchase history for the product
- Inventory service – product availability
- Review service – customer review
There are multiple problems in a scenario like this:
- The granularity of APIs provided by microservices is often different than what a client needs. For example, as described above, a client needing the details for a product needs to fetch data from numerous services.
- Different clients need different data. For example, the desktop browser version of a product details page desktop is typically more elaborate than the mobile version.
- Network performance is different for different types of clients. Mobile network is typically much slower and has much higher latency than a non-mobile network
- Services might use a diverse set of protocols, some of which might not be web-friendly.
Welcome API Gateway
We could use an API Gateway to address the problems listed above. API gateway acts as a middle-tier from which the UI can interact with the microservices. It also provides a simpler interface and makes the process of consuming these services simpler. It provides a different level of granularity to different clients(browser and desktop).
An API gateway can act as a single entry point for all clients. It can handle requests in one of two ways. Some requests are simply routed to the appropriate service while other requests are handled by fanning out to multiple services.
A variation of this pattern is the Backends for Frontends pattern. It defines a separate API gateway for each kind of client. In the example below, there are three kinds of clients: web application, mobile application, and external 3rd party application. There are three different API gateways to service the three kinds of clients.
API gateway hides the actual APIs from its clients and then simply redirects the calls to the actual API from these clients. The API management solution provides a complete management system to manage all of the APIs of its external consumers.
API management solutions, such as Azure API Management (https://docs.microsoft.com/en-us/azure/api-management/), provide various capabilities and functionalities like: