Skip to the content

Lesson 233 of 296

Load balancers, scaling and stateless services

What you will learn

  • Vertical vs horizontal scaling
  • What a load balancer does
  • Load balancing algorithms
  • Layer 4 vs layer 7
  • Health checks
  • Stateless services and where state goes
  • Auto scaling
On this page

Vertical vs horizontal scaling

Vertical (scale up)Horizontal (scale out)
HowA bigger machine: more CPU, RAMMore machines sharing the work
ProsSimple; no code changesAlmost unlimited; survives single machine failures
ConsHard upper limit; expensive; still a single point of failureNeeds load balancing and stateless servers

Start vertical (it is simple and modern machines are big); design so you can scale horizontally.

Load balancers

A load balancer (LB) receives incoming traffic and distributes it across several servers. Benefits:

  • Scalability: add servers behind it.
  • Availability: it stops sending traffic to unhealthy servers.
  • Single entry point: TLS termination, routing, security rules in one place.

Examples: AWS Application Load Balancer and Network Load Balancer, nginx (Lesson 248), HAProxy, Envoy, Cloudflare. The load balancer itself must not be a single point of failure: managed cloud load balancers are redundant across availability zones.

Algorithms

AlgorithmHowGood for
Round robinEach server in turnSimilar servers, similar requests
Weighted round robinMore requests to bigger serversMixed server sizes
Least connectionsServer with the fewest active connectionsRequests of very different lengths, WebSockets
Least response timeFastest-responding serverMixed performance
IP hash / consistent hashingSame client or key → same serverStickiness, caches (Lesson 235)

Layer 4 vs layer 7

Layer 4 (transport)Layer 7 (application)
SeesIP addresses and ports (TCP/UDP)Full HTTP: URL, headers, cookies
Can doFast forwarding of connectionsRoute /api to one service and /images to another, TLS termination, header rules, rate limits
ExampleAWS NLBAWS ALB, nginx

Health checks

The load balancer regularly calls each server (for example GET /readyz, Lesson 193). Servers that fail are removed from rotation until they recover. This is how deployments and crashes avoid user-facing errors.

Stateless services

A server is stateless when it keeps no user data in its own memory between requests. Any request can go to any server. This is what makes horizontal scaling and safe restarts possible.

Where state goes instead:

StateStore it in
SessionsRedis or the database (Lesson 200), or a signed token
Uploaded filesObject storage (S3), not the server's disk
Caches shared by all serversRedis
Background workA queue (Lesson 236)

Sticky sessions (the LB always sends a user to the same server) are a workaround for stateful servers; they make scaling and failover worse, so avoid them unless required (some WebSocket setups).

Auto scaling

The platform adds servers when load rises (CPU above 60%, queue length, requests per target) and removes them when it falls: AWS Auto Scaling groups, ECS service scaling, Kubernetes Horizontal Pod Autoscaler. Requirements: stateless servers, fast start-up, graceful shutdown, and headroom, because new servers take time to start.

How to say it in an interview

"I start with vertical scaling for simplicity but design for horizontal scaling, which needs stateless application servers: sessions in Redis or tokens, files in object storage, shared caches in Redis and background work in queues. A load balancer spreads traffic with round robin or least connections, removes unhealthy servers using readiness checks, and at layer 7 can route by path and terminate TLS. With stateless servers I can auto scale on CPU or request metrics and deploy without downtime, and I avoid sticky sessions unless a protocol requires them."

Key words

WordMeaning
Vertical scalingUsing a bigger machine
Horizontal scalingUsing more machines
Load balancerDistributes traffic across servers
Round robin / least connectionsLoad balancing algorithms
Layer 4 / layer 7Transport-level / HTTP-level load balancing
Stateless serviceKeeps no user state in memory between requests
Sticky sessionA user always routed to the same server
Auto scalingAutomatically adding and removing servers

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.