Skip to content
We areTOP 1%

What is a load balancer, and how do round robin, least connections and IP hash work?

MediumConceptTLS, Firewalls & Load Balancers
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

A load balancer sits in front of a group of servers and spreads incoming requests across them so that no single server is overloaded. It improves scalability, because servers can be added as traffic grows, and availability, because health checks stop sending users to a failed server.

  • Round robin sends requests to servers in turn (1, 2, 3, 1, 2, 3). It is simple and works well when servers and requests are similar; weighted round robin gives stronger servers a larger share.
  • Least connections sends each new request to the server with the fewest active connections, which suits requests of very different lengths, such as uploads mixed with quick page loads.
  • IP hash picks the server from a hash of the client's IP address, so a client keeps reaching the same server, which helps when sessions are stored on that server.
  • A layer-4 balancer routes by IP address and port only; a layer-7 balancer reads HTTP and can route /api and /images to different server groups.

Example: Three servers hold 10, 4 and 7 connections. Round robin sends the next request to whichever server is next in turn, even the busy one; least connections picks server 2 (4 connections); IP hash computes, say, hash(203.0.113.25) mod 3 = 1 and always sends that client to server 2 (counting from 0).

More in TLS, Firewalls & Load Balancers

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account