Stop 10 · Load Balancing, Scaling & High AvailabilityTopics · 1/8

How Load Balancers Work

Part of the AWS Roadmap

In short

Once you have more than one server, someone has to decide which one gets each request, and stop sending people to the one that just crashed. That someone is the load balancer.

A load balancer spreads incoming requests across several servers, checks their health and stops sending traffic to failed ones. It gives users one stable address while servers come and go behind it.

The blob character dressed as a traffic cop waves cars towards open toll booths.

Think of it like this

The cop at the toll plaza

At a busy toll plaza, a traffic cop waves cars to whichever booth is free. If one booth's barrier is stuck, he stops sending cars there until it's fixed. A load balancer does that for requests.

Watch it happen

Health checks in action

Users
Load balancer
web-1
web-2

GET /products

1/6A request reaches the load balancer's single public address.

The big ideas

What you need to know

Target groups

The load balancer forwards to a group of targets: instances, IPs or containers. Auto scaling adds and removes them.

Health checks

A small /health endpoint that returns 200 when the app is truly ready. Unhealthy targets get no traffic.

Layer 7 routing

An application load balancer can send /api to one group and /images to another, based on path or host.

TLS at the edge

The load balancer can hold the HTTPS certificate, so your servers deal with plain HTTP inside the VPC.

See it in code

Walk through the code

hcl
1resource "aws_lb_target_group" "web" {2  name     = "web-tg"3  port     = 80804  protocol = "HTTP"5  vpc_id   = aws_vpc.main.id6 7  health_check {8    path                = "/health"9    matcher             = "200"10    interval            = 1511    healthy_threshold   = 212    unhealthy_threshold = 313  }14}

1/3Targets listen on port 8080 inside the VPC.

Terraform for a target group. Attach it to an application load balancer listener and an auto scaling group.

Save yourself the pain

Common mistakes

Oops

Pointing the health check at the home page that loads ten database queries.

Do this instead

Use a light /health route that checks the app is up and its key dependencies are reachable.

Oops

Opening app servers to the internet as well as the load balancer.

Do this instead

Let app servers accept traffic only from the load balancer's security group.

Your mission

Break one server on purpose

~45 min

Quick check

A target fails its health checks. What does the load balancer do?

Go deeper

  1. 1Explain round robin and least outstanding requests routing in simple words.
  2. 2Draw a load balancer in front of two servers in different Availability Zones.
  3. 3List what happens to user requests when one server crashes.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge