Load Balancing and Horizontal Scaling
Core Concepts (5)
- 1.Vertical scaling makes one machine bigger; horizontal adds machines
- 2.A load balancer spreads requests across servers
- 3.Health checks remove broken servers automatically
- 4.Stateless servers make horizontal scaling easy
- 5.The load balancer itself must not be a single point of failure
Map to Your Experience
Think of a checkout line at a supermarket with one cashier and a long queue. What would you change, and what new problems would that create?
Design Brief: Scale a Web Tier
Learn
When one server cannot handle the load, there are two ways to grow. Vertical scaling means buying a bigger machine. It is simple, but it has a ceiling and one failure takes everything down. Horizontal scaling means running several machines side by side. It has no hard ceiling and tolerates failures, but it requires splitting the work between them.
A load balancer does that splitting. It sits in front of the servers, accepts every request, and forwards each one to a server chosen by a rule. Round robin hands requests out in turn. Least connections picks the server with the fewest active requests. Hashing the client address or a header sends the same client to the same server.
Health checks make the system self-healing. The load balancer regularly asks each server whether it is alive, typically through a small endpoint that returns success. A server that fails several checks in a row is removed from rotation, and it is added back once it recovers. Users never see the failed machine.
Horizontal scaling works best when servers are stateless. If a login session lives in one server's memory, the next request landing on another server will not recognise the user. Two fixes are common: store sessions in a shared place such as a database or cache, or put the session in a signed token that the client carries.
Load balancers work at different layers. A layer 4 balancer routes by network connection and is very fast. A layer 7 balancer understands HTTP and can route by path or header, terminate TLS, and add features such as rate limiting. The extra intelligence costs a little latency.
Remember that the balancer is now a critical component. Run it as a redundant pair or use a managed cloud service, otherwise you have swapped one single point of failure for another. Also plan capacity so that when one server is lost, the remainder can absorb its share of traffic.
Map
A fleet of four servers each handles 300 requests per second at most. Peak traffic is 900. Work out how many servers can fail while still serving peak, and decide whether four is enough.
Design
A web app runs on one server with sessions in memory and sees 600 requests per second at peak. Redesign it as a fleet behind a load balancer. Draw the request path, pick a balancing rule, define the health check, move session state somewhere safe, and size the fleet so that one server can fail at peak without errors.
Requirements
- •Show the new request path
- •Define a health check
- •Explain how sessions are handled
Constraints
- •No downtime during a server failure
- •Sessions currently live in server memory
Must Cover
Trade-off Axis
simplicity vs availability
Referenced Patterns
Competencies Advanced
Ready to Design?
Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.
Open in Studio →