Especially when talking about microservices, many articles and videos focus on an architectural element that, in their opinion, is absolutely necessary for building such kinds of applications: API Gateways.
Yes, they are actually useful, but they are not the solution to every architectural problem.
In this article, we will learn what API Gateways are, their pros and cons, why you should distinguish them from Load Balancers, and more.
Imagine that your application is made of several web services and APIs. Each of them will be deployed on a separate host, therefore having a different URL.
While it can be helpful for internal development - you have a clear separation of operations available in your system - it can become cumbersome for the developers who have to integrate all those endpoints in their application.
That’s where API Gateways come into play: they add a sort of façade in front of your set of APIs to uniform the access to your system.
Say that you’ve built a Hotel booking system that is made of 3 services:
By adding an API Gateway, you can hide these APIs behind a single host, like https://api.mycompany.org/, mapping the route path to the original services’ URLs.
As with everything in the world, there are advantages and disadvantages. Let’s learn! 🔥
API Gateways have lots of advantages. It’s not a coincidence that they are being used in many architectures.
Let’s see some of the best characteristics of an API Gateway.
Since your internal APIs are hidden behind an API Gateway, you make them less discoverable by intruders who might want to access your systems and data.
Let’s go back to the Hotel Booking system in our example. External users will only see the api.mycompany.com host without knowing the structure of the internal API services. This reduces the attack surface.
You can also centralize Access Control Policies by adding them to the API Gateway and have them applied to all the APIs behind the Gateway.
Finally, you can monitor all the incoming requests and analyze the traffic to spot unusual behavior in a single place.
Some concerns are common to all the APIs within the same system, such as rate limiting, throttling, and access control.
With API Gateways, you can centralize the settings for such common concerns in a single point. For example, once you have defined some rate limiting policies on your Gateway, you can have the same settings automatically applied to every internal API endpoint (well, not “physically” applied to the internal APIs, but since the Gateway blocks some incoming requests, they will not reach the internal systems as well).
API Gateways also help with service documentation: since you have all the internal services listed in the API Gateway, just by looking at its configurations, you can see the list of the deployed APIs and their hostname.
Since API Gateways act as a façade in front of other services, you can integrate services that work with different protocols or formats.
For example, you can have one service that exposes GraphQL APIs, another one that works with GRPC, and have them all exposed as REST APIs using the API Gateway as a wrapper/converter to and from such formats.
Hiding the actual API endpoints behind a Gateway also allows you to implement API Composition, which is a technique that will enable you to aggregate the results from different internal API calls and return the caller only the final result (as opposed as making the client call each single service and compose the result afterwards).
Depending on the vendor, you can have different tools to improve the overall performance.
For example, you can cache responses to get quicker response times, or you can apply uniformed data compressions.
Some vendors allow you to have multiple instances of the same service available behind the API Gateway, acting as a sort of Load Balancer.
Another powerful technique you can implement is SSL Termination. In a simple architecture, if you have to communicate securely with three systems to aggregate the result, you have to validate the SSL connection three times. With SSL Termination, you can move these operations on the API Gateway component and avoid doing that in all the other services, making the application more performant.
Since we placed an API Gateway in front of all the other API applications, all the external communication passes through the API Gateway.
This means that if the API Gateway is unavailable, the whole system becomes unavailable as well, even though the single APIs are still up and running.
So, when using an API Gateway:
An API Gateway is an additional hop in the API processing: when a client wants to call an API application, all requests must first pass through the API Gateway, which, in turn, will call the internal API.
This processing, this routing, adds some network latency. Maybe it’s not noticeable, but it depends on the Gateway you choose, the underlying infrastructure, and the processing done by the Gateway.
Once you have deployed and configured an API Gateway, moving to another vendor might be difficult.
You risk vendor lock-in, with all the consequences: if the price goes up or if they remove some functionalities that you need, you will have to come up with a solution to migrate that specific part of your infrastructure to another vendor.
It might not be so easy - some straightforward ways to mitigate the risk are to make your systems platform agnostic and to create e2e tests that validate the routing and the functionalities.
Some people I know and the authors of some articles I read use API Gateway and Load Balancer as synonyms.
Let me get it straight: they are not the same!
Yes, they both are components that work with incoming requests. But they serve two totally different purposes.
While Load Balancers “just” spread incoming requests across multiple instances of the same service, API Gateways act as a façade in front of different types of services.
They are two totally different components of an architecture.
Some vendors provide both functionalities, while others don’t. So, keep the two meanings distinct, and try to use the correct wording.
Here is a list of some API Gateway vendors’ prices, pros, and cons.
Notice: I have yet to try all these vendors. The info in this list comes primarily from their official documentation and, in some cases, from other resources that compare such products.
It’s not the first time we talked about API Gateways in this blog: in fact, there’s an old article where I shared some things I learned about API Gateways the first time I heard about them:
🔗 API Gateways - an overview | Code4IT
We also learned how to use Azure API Management to integrate the API Gateways product provided by Azure to organize some .NET APIs.
🔗 How to create an API Gateway using Azure API Management | Code4IT
This article taught us that API Gateways are different from Load Balancers. Do you know what are some types of Load Balancers and why you should care?
🔗 Davide’s Code and Architecture Notes - L4 vs L7 Load Balancer | Code4IT
Beware those who say that API Gateways are «the best thing to add to a microservice architecture»! As we learned, there are some downsides, too.
Always make sure that the products and the components you choose fit your necessities and that you learn when something is not for you.
I hope you enjoyed this article! Let’s keep in touch on Twitter or LinkedIn! 🤜🤛
Happy coding!
🐧
Also published here.