Buildfest Learn System DesignRead and experiment freely · Chapter activities stay in this tab
Practice librarySystem DesignScaling and load balancing

LESSON 04 • SYSTEM DESIGN

Scaling and load balancingOne server has limits.
Your system doesn’t have to.

Scaling and load balancing. Learn the concepts, explore a model and defend your design.

DEFINITION

Scaling changes resources or instance count so a system can meet a workload target. Vertical scaling gives a machine more resources; horizontal scaling adds instances and distributes work. A load balancer chooses an eligible instance for traffic. It does not automatically coordinate data or eliminate a shared downstream bottleneck.

Two different ways to grow
  1. CONCEPTVertical scalingMore resources in one machine
  2. CONCEPTHorizontal scalingMore application instances
  3. CONCEPTShared constraintA bottleneck can remain in storage

Adding application servers helps only when the limiting work can be distributed.

CONCEPT 1

What happens when one server is too small

Two ways to grow

Vertical scaling means giving one machine more resources, such as CPU or RAM. Horizontal scaling means adding machines or instances and sharing work between them. A bigger kitchen and another kitchen are useful analogies. A larger machine may be the simplest first improvement; several machines may improve capacity or failure tolerance when the application can share work safely.

More machines do not remove every limit

If all application instances depend on one slow database, adding application instances can send even more work to the same bottleneck. A bottleneck is the resource or dependency limiting the useful result. It may be CPU, a database lock, a small connection pool, a network link or a remote API. Find it through measurement instead of assuming the visible web server is always the problem.

Start with a practical change

For a small canteen app, first check the slow query, unnecessary repeated work and resource limits. Upgrade the machine if that meets the need cheaply. Add application instances when useful, with shared durable state and a way to route requests. The decision depends on constraints, not a rule that horizontal is always superior.

CONCEPT 2

A load balancer chooses where a request goes

A person directing customers

Imagine a helper sending customers to available checkout counters. A load balancer performs a related job for requests. It accepts or routes traffic and chooses an eligible application instance. Round robin cycles through the eligible list. Least connections chooses using active connection counts. Neither method directly measures how much CPU the next request will consume.

Health and readiness

An instance may be running but unable to do useful work. Readiness checks help decide whether it should receive requests. A failing instance must be detected, and in-flight work may need draining during shutdown. Detection takes time in real systems. Avoid a health check that only says the process exists while ignoring a critical failure that makes every real operation unusable.

Where the analogy stops

Customers normally do not return every few milliseconds carrying state that only one cashier remembers. Requests may depend on authentication, carts or long-running sessions. If that state is private to one server, sending the next request to another server may break the experience. Externalize appropriate state or deliberately design affinity and recovery. Affinity alone does not make data durable.

A healthy target can receive new work
  1. STEP 1Health checkCan this instance serve?
  2. STEP 2Target poolInclude healthy instances
  3. STEP 3Routing policyChoose one available target

Readiness controls admission; liveness controls whether a process should be restarted.

CONCEPT 3

Work through a server failure

Before the failure

Assume three identical application servers can each sustain 200 requests per second for this workload. Their ideal combined capacity is 3 x 200 = 600 requests per second. At 500 incoming requests per second, offered load is about 83.3 percent of that capacity. The unused 100 requests per second is headroom in this simplified model.

After one server fails

Two healthy servers remain. Capacity becomes 2 x 200 = 400 requests per second. Now 100 of the offered 500 requests per second exceeds capacity. A real system may queue, time out or reject that excess; the teaching model simply reports it. If you begin with four servers, losing one leaves three and a modeled 600 requests per second of capacity.

What the calculation does not prove

The arithmetic assumes equal capacity, perfect distribution and immediate failure detection. It says nothing about real latency, connection setup or database limits. If the shared database handles only 300 relevant operations per second and each request needs one, it may set a lower end-to-end limit. A benchmark must use the actual operation mix and report what failed as well as what completed.

Capacity after a server fails
  1. STEP 1Before3 x 300 = 900 requests/s
  2. STEP 2One fails2 x 300 = 600 requests/s
  3. STEP 3Demand: 750/s150/s exceeds capacity

These are illustrative limits. Account for failure capacity rather than sizing to a perfect day.

CONCEPT 4

Common scaling questions with answers

Should every system use microservices

No. A modular monolith keeps one deployment while organizing code by responsibility. It can support several application instances behind a balancer. Separate services can help with independent teams, workload scaling or trust isolation, but introduce network delays, partial failures and data coordination. Choose a boundary because it solves a problem you can explain.

Will autoscaling prevent every outage

No. Autoscaling adds or removes capacity based on configured signals and takes time. A sudden burst can arrive before new instances are ready. A database or external provider may not scale with the application fleet. Combine suitable scaling with admission limits, recovery margin and a plan to degrade nonessential behavior, such as delaying analytics instead of blocking orders.

What should I say in an interview

Explain the request flow, shared state, health checks and the new bottleneck after adding instances. Then discuss the balancer itself: what happens if it fails, how TLS is handled and how bad instances stop receiving traffic. A clear statement of limits is stronger than saying a component makes the whole system highly available without defining a failure scenario.

Autoscaling reacts after demand changes
  1. STEP 1Traffic risesQueueing may begin
  2. STEP 2Observe + startNew capacity takes time
  3. STEP 3Capacity readyRoute eligible work

A scaling delay still needs bounded queues, admission policy and headroom.

Key learning points

  • Find the limiting resource before adding more of the visible web-server layer.
  • Stateless application instances can recover and share work more easily when appropriate state is external.
  • Readiness means able to serve useful work, not merely that a process exists.
  • Calculate healthy capacity after an instance failure.
  • Autoscaling takes time and must not overload a fixed-capacity dependency.

Compare the important ideas

Scaling and load balancing: concepts and trade-offs
ApproachWhat changesBenefitLimit
Vertical scalingResources of one machineSimple first improvementMachine size and failure-domain ceiling
Horizontal scalingNumber of instancesDistribute suitable workShared state and routing need design
Round robinEligible request destinationSimple routing policyEqual counts can hide unequal request cost
Least connectionsDestination by active countUseful for some connection-heavy workloadsNot a direct CPU or queue-work measurement
AutoscalingCapacity over timeRespond to suitable signalsStartup delay and downstream limits remain

WORKED EXAMPLE

Keep a failure margin

Three identical servers at an assumed 200 requests/s each provide 600 requests/s in an ideal model. At demand 500 requests/s, one failed instance leaves 400 requests/s and 100 requests/s above capacity. Four initial servers leave 600 requests/s after one failure. This assumes perfect distribution and immediate detection; real latency, queues and the database must be assessed separately.

Further reading

Original teaching explanations by Buildfest Learn. Check these primary references for the underlying concepts and implementation details.