Grokking the System Design Interview

System Design Interview: Difference Between API Gateway and Load Balancer

Arslan Ahmad
Level Up Coding
Published in
7 min readDec 9, 2022

--

Often, we come across software architectural components that are part of every system design and feel as though we don’t have much understanding of them. API Gateway and Load Balancer are a couple of examples that come to mind.

Most people don’t have experience working with a load balancer or an API Gateway; this explains the reason that we feel uncomfortable talking about them, especially in a system design interview.

In this post, let me explain the basic knowledge about these two components and how they are used.

Let's start with the definition of related terms.

What are micrososervices?

Microservices are a software architecture design pattern in which a large application is built as a suite of modular components or services. Each microservice is a small, independent unit of functionality that communicates with other microservices using well-defined interfaces, typically over a network. For example, while building a service like Instagram, we can have separate microservice for storing photos, generating newsfeeds, or notifying users.

What is an API gateway?

An API gateway is a server that acts as a single point of entry for a set of microservices. It receives client requests, forwards them to the appropriate microservice, and then returns the server’s response to the client.

API Gateway

What is a load balancer?

A load balancer is a network device that distributes incoming network traffic across multiple backend servers or services to improve the performance and availability of the system. The load balancer typically sits between the client and the server and uses various algorithms to distribute incoming requests among the available servers in a manner that maximizes performance and ensures that no single server is overwhelmed. This can improve the overall reliability and responsiveness of the system, as it allows for a more even distribution of workload and enables the system to handle a higher volume of requests.

Difference between an API gateway and a load balancer?

An API gateway is focused on 𝗿𝗼𝘂𝘁𝗶𝗻𝗴 requests to the appropriate microservice, while a load balancer is focused on 𝗱𝗶𝘀𝘁𝗿𝗶𝗯𝘂𝘁𝗶𝗻𝗴 requests evenly across a group of backend servers.

An API gateway and a load balancer are both types of infrastructure that can be used in a computer network to manage incoming requests and enhance the performance of a system. However, they work in different ways and serve different purposes.

API Gateway vs. Load Balancer

API gateway: An API gateway is a type of middleware that sits between a client and a collection of microservices. Its main purpose is to route requests from clients to the appropriate microservice and then to return the response from the microservice back to the client. An API gateway can also perform other tasks such as authorization, rate limiting, and caching.

Load balancer: A load balancer, on the other hand, is a type of infrastructure that distributes incoming requests evenly across a group of backend servers in order to improve the performance and availability of a system. Load balancers are typically used to handle requests that are sent to a single, well-known IP address, and then route them to one of many possible backend servers based on factors such as server performance and availability.

Another difference between the two is the type of requests that they typically handle. An API gateway is typically used to handle requests for APIs, which are web-based interfaces that allow applications to interact with each other over the internet. These requests typically have a specific URL that identifies the API that the client is trying to access, and the API gateway routes the request to the appropriate microservice based on this URL. A load balancer, on the other hand, is typically used to handle requests that are sent to a single, well-known IP address, and then routes them to one of many possible backend servers based on factors such as server performance and availability.

Usage of API gateway

API gateways are used for a variety of purposes in microservice architectures, including the following:

  1. Routing: The API gateway receives requests from clients and routes them to the appropriate microservice. This enables clients to access the various microservices through a single entry point, simplifying the overall system design.
  2. Rate limiting: You can rate limit client access to microservices with an API gateway. This can help prevent denial of service attacks and other types of malicious behavior.
  3. Caching: The API gateway can cache responses from the microservices, reducing the number of requests that need to be forwarded to the microservices and improving the overall performance of the system.
  4. Authentication and Authorization: The API gateway can be used to authenticate clients and enforce access control policies for the microservices. This helps to ensure that only authorized clients can access the microservices and helps to prevent unauthorized access.
  5. Load balancing: The API gateway can distribute incoming requests among multiple instances of a microservice, enabling the system to handle a larger number of requests and improving its overall performance and scalability.
  6. Monitoring: The API gateway can collect metrics and other data about requests and responses, providing valuable insights into the performance and behavior of the microservices. This can help to identify and diagnose problems, and improve the overall reliability and resilience of the system.
  7. Transformation: The API gateway can be used to transform the data received from the microservices into a format that is more convenient for the clients to use. This can include tasks such as converting between different data formats, such as XML and JSON, or aggregating data from multiple microservices into a single response.
  8. Request and response validation: The API gateway can be used to validate the requests and responses from the microservices to ensure that they conform to the expected format and structure. This can help to prevent errors and ensure that the microservices are functioning properly.
  9. Circuit breaker: The API gateway can be used to implement a circuit breaker pattern, which can help to prevent a single failed microservice from bringing down the entire system. The circuit breaker can monitor the health of the microservices and automatically fail over to a backup service if necessary.
  10. Service discovery: The API gateway can be used to discover the available microservices and their locations, enabling the clients to access them without knowing their specific addresses. This can make it easier to add new microservices or make changes to the existing ones without impacting the clients.

Advantages of using API Gateway

Here are some of the advantages of using API Gateway:

  1. Improved performance: By handling tasks such as routing and load balancing, the API gateway can improve the overall performance of the system, enabling it to handle a larger number of requests and respond more quickly to the clients.
  2. Simplified system design: By providing a single entry point for the clients, the API gateway simplifies the overall system design and makes it easier for the clients to access the various microservices.
  3. Enhanced security: The API gateway can be used to enforce authentication and access control policies, helping to prevent unauthorized access and improve the security of the system.
  4. Improved scalability: The API gateway can distribute incoming requests among multiple instances of a microservice, enabling the system to scale more easily and handle a larger number of requests.
  5. Better monitoring and visibility: The API gateway can collect metrics and other data about the requests and responses, providing valuable insights into the performance and behavior of the system. This can help to identify and diagnose problems, and improve the overall reliability and resilience of the system.

Disadvantages of using API Gateway

There are also some potential disadvantages to using an API gateway, including:

  1. Increased complexity: The use of an API gateway can add an extra layer of complexity to the system, which can make it more difficult to manage and maintain.
  2. Performance overhead: The API gateway can introduce some performance overhead, as it adds an additional layer to the request-response path that the clients must traverse.
  3. Single point of failure: The API gateway can become a single point of failure if it is not designed and implemented properly. This can impact the overall reliability and availability of the system.

Conclusion

In summary, an API gateway provides an interface between the client and the microservices, while a load balancer distributes incoming requests among multiple servers or services to improve performance and availability. While they serve similar purposes, they are not interchangeable and are often used together in a microservice architecture. They both enable the microservices to focus on their individual tasks and improve the overall performance, scalability, and reliability of the system.

➡ Check Grokking System Design Fundamentals for a list of common system design concepts.

➡ Learn more on system design interviews in “Grokking the System Design InterviewandGrokking the Advanced System Design Interview.”

➡ Follow me on Linkedin for tips on system design and coding interviews.

--

--

Founder www.designgurus.io | Formally a software engineer @ Facebook, Microsoft, Hulu, Formulatrix | Entrepreneur, Software Engineer, Writer.