Load Balancer & API Gateway…Why do you care?

Digital Integration Samit

What is a Load Balancer

Load balancing is defined as the distribution of network or application traffic across multiple servers or services in the backend. A load balancer sits between the client application and backend servers/services.

It receives the incoming request and then distributes the incoming requests to any available server/services capable of fulfilling the request. In short – it splits the incoming request.

There are few industry-standard algorithms in splitting the traffic:

  • Round robin
  • Weighted round robin
  • Least number of connections
  • Least response time
  • Layer 7 load balancers can further distribute requests based on application-specific data such as HTTP headers, cookies, or data within the application message itself, such as the value of a specific parameter.

We generally group the Load Balancers into two categories: Layer 4 and Layer 7. Layer 4 load balancers act upon the data found in network and transport layer protocols (IP, TCP, FTP, UDP). Layer 7 load balancers distribute requests based upon data found in application layer protocols such as HTTP.

If you have worked with AWS, you know that Application Load Balancers are used to route HTTP/HTTPS (or Layer 7) traffic, whereas Network Load Balancers and Classic Load Balancers are used to route TCP (or Layer 4) traffic.

What is an API Gateway

An API gateway is an API management component. Just like the Load Balancer, an API Gateway also sits between the client application and backend servers/services.

But there are fundamental differences between a Load Balancer & an API Gateway.

An API gateway acts as a reverse proxy to accept all application programming interface (API) calls, aggregate the various services required to fulfill them, and return the appropriate result. If you have many services exposed to client applications, it is a good practice to use an API gateway to expose those services.

It’s common for API gateways to handle common tasks that are used across a system of API services, such as user authentication, rate limiting, and statistics.

Why use an API gateway?

At the fundamental level, an API accepts a request from a client application, routes it to a back-end service, gets the response from the service & responds back to the client application. However, there are multiple other requirements to handle when you work with enterprise-grade API:

  • You want to learn how people are actually using the APIs, so you need analytics and monitoring components
  • You want to protect your APIs from external malicious attack, so you use an authentication service
  • In case the back-end is fine-grained microservices architecture, API layer will break down the original (API) request into multiple service request
  • You need to limit the overall usage of API’s (because of hardware limitations or any other reasons) and rate limiting.
  • If you want to monetize API calls, you’ll like to connect to a billing system.
  • You would like to isolate your service changes in the backend from the remote client applications (for example – for new customers – you would like to have new version of API’s, old customers continue to use legacy version of API’s)

Thus, an API gateway is a way to decouple the client view of the services from your backend implementation. When a client makes a request, the API gateway applies the security policies, routing policies, breaks the incoming request into multiple requests, routes them to the right places, produces a response, and keeps a track of everything happening.

The difference in Load Balancer & API Gateway

So, you see the fundamental difference in a Load balancer & and an API gateway:

Load balancer distributes the incoming traffic to multiple back-end services – typically these services/servers are different instances of the same server/service

API gateway encapsulates the complexity of backend services, decomposes the incoming request into multiple service request and controls the routing of the requests.

There are instances when Load Balancer & API Gateway are used together, as below:

Hence, both Load balancer & API Gateway split incoming request but the intent of splitting the requests are different. Load balancer splits it to distribute the load of traffic, API gateway splits it to route requests to the right service.