How does request routing work for AWS Application Load Balancer?
AWS Application Load Balancer (ALB) is a type of load balancer that functions at the application layer i.e. Layer 7 of the OSI model. An ALB provides advanced load balancing of HTTP & HTTPS traffic by routing requests based on the content of the request.
In this article, we will take a closer look at the various routing features provided by the ALB.
A load balancer is the point of contact for clients. The load balancer distributes incoming requests across multiple targets, such as EC2 instances or ECS containers.
One or more listeners need to be added before an ALB can serve traffic. A listener checks for the connection requests using the protocol and port. Listeners support the following protocols and ports:
- Protocols: HTTP, HTTPS
- Ports: 1-65535
Each listener has a default and you can add additional rules at any time. Each rule consists of a priority, one or more actions, and one or more conditions.
When you create a listener, you define actions for the default rule,. Default rules can’t have any conditions. If the conditions for none of a listener’s rules are met, then the default rule is triggered.
A target group is used to route requests to one or more registered targets. While creating a listener rule, a target group and conditions need to be specified. When a rule condition is met, traffic is forwarded to the corresponding target group. Different target groups can be created for different type of requests.
The following are possible as target types:
instance: The targets are specified by instance ID
ip: The targets are IP addresses
lambda: The target is a lambda function
Check out the AWS documentation for more details about Target Groups.
The ALB periodically sends requests to registered targets to test their status. Targets that respond successfully to these requests are marked as healthy. The ALB only routes real requests to healthy targets.
ALB allows multiple services to be hosted behind a single load balancer. ALB can route to each service based on the path of the request as shown in the example below.
Host-based routing feature allows you to write rules that use the
Host header to route traffic to the desired target group.
In addition to the
Host header, ALBs also provide the ability to write rules based on any other HTTP headers, query string and the source IP address.
An example of these rules look something like this:
This article provides a detailed look into all the advanced routing features provided by ALB.
ALBs support redirects natively as an action while setting up a listener. This action can be used for the following use-cases:
- HTTP to HTTPS redirects: Disallowing any insecure traffic to your application
- HTTPS to HTTPS: Redirecting to a different listener port or redirecting to a different target URL
ALB can auto respond to HTTP requests based on any criteria and rules supported by the content-based routing rules. This action can be used to return a 2XX, 4XX or 5xx response code and an optional message.