A comparison of the API Gateway and the Backend For Frontend (BFF) pattern. The API Gateway is a single point of entry into the system for all clients, while a BFF is only responsible for a single type of client. To choose between those patterns we need to consider several factors...
One part of modern microservice systems is the so called API Gateway. A component sitting on the edge of the network for the system, through which all communication between clients and the system flows. However there is also the Backend For Frontend (BFF) pattern, which is a variation of the API Gateway pattern. We will talk briefly about the differences of those two patterns and when to use which one.
From a high-level perspective both patterns follow the same goals. They are decoupling the system's services from the actual clients that use the system. Such a decoupling can be useful, because if clients would talk directly to each microservice they would need to know about the location of each service and either all services need to support the client's communication protocol or the client would need to support multiple communication protocols offered by different services.
When using an API Gateway clients need to only to know about the API Gateway's location, whether the request is served by service A or service B does not matter to the client and therefore there should be no direct coupling between service A and the client.
The API Gateway is then able to handle cross-cutting concerns like Authentication, Rate Limiting and to translate between the client's and the service's communication protocol.
So far these properties hold true for both API Gateways and BFFs, but how do they differ?
While an API Gateway is a single point of entry into the system for all clients, a BFF is only responsible for a single type of client. Let's assume your system has two (typical) clients: a Single Page Application (SPA) and a mobile client (Android, iOS). In this case each client would get its own API Gateway, which would therefore be a BFF.
When we have to decider whether we want to use the API Gateway or the BFF approach, there are several indicators that can help us to decide between the two approaches.
In case there are several clients we have to ask ourselves:
You might also be interest in Microservice Bad Smells - Common Problems in Microservices.
If you want to know more about the API Gateway and Backend For Frontend patterns you should definitely check out the API Gateway page on microservices.io. An even more detailed description of this can be found in the book Microservice Patterns, whose author is also the owner of microservices.io.