Ankit KanojiyaBeginner

Evergreen guide by Ankit Kanojiya · Beginner · System Design

Scaling a Web App: From One Server to Millions of Users

Learn how a single-server app grows into a system that serves millions, one bottleneck at a time. Covers vertical vs horizontal scaling, statelessness, databases, caching, and queues.

Most systems do not start big. They start as one process on one machine and grow only when traffic forces the issue. Understanding the usual sequence of bottlenecks makes scaling feel like a series of small, defensible decisions instead of a rewrite.

Stage 0: one box does everything

The first version runs the web server, application code, and database on a single machine. This is correct for early traffic, and premature scaling here wastes time. The limits show up as high CPU, exhausted memory, or disk I/O saturation.

Vertical vs horizontal scaling

  • Vertical scaling means buying a bigger machine: more CPU, RAM, faster disks. It is simple and requires no code changes, but there is a ceiling and a single point of failure.
  • Horizontal scaling means adding more machines behind a load balancer. There is no hard ceiling, but the application must be designed for it.

Vertical scaling buys time. Horizontal scaling buys longevity.

Split the tiers

The first real architectural move is separating concerns onto different machines:

  • Application servers run your code.
  • A database server owns persistent state.
  • A load balancer distributes requests across app servers.

This lets each tier scale on its own schedule.

Make app servers stateless

Horizontal scaling only works if any server can handle any request. That requires removing local state. The common offender is session data stored in server memory.

Move session state out of the process:

Browser --cookie--> Load Balancer --> App Server (any)
                                          |
                                     Redis (sessions)

With sessions in a shared store, you can add or remove app servers freely, and a crash does not log everyone out.

The database becomes the bottleneck

Stateless app servers scale easily, but they all point at one database. Reads usually dominate, so attack reads first:

  • Add a cache (such as Redis or Memcached) in front of expensive queries.
  • Add read replicas so reads spread across copies while writes go to the primary.
-- writes go to primary
UPDATE accounts SET balance = balance - 10 WHERE id = 42;

-- reads can go to a replica
SELECT balance FROM accounts WHERE id = 42;

Replication introduces lag: a read replica may be milliseconds to seconds behind. For a balance shown after a transfer, read from the primary or the cache instead.

Caching layers

Caching appears at several levels, and each removes load from the one below it:

  • Browser and client caches avoid requests entirely.
  • A CDN serves static assets and some cacheable responses close to users.
  • An application cache stores query results and computed values.

A cache hit that avoids a database round trip is often the single highest-leverage optimization available.

Offload slow work with queues

Some work does not need to finish before responding to the user: sending email, generating thumbnails, updating search indexes. Pushing these onto a message queue keeps request latency low.

Request --> enqueue job --> respond 202 Accepted
                 |
            Worker pool --> processes job later

This decouples request handling from background processing, and the worker pool scales independently of the web tier.

Scale the database writes

Read replicas and caches handle reads, but a single primary still absorbs every write. When writes saturate it, options include:

  • Sharding: partition data across multiple databases by a key such as user ID.
  • Specialized stores: move high-volume data (time series, search, blobs) to systems built for it.

Sharding is powerful but costly in complexity, so it is usually a late move.

Measure before you move

Each stage above is a response to a real, observed bottleneck. Adding replicas when the CPU is pegged on the app tier solves nothing. Invest in observability early:

  • Request latency percentiles (p50, p95, p99), not just averages.
  • Database query times and slow query logs.
  • Cache hit rates and queue depth.

A practical progression

  1. One server. Ship the product.
  2. Split the database onto its own machine.
  3. Add a load balancer and a second app server; make servers stateless.
  4. Add a cache for hot reads.
  5. Add read replicas.
  6. Introduce a queue for background work.
  7. Add a CDN for static and cacheable content.
  8. Shard or adopt specialized stores when writes demand it.

Millions of users is rarely one heroic design. It is this ordered list, each step justified by a metric, with complexity added only when the previous stage runs out of room.