Bugpit

Lessons › Design skills

Is that server alive?

One of your 6 servers crashes at 3 a.m. Your load balancer has no health checks, so it keeps sending that server a sixth of all requests, and every one of them fails.

Play this level

Part of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

A load balancer's first job is sharing requests out. Its second is knowing which servers are worth sending to. A health check asks each server every few seconds whether it's alive, and takes it off the list after a few missed answers. Click **Load Balancer** and tick **Health checks**.

Check something real. A check that only proves the program started will pass while the server can't reach its database. A check that tests every dependency can take all your servers out at once when one shared dependency blips. Many teams use a light check for the load balancer and deeper ones for alerts.

How requests are shared matters too. Round robin takes servers in turn, and suits requests that all cost about the same. Least connections sends each request to the server with the fewest in progress, which is better when some requests are slow.

Sticky sessions send a user to the same server every time. They're a patch for servers that keep data in memory, and they make servers hard to remove. Keep servers stateless and you don't need them.

A layer 4 load balancer forwards connections without reading them, so it's fast and simple. A layer 7 one reads each HTTP request, so it can route by path or header. This simulator's load balancer shares requests evenly and doesn't model the choice of algorithm.

What you have to achieve

8,000 requests a second at the busiest hour, 3% of them writes.

What you learn

With health checks on, a dead server is skipped and the other five carry the load. In a real system the requests sent in the few seconds before the check notices still fail, which is one more reason clients retry.

Check yourself

Some requests take 10 ms and some take 5 seconds. Which way of sharing them out suits this?

Show the answer

Least connections. Round robin can pile slow requests on one server. Least connections sends new work to whoever is least busy.

A health check only confirms that the server program is running. What can it miss?

Show the answer

A server that's up but can't reach its database. The program answers "I'm fine" while every real request fails. A useful check exercises what requests need.

Why do sticky sessions make servers hard to remove?

Show the answer

Users tied to that server lose what it held in memory. The session lived on that one machine. Keep it in a shared store and any server can take any user.

You want /images to go to one group of servers and /api to another. Which load balancer can do it?

Show the answer

Layer 7. Only layer 7 reads the request, so only it can see the path.

Try it yourself

More in Design skills

← Three copies, no leader Push or poll →