Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 3 OF 5 · 2 MIN READ

Load Balancing and Horizontal Scaling

Scaling a backend means handling more traffic by making one machine bigger (vertical) or adding more machines (horizontal). Horizontal scaling needs a load balancer and stateless app servers, which is why these topics come together in interviews.

What you will learn

  • Compare vertical and horizontal scaling
  • Know load-balancing algorithms and L4 vs L7
  • Make the app stateless
  • Explain autoscaling and health checks

Key concepts

L4 vs L7 load balancer

L4 balances TCP connections by IP and port, fast and simple. L7 understands HTTP, so it can route by path or header, terminate TLS, retry and add headers.

Algorithms

Round robin, least connections, weighted, and consistent hashing (the same key goes to the same server, useful for caches).

Stateless servers

No session, upload or cache lives only in one server's memory or disk. State goes to Redis, the database or object storage, so any instance can serve any request and instances can come and go.

Autoscaling

Add or remove instances based on CPU, request rate or queue depth. Needs fast startup, readiness checks and graceful shutdown.

Step by step

Scaling a growing app

  • 1 server → add a CDN for static files
  • Move the database to its own machine; add connection pooling
  • Add Redis for cache and sessions; make the app stateless
  • Run several app instances behind a load balancer with autoscaling
  • Add read replicas; move slow work to queues
  • Shard or split services only when one database or team becomes the limit

Examples

Nginx as an L7 load balancer

upstream api {
  least_conn;
  server 10.0.1.10:3000 max_fails=3 fail_timeout=10s;
  server 10.0.1.11:3000 max_fails=3 fail_timeout=10s;
  keepalive 64;
}
server {
  listen 443 ssl;
  location /api/ {
    proxy_pass http://api;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Request-Id $request_id;
  }
}

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • List every piece of state in a typical Express app that breaks with two instances.
  • Explain sticky sessions and why they are a last resort.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

What is consistent hashing?

Servers and keys are placed on a hash ring, and each key goes to the next server on the ring. Adding or removing a server moves only the keys near it instead of reshuffling almost all of them, which matters for caches and sharded stores.

What is a single point of failure and how do you remove it?

Any component whose failure takes the whole system down: one load balancer, one database primary. Remove it with redundancy (two load balancers, replicas with automatic failover) across availability zones.

Summary

Scale out with stateless app servers behind an L7 load balancer with health checks and autoscaling, keep state in shared stores, and remove single points of failure.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus