KAHIBARO
Discord Login Register

26.8. Horizontal Scaling

Understanding Horizontal Scaling

Horizontal scaling is about adding more machines to handle more work. Instead of buying one very powerful server, you use several smaller servers that work together.

This is one of the most important ideas in modern backend systems, especially for applications that need to handle millions of users.

Horizontal vs Vertical Scaling

There are two basic ways to scale a backend system.

ApproachWhat you addExample
Vertical scalingMore power to one machineUpgrade from 4 cores / 8 GB RAM to 32 / 128
Horizontal scalingMore machinesRun 10 servers instead of 1

Vertical scaling is simple to manage, but it has limits. At some point you cannot add more CPU or RAM, or it becomes very expensive.

Horizontal scaling has more moving parts, but it can grow much further. You can keep adding more servers as needed, if your architecture allows it.

Key rule: Horizontal scaling works well only if your application is stateless, or at least minimizes state on each server.

When Horizontal Scaling Helps

Horizontal scaling is useful when:

It is less useful when:

Example: API Server

Imagine an HTTP API server that:

Each request is mostly independent from others. You can easily put 10 identical API servers behind a load balancer. Each server runs the same code and connects to the same database.

If one server crashes, the load balancer can send traffic to the remaining 9.

Load Balancers and Traffic Distribution

Horizontal scaling usually requires a load balancer in front of your servers.

A load balancer:

Common Load Balancing Strategies

StrategyIdeaWhen useful
Round robinSend each new request to the next server in a listSimple, servers have similar capacity
RandomPick a random serverSimilar to round robin, simple to implement
Least connectionsChoose server with fewest active connectionsWhen request duration varies
IP hashChoose server based on client IP hashWant the same client to usually hit the same server

You will see load balancers implemented with tools like Nginx, HAProxy, Traefik, or cloud load balancers from AWS, GCP, or Azure.

Example: Simple Round Robin

Suppose you have 3 servers: A, B, C.

Requests arrive like this:

  1. Request 1 → A
  2. Request 2 → B
  3. Request 3 → C
  4. Request 4 → A
  5. Request 5 → B
  6. Request 6 → C

No client needs to know that there are 3 servers. They only talk to the load balancer.

Stateless Applications and Shared State

Horizontal scaling is easiest if your application servers are stateless.

A stateless server:

Important: If you store user sessions only in server memory, you will have problems with horizontal scaling. Use shared stores like Redis or the database instead.

Bad Example: In-Memory Sessions

Imagine a login system like this:

Server B has no idea about the session in server A, so the user appears logged out.

Better Example: Shared Session Store

A better design for horizontal scaling:

This way any server can serve any user.

Database Constraints and Scaling Limits

Horizontal scaling often starts by adding more application servers. However, the database can quickly become the bottleneck.

Often the pattern looks like this:

To scale further, you will eventually need:

Horizontal scaling of app servers is simpler than scaling databases, but both are usually needed in large systems.

Horizontal Scaling in Practice

Let us see how horizontal scaling fits into a typical production setup.

Typical Architecture

A common horizontally scaled architecture looks like this:

  1. Clients (browsers, mobile apps) send HTTP requests.
  2. A load balancer (for example Nginx) receives the requests.
  3. The load balancer forwards requests to one of several application servers.
  4. Application servers read and write data in:
    • A database (for persistent storage).
    • A cache such as Redis (for fast lookups, sessions, rate limiting).

You can increase capacity by:

Example: Scaling a FastAPI Service

Assume you have a FastAPI application running with Uvicorn.

On a single server you might run:

bash
uvicorn app.main:app --host 0.0.0.0 --port 8000

To scale horizontally you could:

  1. Build a Docker image for your app.
  2. Run the same container on 5 different servers.
  3. Put an Nginx or cloud load balancer in front of those 5 servers.

From the user’s point of view it is still one API endpoint, for example:

text
https://api.example.com

Internally, this points to 5 backend instances.

State, Files, and Shared Storage

Horizontal scaling becomes tricky when your application uses local storage.

Problem: Local File Uploads

Bad pattern:

This breaks the user experience.

Better Pattern: Shared or External Storage

Safer patterns for horizontally scaled systems:

The basic rule is:

Important: Do not rely on local files on a single server for user data if you plan to scale horizontally. Use shared or external storage instead.

Health Checks and Auto Scaling

In many cloud environments you can combine horizontal scaling with auto scaling.

Key ideas:

Health checks are usually simple endpoints that check:

Simple Health Check Example

A minimal FastAPI health check:

python
from fastapi import FastAPI
app = FastAPI()
@app.get("/health")
def health():
    return {"status": "ok"}

Your load balancer uses this to decide if an instance can receive traffic.

Consistency and Sticky Sessions

Sometimes you cannot avoid having per-user state in memory. In that case, load balancers can use sticky sessions.

Sticky sessions (also called session affinity):

Example:

This can work in some cases, but it has problems:

In modern systems, sticky sessions are usually avoided in favor of shared session stores and stateless application instances.

Horizontal Scaling and Microservices

Horizontal scaling is related to architecture choices such as microservices.

Example:

This is another benefit of separating services cleanly.

Costs, Tradeoffs, and Complexity

Horizontal scaling is powerful, but not free.

Advantages

Disadvantages

A common path is:

  1. Start with a simple single server, vertical scaling only.
  2. When you hit limits, move to horizontal scaling with at least two instances and a load balancer.
  3. Gradually externalize state to databases, caches, and storage services.

Summary

Horizontal scaling is about adding more machines instead of just a bigger one. To make this work:

Understanding horizontal scaling is a key step toward building backend systems that can support real world traffic and remain reliable as your user base grows.

Views: 6

Comments

Please login to add a comment.

Don't have an account? Register now!