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.
04SYSTEM 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
CONCEPTVertical scalingMore resources in one machine
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
STEP 1Health checkCan this instance serve?
STEP 2Target poolInclude healthy instances
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
STEP 1Before3 x 300 = 900 requests/s
STEP 2One fails2 x 300 = 600 requests/s
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
STEP 1Traffic risesQueueing may begin
STEP 2Observe + startNew capacity takes time
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
Approach
What changes
Benefit
Limit
Vertical scaling
Resources of one machine
Simple first improvement
Machine size and failure-domain ceiling
Horizontal scaling
Number of instances
Distribute suitable work
Shared state and routing need design
Round robin
Eligible request destination
Simple routing policy
Equal counts can hide unequal request cost
Least connections
Destination by active count
Useful for some connection-heavy workloads
Not a direct CPU or queue-work measurement
Autoscaling
Capacity over time
Respond to suitable signals
Startup 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.
Fail one instance. Find a fleet size that can still serve the offered demand in this model.
12 healthy servers
2400 requests/s capacity
3100 requests/s excess
Offered demand exceeds modeled capacity.
Healthy instances x per-instance capacity gives the ideal fleet limit. Retain room for failures and identify shared dependencies before adding servers.
Chapter review: two questions
Attempt both questions before opening the model answers.
Question 1
A fleet must serve 900 requests/s after one server fails. Each server handles an assumed 250 requests/s. Find the minimum total server count in the homogeneous ideal model.
Reveal model answer
The healthy fleet needs ceil(900 / 250) = 4 servers. Add one server for the stated single-instance failure, giving five total. Four total would leave only 750 requests/s after the failure. This does not account for other shared bottlenecks or detection delay.
Question 2
A load-balanced application has four healthy servers but one student's cart disappears when requests change servers. Identify the likely design issue and compare two fixes.
Reveal model answer
The cart may live only in an instance's private memory. Shared durable or appropriately persisted session storage lets another instance recover it. Sticky routing may temporarily preserve the experience but creates affinity, imbalance and loss exposure when that instance fails. Explain the selected durability contract.
Answers stay in this tab. Download before leaving. This practice is not graded or saved to your account.