AWS Bites Podcast

127. Load Balancers

An overview of load balancers, explaining how they distribute traffic across multiple servers and provide high availability. We discuss layer 4 and layer 7 load balancers, detailing their pros and cons. We then focus on AWS load balancers, covering network load balancers and application load balancers in depth, including their features, use cases, and pricing models. We conclude by mentioning some alternatives to AWS load balancers.

AWS Bites is brought to you by fourTheorem an AWS consulting partner with tons of experience with AWS. If you need someone to help you with your ambitious AWS projects, check out fourtheorem.com!

In this episode, we mentioned the following resource:

Let's talk!

Do you agree with our opinions? Do you have interesting AWS questions you'd like us to chat about? Leave a comment on YouTube or connect with us on:

Eoin
00:10:54

Yeah, if you're looking for some more control at the layer 7 layer, then you'll need a layer 7 load balancer, because they can see the traffic at that HTTP or HTTPS level. That means you can see the headers, the path, query parameters, and even the body. Doing this, as you said, Luciano, it's going to introduce some latency, but with that, you'll get a lot more useful features. And the list is a bit longer, like you can get routing based on header, path, and query. You can even modify all of those things before you pass the request to the backend. You can do HTTP compression in your load balancer, so you could do gzip encoding at the load balancer level. You can do much finer-grained specific HTTP health checks, like checking the health of a specific URL and inspecting the response in detail, like the status code and the body or response headers. You can even do caching in the load balancer. Another thing that is quite common in load balancers is to do authentication and authorization at the load balancer level. Like layer 7 load balancers can also almost be used as a very effective API gateway. When we're talking about security, we mentioned things like DDoS protection, but when you're operating at layer 7, you can also have a web application firewall linked to your load balancer so that you can do lots of more specific HTTP, like kind of OASP top 10 type attack detection. Because you can see headers, you can see cookies, and therefore you can also do cookie-based stickiness in layer 7 load balancers. And you don't need that proxy protocol thing that was added for network load balancers or layer 4 load balancers, Luciano, because the X-forwarded for header is very typical with load balancers and reverse proxies. Whereas layer 4 load balancers normally have one connection inbound associated with another connection to the backend, layer 7 typically have a different model where they terminate a connection and then forward traffic to like a pool of connections to the backend server. So it can be more efficient in terms of connection utilization as well. So that's layer 4, layer 7 load balancers. Where does this all fit into the world of AWS? We mentioned that there are four types. We're going to talk about two, the network load balancer and the application load balancer in AWS. Yes.

00:14:29

If you're building something internally, maybe this is an internal API or internal microservices, if you want, you can also use all the same features of both NLB and ALBs just for internal traffic. So traffic that is only existing inside your own private VPC. So let's talk a little bit more about network load balancers. As we said, they support layer 4 and these are great candidates if you don't need all the features of layer 7. And these are generally the way to go because the latency is so much lower. So if you need to provide as low a latency as possible, you probably want to use a network load balancer. They have some support for TLS termination and you can use AWS certificate manager, but still that termination is already done at the TLS level. It doesn't really understand, for instance, HTTPS. So it only unwraps the TLS traffic. Another thing that is interesting is that because you are using certificate manager and it can use SNI, it can support multiple domains. So that's another great feature to have for NLBs. So one more reason to consider a network load balancer is that a network load balancer will give you a fixed public IP address. And either it will provision one for you or you can use an existing Elastic IP that you have pre-allocated before. And this is great because sometimes you might need that for specific reasons depending on your network needs. Instead, if you compare that with an application load balancer, an application load balancer will only give you a CNAME and that you can then configure, for instance, if you need to forward traffic from somewhere else, from like a DNS, you can point it to that particular CNAME. But then behind the scene, the IP associated might change over time. So you can not rely on that IP being fixed. So if you really need a fixed IP, don't use application load balancer, but use network load balancers. Now, what are some of the features of application load balancer and why it might be worth considering those?

00:16:57

You can't do that with a network load balancer. And they have authorizers built in. So you can use an OADC provider, use an OADC authorizer, and you can also use Cognito user pool authorization. They also integrate pretty well with ECS, like through ECS deployments. Containers are automatically added as targets to your load balancer and ECS will watch your health checks and make sure that the deployment is successful. And then you can integrate into AWS web application firewall or WAF to get your security rules applied, like IP address blocking or preventing SQL injection or cross site scripting attacks. Now, if we look at NLDs in terms of how you create them at ALBs, it's actually very similar between the two. There's just a few subtle differences, even though the feature set is quite different. So let's just talk through the concepts and how you kind of construct an architecture around load balancers, whether you're using an NLD or an ALB. So you create your load balancer first. And once you have a load balancer, it doesn't do anything on its own. You have to create a listener. And a listener is where you'll specify the port and the protocol. And if you want to use TLS termination on your load balancer, you can specify your certificate as well, linking it into ECM. Now within the listener, you can specify your default action and an action can either provide a fixed response like a 404 or forward traffic to a target group. And target groups are really one of the most important concepts when it comes to load balancers, because this is where you route traffic from the load balancer to the backend. Target groups represent your fleet of backend things. Now it differs a little bit at this stage if you're using an application load balancer, because you don't just specify the default action, you can also specify additional listener rules. Each rule will have its own target group. And the listener rules can say, based on the path being slash login, go to this target group. Or if I've got a HTTP header, which matches this value, then go to a different target group. And all of these rules are prioritized, like you give each one a unique priority number value, and they're evaluated in that order. If no rules match at all, the default action will apply. So your default action might be to serve index.html, or it might be serving a 404. And you can have up to 100 rules in these application load balancer listeners as well by default, but it's only a soft limit. So you can get more if you ask nicely. And go back to the target groups, whether you're using an ALB or an NLB, these are groups of targets, as the name suggests, but the targets can be IP addresses. And that IP address doesn't even have to be on AWS, it could also be on premises. And a lot of people use that as a gateway drug, if you'll excuse the pun, to cloud migration. It's like using a load balancer with an on-premises server as the backend, and you can shift the traffic over to cloud instances over time. You can also have application load balancers that are targets of load balancer listener rules. So that means that you can have a hierarchy of load balancers. And it's not uncommon to do that. A lot of people will have that kind of architecture where they find out traffic across multiple load balancers. You might have like a network load balancer in front of an ALB, because you want a fixed IP address, for example. And that's not something that's uncommon at all. It's pretty common. For application load balancers, we mentioned that you can have Lambda functions as targets. At scale, actually, application load balancers can be a much more cost-effective alternative to API gateway in front of Lambda, albeit with a reduced feature set, because you don't have things like caching, rate limiting, and API keys, and various other features. But the target group is also the place where you configure health checks, which will make a request to the targets to determine whether they should be added to or removed from the target group based on their health. And you can specify the frequency, the number of target group health checks that have to succeed in order for a target to be added to your pool. So Luciano, I think we covered quite a lot. Although there's probably quite a lot else we could cover. Is there anything else we need to know?

00:23:01

My caveat there would be that I think it's significantly complex to do that correctly and to make it scalable and secure. So do that only if you really have the skills and you are really knowledgeable about these tools and all the relevant networking configuration. The pricing is simpler though. Yeah, that's actually one reason maybe to consider that if you don't account for the amount of hours that you will be spending configuring all of that. But yeah, another option could be using DNS load balancing, which has interesting trade-offs. For instance, one thing you could do, you could do geographical distribution of traffic. Maybe you can distribute traffic across regions using DNS load balancing, but also you know that DNS is always the reason for problems in the web. So there is significant complexity there to do it right, to do DNS caching and validation and all that kind of stuff. So again, it's an option, but comes with its own trade-offs. And another option that we have been discussing before is VPC lattice. And we have an entire episode and a blog post. So if you haven't seen that, we'll leave the link in the show notes so you can check those out. So hopefully this was a complete enough coverage of load balancers and how do you use load balancers on AWS. I'm sure that there is a lot we have missed. So let us know if you have any particular experience that might be worth sharing. What kind of load balancers do you like to use? Or maybe any other comments of question that you think is relevant here. So that's all for today. Thank you for being with us. And as always, if you liked this episode, consider sharing it and giving us a like. See you in the next one.

Help us to make this transcription better! If you find an error, please submit a PR with your corrections.