# How to Scale a Web Application: Stateless Servers to CDN

> How to scale a web application step by step: stateless copies behind a load balancer, sessions in a shared store, then a CDN and a cache to absorb reads.

URL: https://www.matthewswong.com/en/learn/system-design/how-to-scale-a-web-application/

System design, step by step · Lesson 2 of 11 · Reviewed on 13 October 2026 · By Matthews Wong (https://www.matthewswong.com/en/about/)

> Scaling a web application means adding capacity where it is cheapest first: make the app servers stateless so identical copies can run behind a load balancer, move sessions and other state into a shared store, then let a CDN and a cache answer repeated reads, so the origin and the database see only a fraction of the traffic.

## You will be able to

- Explain what makes a server stateless, and why sticky sessions only hide the problem.
- Trace a request through a load balancer, a session store, a CDN and a cache, and say which layer answers it.
- Decide what a shared cache may store, and use versioned URLs so nothing needs purging.

## One server to a stateless tier, step by step

A second app copy behind nginx loses sessions kept in memory; moving them to Redis makes every copy stateless, a CDN answers the repeated public requests, Redis caches hot reads, and private pages always reach the origin.

1. **Meet the parts.** Today one app server does everything: it renders pages, keeps each login in its own memory and reads the database. Dashed boxes are what scaling will add.
2. **Add a second copy.** Traffic grows, so a second copy joins behind nginx, which now deals requests in turn. Ana logs in on app 1, and app 1 keeps her session in memory.

   *Predict:* Ana's next request goes to app 2. What does app 2 see? (Ana, still logged in / A stranger with no session)
3. **The session is lost.** Round robin sends her next request to app 2, which has never seen her session. To app 2 she is a stranger: logged out, cart empty.
4. **Move sessions out.** Move sessions to a shared store with an expiry, such as Redis. Any copy can now load Ana's session by id, so any copy can serve any request.

   ```
   SET session:ana '...' EX 1800
   ```
5. **A CDN takes the repeats.** Most requests are identical for every visitor: images, scripts, public pages. A CDN edge near Ana keeps copies and answers repeats itself; the origin never sees them.

   ```
   Cache-Control: public, max-age=31536000, immutable
   ```
6. **A cache takes hot reads.** Reads that reach the app repeat too. Redis can keep hot results beside the sessions, so the database serves only cache misses and writes.

   ```
   GET post:42
   ```

   *Predict:* Ana opens her own cart page. Who should answer it? (The CDN's shared copy / The origin app, every time)
7. **Private pages pass through.** Her cart holds her data, so no shared cache may store it. The response says private, no-store, and the CDN sends every cart request to the origin.

   ```
   Cache-Control: private, no-store
   ```

## What makes a web server stateless, and why does scaling need it?

There are two ways to add capacity. Scaling up gives one machine more CPU and memory, and your code never notices. Scaling out adds more machines, and suddenly every request may land on a different process. Start with up because it needs no code change, and move out when you must survive a machine failure or the biggest machine is no longer enough.

Scaling out only works if the app is stateless: any copy can serve any request, because nothing a user needs lives in one process. The Twelve-Factor App states it as processes that are stateless and share-nothing, with anything that must persist kept in a stateful backing service such as a database. The usual hidden state is sessions in memory, counters that must agree across copies, files written to local disk, and timers inside the app, which fire once per copy instead of once.

## How does a load balancer spread requests, and why not sticky sessions?

A load balancer sits in front of the copies and picks one for each request. nginx defaults to round robin, dealing requests in turn, and it runs passive health checks: a server whose response fails is marked failed and avoided for a while, set by max_fails and fail_timeout. Spreading requests across copies raises throughput and lets the tier survive one copy dying.

ip_hash can pin a client to one server, a sticky session, and it is tempting while sessions live in memory. The Twelve-Factor App calls sticky sessions a violation that should never be relied upon: when the pinned copy restarts or is removed, every session it held goes with it. Keep sessions in a datastore with time-expiration instead, such as Memcached or Redis, and the load balancer is free to send anyone anywhere.

One part does not scale out this way: the database. Every copy you add still talks to the same one, and every copy's connection pool counts against it. That is where the next module starts.

## What should a CDN cache, and what must it never store?

A CDN is a geographically distributed network of proxy servers that keeps copies of your responses close to visitors. Each edge is a shared cache, one that stores responses for more than one user, so its value is the hit ratio: every request it answers is one the origin never sees. Cache-Control tells it what to do. s-maxage sets the lifetime for shared caches only, so public HTML can send max-age=0 with a short s-maxage: the edge absorbs the traffic while browsers still revalidate.

The safest long lifetime goes on a URL that changes when its content does. MDN calls it cache busting: put a version or a hash in the file name or the query string, mark the response immutable for a year, and nothing ever needs purging, because new content arrives at a new URL.

What must never sit in a shared cache is anything personal: a page behind a login, or a response that sets a cookie. Mark personalised responses private, which forbids shared caches from storing them, or no-store, which forbids every cache. MDN warns that a cookie on its own does not make a response private, and recommends private instead of Vary: Cookie. Requests that change state, such as a POST, go to the origin anyway.

## Where does an application cache fit once the CDN is in place?

Requests that pass the edge still repeat: the same product, the same post, the same profile. Cache-aside handles them in the app: look in the cache first; on a miss, read the database, store the result with an expiry and return it. On a write, update the database first and then delete the cached entry, so the next read loads the fresh row.

The arithmetic is the reason. With an 80 percent hit ratio only 20 percent of reads fall through, so 1,000 reads a second become 200 at the database, and that number, not the raw rate, is what the database must be sized for. Keep the cache shared rather than in each copy's memory: Azure's guidance notes that local caches in different instances quickly become inconsistent. And keep the expiry even when you delete on write, because something else may change the row.

## The real config behind this lesson

`lib/rate-limit.ts:1-11`

```typescript
/**
 * Simple in-memory rate limiter for API routes
 * In production, consider using Redis (Upstash) for distributed rate limiting
 */

interface RateLimitEntry {
  count: number;
  resetTime: number;
}

const rateLimitMap = new Map<string, RateLimitEntry>();
```

This site's API rate limiter keeps its counts in a Map inside the Node process. With one container, as the site runs today, that is correct. Run two copies behind a load balancer and each counts on its own, so a client could get up to twice the limit, and every deploy, which replaces the container, resets the counts. The file's own comment names the fix: a shared store such as Redis.

`next.config.ts:227-239`

```typescript
        // The films, their posters and captions. A re-render keeps the file
        // names, so only a request carrying the render stamp
        // (lib/film-manifest.ts prints `?v=` on every URL) may be cached as
        // immutable; Cloudflare then serves the films from its edge. A bare
        // URL, from an old link, keeps revalidating against the origin.
        source: "/videos/:path*",
        has: [{ type: "query", key: "v" }],
        headers: [
          {
            key: "Cache-Control",
            value: "public, max-age=31536000, immutable",
          },
        ],
```

Cache busting in this site's own config. A film keeps its file name when it is re-rendered, so only a URL carrying the render stamp, ?v=, is marked immutable for a year, and Cloudflare serves those from its edge. A bare URL from an old link keeps revalidating against the origin, so nobody is stuck with an outdated film.

## What breaks

- **Sticky sessions as the fix.** Pinning each user to one copy hides in-memory sessions instead of removing them. When that copy restarts, is removed or fails, everyone pinned to it is logged out. Move sessions to a shared store and drop the stickiness.
- **State you forgot was state.** Sessions are the obvious case. Counters such as a rate limiter, uploads written to local disk and timers inside the app break the same way: each copy counts alone, a file exists on one disk only, and a scheduled job runs once per copy.
- **A personalised page in a shared cache.** Cache a page that greets one user and the next visitor gets their name, or worse, their invoice. Vary: Cookie does not fix it; it stores one copy per cookie and still keeps private data on a shared system. Mark it private or no-store.
- **What I'd change here: immutable on a name that can change.** This site sends public, max-age=31536000, immutable for everything under /images/, yet blog covers are re-rendered under the same file name when the cover template changes. A browser that already holds an old cover may keep showing it for up to a year. Versioning the cover URL, as the films do with ?v=, would fix it.

### Scale up or scale out

|  | Scale up (bigger server) | Scale out (more copies) |
| --- | --- | --- |
| Code changes | None: the app does not notice | The app must be stateless |
| If the machine dies | All capacity is gone at once | One share of capacity is gone |
| Ceiling | The largest machine you can rent | Much higher, bounded by shared parts like the database |
| What it needs | A restart or migration to resize | A load balancer and a shared store |

## Try it yourself

You need Node and Docker. Run two copies of a tiny server that counts visits in memory, put nginx in front of them, watch the count split, then move it to Redis.

1. Save server.mjs. It counts visits in its own memory, which is exactly the state that breaks scaling out.

   ```bash
   // server.mjs: counts visits in this process's memory
   import http from "node:http";
   const port = Number(process.env.PORT);
   let visits = 0;
   http
     .createServer((req, res) => {
       visits += 1;
       res.end(`port ${port}: visit ${visits}\n`);
     })
     .listen(port);
   ```
2. Start two copies on different ports.

   ```bash
   PORT=3001 node server.mjs &
   PORT=3002 node server.mjs &
   ```
3. Save nginx.conf and run nginx in Docker as a round-robin load balancer on port 8080. The --add-host flag lets the container reach your machine on Linux too.

   ```bash
   events {}
   http {
     upstream app {
       server host.docker.internal:3001;
       server host.docker.internal:3002;
     }
     server {
       listen 8080;
       location / { proxy_pass http://app; }
     }
   }
   
   # then:
   docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway \
     -v "$PWD/nginx.conf:/etc/nginx/nginx.conf:ro" nginx:alpine
   ```
4. In another terminal, send four requests. Each copy keeps its own count, so visit 1 appears twice: the state is split across the copies.

   ```bash
   for i in 1 2 3 4; do curl -s localhost:8080; done
   ```
5. Stop both copies, start Redis, install the client and replace the counter with a shared one. Start both copies again: the count now rises across them, 1, 2, 3, 4.

   ```bash
   docker run --rm -d -p 6379:6379 redis:7-alpine
   npm install redis
   
   // server.mjs: the count now lives in Redis, shared by every copy
   import http from "node:http";
   import { createClient } from "redis";
   const redis = await createClient().connect();
   const port = Number(process.env.PORT);
   http
     .createServer(async (req, res) => {
       const visits = await redis.incr("visits");
       res.end(`port ${port}: visit ${visits}\n`);
     })
     .listen(port);
   ```

## Quick quiz

1. **You run three copies behind nginx with round robin, and users say they get logged out at random. What is the most likely cause?**
   A. Sessions live in each copy's memory
   B. nginx drops cookies on the way through
   C. Three copies are too many for round robin
   D. The database is too slow to log users in
   Answer: A. Each copy only knows the sessions it created, so a request dealt to another copy arrives as a stranger. Move sessions to a shared store; sticky sessions only hide the problem until a copy restarts.

2. **Which response is safe to keep at a shared CDN edge for a long time?**
   A. A dashboard page that greets the logged-in user
   B. A response that sets a session cookie
   C. A fingerprinted image such as logo.3f9c2b.png
   D. The response to a POST that places an order
   Answer: C. It is identical for every visitor and its URL changes whenever its content does, so a long immutable lifetime is safe. Personalised pages and cookie-setting responses must stay out of shared caches, and a POST changes state at the origin.

3. **A Redis cache in front of the database has an 80 percent hit ratio, and 1,000 reads per second reach the app. How many reach the database?**
   A. About 800 per second
   B. About 200 per second
   C. None, because the cache answers every read
   Answer: B. Only the misses fall through: 20 percent of 1,000 is 200 a second. That fall-through rate, not the raw read rate, is what the database must be sized for.

## Think it through

- **Name two kinds of state, besides sessions, that stop an app from scaling out.** In-memory counters such as a rate limiter, and files written to local disk. Timers inside the app are a third: with three copies they fire three times.
- **Why send max-age=0 together with a positive s-maxage on public HTML?** s-maxage applies only to shared caches, so the CDN edge keeps and serves the page while each browser revalidates, and a new deploy reaches readers quickly.
- **Why is a versioned URL better than purging the CDN?** Nothing has to be deleted. New content arrives at a new URL, so every cache misses once and fetches it, while the old URL can stay cached for as long as it likes.

## Go deeper

- [Horizontal vs Vertical Scaling: Which to Pick First](https://www.matthewswong.com/en/blog/horizontal-vs-vertical-scaling/)
- [Load Balancing Algorithms: Round Robin, Least Conn, IP Hash](https://www.matthewswong.com/en/blog/load-balancing-algorithms-explained/)
- [How a CDN Works: What to Cache at the Edge and What Not To](https://www.matthewswong.com/en/blog/how-cdn-works-edge-caching-explained/)
- [Cache-Aside vs Write-Through vs Write-Back: Which Fits?](https://www.matthewswong.com/en/blog/cache-aside-vs-write-through-caching/)

## Every post in this module, in order

- [Horizontal vs Vertical Scaling: Which to Pick First](https://www.matthewswong.com/en/blog/horizontal-vs-vertical-scaling/)
- [Load Balancing Algorithms: Round Robin, Least Conn, IP Hash](https://www.matthewswong.com/en/blog/load-balancing-algorithms-explained/)
- [Cache-Aside vs Write-Through vs Write-Back: Which Fits?](https://www.matthewswong.com/en/blog/cache-aside-vs-write-through-caching/)
- [Cache Invalidation Strategies: TTL, Events and Versioned Keys](https://www.matthewswong.com/en/blog/cache-invalidation-strategies/)
- [How a CDN Works: What to Cache at the Edge and What Not To](https://www.matthewswong.com/en/blog/how-cdn-works-edge-caching-explained/)
- [Consistent Hashing Explained: Hash Ring and Virtual Nodes](https://www.matthewswong.com/en/blog/consistent-hashing-explained/)

## Sources

- [The Twelve-Factor App: VI. Processes](https://12factor.net/processes)
- [nginx docs: Using nginx as HTTP load balancer](https://nginx.org/en/docs/http/load_balancing.html)
- [IETF RFC 9111: HTTP Caching](https://www.rfc-editor.org/rfc/rfc9111.html)
- [MDN: HTTP caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching)
- [Azure Architecture Center: Cache-Aside pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/cache-aside)
- [Wikipedia: Content delivery network](https://en.wikipedia.org/wiki/Content_delivery_network)

---

- HTML version: https://www.matthewswong.com/en/learn/system-design/how-to-scale-a-web-application/
- Bahasa Indonesia: https://www.matthewswong.com/id/learn/system-design/how-to-scale-a-web-application/
- Site index for agents: https://www.matthewswong.com/llms.txt
- Sitemap: https://www.matthewswong.com/sitemap.xml
- API description (OpenAPI): https://www.matthewswong.com/openapi.json
