Skip to the page

Scaling and running in production · mid

Stateless servers, load balancers and horizontal scaling

To scale out, run many identical stateless API instances behind a load balancer, keep all shared state (sessions, files, caches, rate-limit counters) in external stores, and scale the database separately with replicas, caching and later partitioning.

The notes

What you must know

  • Vertical scaling: a bigger machine (simple, has a ceiling). Horizontal: more instances (needs statelessness and a load balancer).
  • Stateless means any instance can serve any request: sessions in Redis or signed tokens, files in S3, caches in Redis, jobs in a queue, no in-memory state that matters.
  • Load balancer: L7 (HTTP: path routing, TLS termination, e.g. an ALB or nginx) vs L4 (TCP); algorithms round robin or least connections; health checks remove bad instances.
  • Sticky sessions pin a user to an instance: a crutch for stateful apps and sometimes needed for WebSocket/long-polling; they make scaling and deploys uneven.
  • Database: read replicas for read-heavy traffic (replication lag means reads may be slightly stale), caching, connection poolers, then partitioning or sharding when one primary is not enough.
  • Autoscale on CPU, latency or queue depth; CDN for static assets and cacheable responses takes load away entirely.
  • Find the bottleneck first (usually the database or an external call), not the Node process.

The red pen

Common traps: mistake → fix

  • Mistake: Add more Node instances to fix any slowness

    Fix: if the database is the bottleneck, more instances make it worse (more connections).

  • Mistake: In-memory sessions work fine with a load balancer

    Fix: users get logged out when they hit another instance.

  • Mistake: Read replicas are always up to date

    Fix: replication lag can show a user stale data right after they write.

Easy to hard

Questions interviewers ask, easy to hard

  1. How would you scale this API from 1,000 to 1,000,000 users?
  2. What does 'stateless' mean and why does it matter?
  3. What does a load balancer do? L4 vs L7?
  4. A user updates their profile and the next page shows the old value. Why might that be?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview