• Technicalpig
  • Posts
  • TechnicalPig🐷: API Gateway vs Application Load Balancer

TechnicalPig🐷: API Gateway vs Application Load Balancer

When to use them and how they work

What is an API Gateway?

An API Gateway acts as a front-door interface to manage all API calls between clients and backend services. It's essentially a traffic cop that ensures only authorised, well-formed requests reach your back-end services.

Beyond routing, API Gateways offer functionalities including but not limited to authentication, rate limiting, request and response transformation, and API version management.

Key Functions:

  • Request Routing: Directs incoming API calls to the appropriate services.

  • Authentication and Authorisation: Validates API keys, JWT tokens, and other credentials before allowing access to backend services.

  • API Metering and Rate Limiting: Monitors and controls the number of requests a user can send to prevent abuse.

  • Data Transformation: Converts data formats (e.g., from XML to JSON) as needed between client and server.

  • API Version Management: Facilitates the management of different API versions to ensure backward compatibility.

What is an Application Load Balancer (ALB)?

An ALB, a specific type of load balancer, operates at the application layer (Layer 7) of the OSI model. It intelligently distributes incoming application traffic across multiple targets, such as EC2 instances, containers, and IP addresses, in multiple Availability Zones.

ALBs are adept at handling HTTP, HTTPS traffic, offering features like host- and path-based routing, SSL/TLS termination, and sticky sessions.

Key Functions:

  • Load Distribution: Evenly distributes incoming traffic among backend targets to optimise resource use and maximise application responsiveness.

  • Health Checks: Continuously checks the health of the underlying servers to ensure requests are only routed to healthy instances.

  • Host and Path-Based Routing: Directs traffic based on the URL path or domain name to different backend services.

  • SSL/TLS Termination: Decrypts incoming SSL/TLS sessions at the load balancer, reducing the encryption overhead on backend servers.

Differences and Use Cases

While both API Gateways and ALBs manage network traffic, their primary distinction lies in their scope and functionality. API Gateways are specifically designed for to manage access to backend services. In contrast, ALBs are focused on distributing web traffic across servers.

Use Cases for API Gateways:

  • Microservices architecture, where different services require a unified entry point.

  • Applications requiring detailed API analytics, user authentication, and access control.

  • Situations needing API version control and endpoint documentation.

Use Cases for ALBs:

  • Web applications needing efficient load distribution to handle varying traffic volumes.

  • Environments requiring sophisticated routing based on request content (path or host-based routing).

  • Applications that benefit from offloading SSL processing to improve backend performance.

Combining API Gateways and ALBs

There are scenarios where using both API Gateway and ALB together makes sense, such as:

  • Using API Gateway at the edge to handle API management tasks (throttling, auth, etc.) and routing specific requests to an ALB for further distribution among microservices or server-based applications.

  • Leveraging ALB for internal load balancing and traffic management within a VPC, while exposing APIs to external clients through API Gateway.

Conclusion

Ultimately, the choice between API Gateway and ALB should be driven by your application's specific requirements, architectural design, and the particular features and capabilities that each AWS service provides.