avatarDineshchandgr - A Top writer in Technology

Summary

The article provides a comprehensive comparison between API Gateway and Backend for Frontend (BFF) patterns, discussing their usage, benefits, and drawbacks in the context of microservices architecture.

Abstract

The article "API Gateway vs Backend For Frontend (BFF) — Everything you need to know" delves into the roles of API Gateways and the BFF pattern in microservices architecture. It explains how an API Gateway acts as a reverse proxy, handling requests from multiple clients to various microservices, centralizing cross-cutting concerns like security, logging, and caching. The author highlights the benefits of using an API Gateway, such as abstracting client complexity, centralized authentication, and request transformation, while also acknowledging its potential drawbacks, including becoming a single point of failure and increasing latency. The BFF pattern is presented as a solution for scenarios with diverse client types, such as web, mobile, and IoT, by providing a separate API Gateway tailored for each client's needs, thus avoiding a monolithic gateway service. The article concludes by offering guidance on when to use an API Gateway versus a BFF approach, considering factors like the application's age, authentication requirements, and team ownership of client applications.

Opinions

  • The author suggests that a single API Gateway is sufficient when an application is new but recommends transitioning to BFF as the application scales and serves multiple client types.
  • The article implies that using a BFF pattern is more suitable when different clients require distinct authentication methods or when client applications are managed by separate teams.
  • The author emphasizes the importance of avoiding a monolithic API Gateway when dealing with multiple client types to prevent performance bottlenecks and maintain scalability.
  • It is the author's opinion that the BFF pattern, while more complex, can lead to a more efficient and tailored experience for each type of client by reducing unnecessary overhead and optimizing data aggregation.

API Gateway vs Backend For Frontend (BFF) — Everything you need to know

Hello everyone. In this article, we are going to see what is an API Gateway and how it is used for Client-Server communication in a Microservices architecture. We will also see the benefits and drawbacks of the API Gateway pattern. Then we will look at a pattern called Backend for Front End, which is a variant of the API Gateway pattern, and conclude the article by looking at when we need API GW vs BFF. Let's get started.

Image Source: https://raw.githubusercontent.com/abpio/abp-commercial-docs/rel-4.3/en/images/gateway-bff.png

What is an API Gateway?

The API Gateway is a server that handles many functionalities in a single place for the clients to interact. It also works as a reverse proxy between your client applications and the back-end microservices architecture

A reverse proxy is a server component that sits in front of web servers and 
they are responsible for forwarding the  client requests to those web servers
and send the response back to the client
 

A reverse proxy provides an additional level of abstraction and control 
to ensure the smooth flow of network traffic between clients and servers.

There could be multiple clients that call the server APIs and the API Gateway is the component that routes requests to the relevant microservice and then gets the response and send it to the client.

It handles all the cross-cutting features like Security, Logging, Caching, etc in a single place instead of us implementing these functionalities in every single microservices. Moreover, it can consolidate and aggregate the data across aggregating multiple microservices using a single endpoint for clients to communicate.

Before the evolution of Microservices architecture, most of the systems used a Monolith pattern and we could even handle the cross-cutting concerns in a single or couple of servers. But with microservices, we cannot afford to handle cross-cutting concerns in each of the microservices which will make things slower with a larger memory footprint degrading the performance of the system.

Client to Microservices Communication without using API Gateway

Let us look at a scenario in the microservices architecture without an API Gateway. In the below diagram, the clients directly communicate with the microservices without any API gateway involved.

Image Source: https://learn.microsoft.com/en-us/dotnet/architecture/microservices/architect-microservice-container-applications/media/direct-client-to-microservice-communication.png

There are a lot of drawbacks there.

  1. If the Microservices grow bigger and the system becomes more complicated, then the client has to remember each and every endpoint
  2. The client must make a lot of API calls to different microservices to load the data which could increase the latency and may not provide a good experience to the user
  3. As discussed above, cross-cutting concerns like Authorization, logging, Caching, etc must be implemented in every microservice
  4. Usually, Microservices will not have internet connectivity and they would be placed inside private subnets with private IPs. Opening up Microservices to the public is asking for trouble from attackers.
  5. The Clients may not be able to communicate with microservice that only supports specific protocols like AMQP or Binary protocols

Client Server Communication using API Gateway in Microservices Architecture

Image Source: https://hub.packtpub.com/wp-content/uploads/2018/02/B04926_02_05-600x427.png

We have seen a lot of drawbacks without using an API Gateway in a Microservices architecture. To eliminate those drawbacks and make our lives convenient, we definitely need an API Gateway as a layer between the Client and the Services

Benefits of API Gateway

Image Source: https://educosta.dev/wp-content/uploads/API-Gatey-Architecture.jpg

The API Gateway can do a lot of things and let us look at the benefits below

1. Abstracting the clients from the complexity of the backend system

2. Handling Authentication and Authorization of users in a single place for the entire system

3. Transforming the requests and responses according to the format needed

4. Rate limiting for each of the APIs to prevent the load on backend services

5. Throttling the API requests made by a single user to prevent the DDOS attack

6. Reducing the load on the services by Caching the responses for the repeated requests

7. Supports different protocols

8. Metering individual APIs to make billing the clients based on usage

9. Logging and tracing the API calls into the system in a single place

10. Error handling

11. Aggregating the data across microservices and respond the data to the client on a single API call. This will avoid multiple API calls the clients need to make

Drawbacks of API Gateway

Every good thing comes with drawbacks and so is the API Gateway

  1. The API Gateway can become a single point of failure for the entire system.

2. It could cause performance issues and increase the latency of API calls if we perform too many things in the API Gateway.

3. If we have too many microservices, managing too many routes could get complicated

4. There is always an extra hop on API calls from the client to the server.

5. Extra cost because of the additional server component involved

What is Backend for FrontEnd (BFF) Pattern?

We saw in detail about API Gateway and this approach is fine if we have a single client like web or mobile. If our application is used by multiple clients like web, mobile, IoT, etc, it is not a good idea to use a single API Gateway for all types of clients. This could get bigger and could bloat up the API Gateway service by making it a single big Monolith service.

The better approach for this type of scenario is to use a separate API Gateway for each client type this architectural pattern is called as Backend for FrontEnd (BFF) pattern and it has become a buzzword.

The BFF pattern is an architectural paradigm, a variant of the API gateway pattern and it comprises of multiple back-ends that are designed to meet the demands of specific front-end applications, such as desktop, browser, and native-mobile applications, IOT devices etc.

Image Source: https://tsh.io/wp-content/uploads/2019/09/backend-for-frontend-design-pattern.png

When to use API GW vs BFF?

Well, this is an open question and it completely depends on the organization.

1. If the application is new, we would not be knowing about its usage in advance. So, initially, the API Gateway can be a single component and once the usage increases across the clients, then the APIs for clients could be moved into individual components i.e BFF

2. If the different clients need a different type of Authentication, definitely better to go for BFF rather than using a single API GW

3. If the client applications are owned by different teams, then it will make sense to use BFF else there would be a lot of interaction and overhead when we have a single API GW

In this article, we saw what is an API Gateway and saw the scenarios for Client to Microservices communication with and without the API Gateway. We also looked at the benefits and drawbacks of the API Gateway pattern. Then we looked at a pattern called Backend for Front End, which is a variant of the API Gateway pattern, and concluded the article by looking at when we need to use API GW vs BFF

Hope you enjoyed reading this and thanks for reading this!!!

If you like to get more updates from me, 
please follow me on Medium and subscribe to email alert.

If you are considering to buy a medium membership, 
please buy through my referral link

Please go through my below article if you wanted to know about Spring Cloud Gateway

Software Development
Software Engineering
API
Coding
Programming
Recommended from ReadMedium