Make the right trade-off / CHAPTER 27
Horizontal versus vertical scaling
Choose between larger machines and more machines by identifying the resource and coordination bottlenecks.
Builds on Back-of-the-envelope estimation, Data partitioning.
- Explain scale-up and scale-out limits.
- Distinguish adding replicas from partitioning writes.
- Plan capacity with failure headroom.
01Larger machines or more machines
A photo API runs on one worker with two CPU cores and 8 GB memory. Tests show that it safely serves 200 requests/s and CPU is the limiting resource.
There are two different changes:
- Vertical scaling: replace that worker with one having eight cores and 32 GB. The worker count stays one; each worker has more resources. Scaling down gives each worker fewer resources.
- Horizontal scaling: run several workers and distribute requests among them. Scaling in removes workers after arranging what happens to their requests and sessions.
Neither change guarantees proportional throughput. Extra cores cannot bypass an exclusive lock that lets only one request proceed at a time. Extra API workers cannot make a saturated shared database accept more work.
Capacity and failure protection are separate. One large worker may handle every healthy request, but its crash removes the entire worker. Several workers need enough capacity left after a crash or while one is being replaced during a deployment.
Vertical: replace one worker with a larger worker
Follow the numbered arrows
- Smaller worker → Resize or replaceprepare the replacement
- Resize or replace → Larger workertest the larger worker
Trace the diagram in words
- Measure which resource limits the small worker.
- Stop new routing and safely finish work while replacing or resizing it.
- Check the larger worker’s actual capacity before returning traffic.
Horizontal: replace one request path with a worker fleet
Follow the numbered arrows
- Clients → Load balancersend requests
- Load balancer → Worker Achoose a healthy worker
- Load balancer → Worker Bchoose a healthy worker
- Worker A → Shared session recordsload or update saved session records
- Worker B → Shared session recordsload or update saved session records
Trace the diagram in words
- Keep the client endpoint stable while adding a worker.
- Either worker loads the shopper’s saved session and cart from shared storage.
- Stop new assignments before removal and verify that survivors can handle the redistributed work.
02Plan routing, state and replacement
For a larger worker, follow the replacement
- Measure the limiting resource. More memory helps when frequently used data does not fit; it does not fix a one-at-a-time lock.
- Prepare the change. Some platforms resize in place; others restart or replace the machine. Stop assigning requests to the old worker. Let accepted requests finish; if a response is lost, retry using the same request key and its saved result so a completed purchase is not repeated.
- Restore access to saved data. Attach persistent storage or reconnect to shared services, check the new worker, then switch traffic. Keep a way to return to the previous setup.
For more workers, follow one shopper
- A load balancer chooses a healthy worker for each request. It needs enough surviving workers, not merely enough configured instances.
- The shopper's cart must be recoverable. If the cart exists only in worker
A's memory, routing the next request toBgivesBno cart to read. Save the cart in a shared store, carry a small signed login token while storing the changing cart separately, or transfer it before removingA. - Handle sessions deliberately. Sending a shopper back to
Ais sticky routing. It improves locality but cannot recoverA's memory after a crash. Tokens need size, privacy and revocation rules too.
| Change | What becomes simpler? | What costs remain? |
|---|---|---|
| Use larger machines | Record placement and routing may stay unchanged. | Restarts, hardware ceilings and spare large-machine cost remain. |
| Use more machines | Independent requests can spread across workers. | Routing, shared-state access, coordination and fleet deployment must work. |
| Remove capacity | Cost may fall when demand falls. | Finish existing work and verify survivor capacity before termination. |
03Worked example: survive one failed worker
Peak demand is 900 requests/s. Each ordinary worker safely handles 200/s.
- Five healthy workers provide 1,000/s. After one fails, four provide only 800/s, below demand.
- Six workers survive one loss. Five survivors provide 1,000/s, assuming reasonably balanced requests and sufficient database capacity.
- Test a vertical alternative separately. If each larger worker is measured at 1,000/s, two such workers can survive one loss and still handle 900/s. One larger worker cannot offer that same failure protection.
- Include deployment time. If one of six ordinary workers is drained for replacement and another crashes, only four remain: 800/s. The six-worker plan does not cover those two simultaneous absences.
- Reduce the fleet safely when demand falls. At 300/s demand, three ordinary workers leave 400/s after one loss. Stop new routing to a removed worker, finish short requests and save progress or reconnect long sessions elsewhere.
Suppose an ordinary worker costs one unit/hour and a large worker costs four. Six ordinary workers cost six units/hour; two large ones cost eight. These are illustrative prices, not claims about actual hardware. Compare tested capacity, discounts, spare capacity and operating effort before deciding.
Horizontal versus vertical scaling — decision flow
Follow the numbered arrows
- Photo API requests → Load balancersend requests
- Load balancer → Workers A–Cchoose a healthy worker
- Load balancer → Workers D–Fchoose a healthy worker
- Workers A–C → Saved session databaseload or save session records
- Workers D–F → Saved session databaseload or save session records
Trace the diagram in words
- The endpoint distributes 900 requests/s across six workers measured at 200/s each.
- After one loss, five workers provide 1,000/s if traffic is balanced and shared storage keeps up.
- Before removing a worker, stop new assignments and preserve its accepted work and sessions.
04A useful scaling sequence
Application scaling and database scaling solve different problems:
- Read replicas copy records. They can answer reads only where their delay is acceptable; copying does not divide primary write ownership.
- Partitions split records. For example, different customer IDs can go to different database servers, spreading independent writes. Queries touching several partitions need more coordination.
- Popular records can still overload one partition. A document database and a relational database can both encounter this problem. Their labels do not decide whether partitioning is possible.
Choose larger workers when the measured resource gain provides sufficient spare capacity with simpler operation. Choose more workers when requests divide usefully and the extra routing and state handling justify their cost.
Before scaling down or in, check memory needs, traffic bursts, surviving capacity and shared dependencies. In the interview, describe the before-and-after request path, how a cart survives, what one failure removes, and what the change costs.
DRAW. CALCULATE. EXPLAIN.
Plan growth and a later scale-in
20 minutes · guided practice
A photo API peaks at 1,350 requests/s. Each ordinary worker safely handles 300/s. It must survive two unavailable workers, including deployment. Demand later falls to 400/s but one loss must remain tolerable. Size both fleets and describe session handling while removing workers.
Compare the worked solution
- Peak demand needs five surviving workers because 1,350 ÷ 300 rounds up to five. To tolerate two unavailable workers, run seven.
- At 400/s demand, three workers leave 600/s after one loss. Two workers would leave only 300/s.
- Before removing a worker, stop new assignments and finish or safely retry accepted work. Save or transfer sessions so another worker can continue them.
- Check uneven traffic, database limits and bursts before reducing the fleet.
- Compare a larger single worker as a separate option; it needs its own measured capacity and additional failure protection.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01What changes in vertical versus horizontal scaling?
- Vertical scaling gives each machine more resources.
- Horizontal scaling adds machines and distributes work.
- Neither removes an unchanged shared bottleneck; measure the resource that limits progress.
CARD 02How do you size for a failure?
- Calculate capacity after the allowed losses.
- At 900 requests/s and 200 per worker, six leave 1,000/s after one loss.
- Include deployment absences and shared-database limits.
CARD 03What happens before removing a worker?
- Stop new assignments.
- Finish or save accepted work.
- Make sessions recoverable elsewhere.
- Check remaining capacity.
Sticky routing cannot recover a terminated worker’s memory.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.