01 / 25 · Traffic and capacity
Load balancing
One address receives requests and shares them across healthy copies of your application. It chooses a destination; it does not create extra servers or make slow database queries disappear.
What you will practise
- Trace a request to a healthy replica
- distinguish routing from creating capacity
- calculate survivor capacity
Before you start: Client, request, server and replica.
About 4 minutes · offline-generated English female narration · captions included. Diagrams are conceptual; timings are illustrative.
Download MP4CaptionsListen without the video
Download audioChapters and full transcript
Let us understand load balancing. A school has three lunch counters. A helper sends each child to an open counter instead of letting everyone crowd around the first. If a counter closes, the helper stops using it. But if all counters share one empty kitchen, the helper cannot create food. Likewise, distributing requests does not remove a shared database bottleneck. One address receives requests and shares them across healthy copies of your application. It chooses a destination; it does not create extra servers or make slow database queries disappear.
Follow the normal path. Visitors send event requests to one endpoint. The load balancer selects an application target so visitors do not need to know individual server addresses. Alternate requests between App A and App B. This shares turns, but a slow request can still occupy one target much longer than another. In our fictional SeatSpot ticket app, many people open the same event at once. One application instance becomes overloaded while a second has spare capacity. A load balancer makes the spare capacity reachable through the same public endpoint.
Now change one condition. App A stops passing readiness checks. Detection takes time; requests already in flight may fail and must be handled safely by the client. A common misunderstanding is this: Round robin means equal CPU usage. It shares turns; request cost and connection lifetime can differ. Think about this. App A fails. Can App B always take all its traffic? Pause the video and make your prediction. Only if App B and the shared dependencies have enough tested spare capacity. Health checks also take time to remove App A.
Let us reason with a concrete example. Suppose one tested replica handles 200 requests/second within the latency objective and the expected peak is 450. Three replicas provide 600 requests/second in the simplified model. Losing one leaves 400, below the peak. Four leave 600 after one failure. Real capacity must be load-tested with the database and dependencies. Use four if that failure headroom is required and affordable. Monitor per-target latency, error rate and queueing; do not infer usable capacity from instance count alone. These figures are teaching examples, not production guarantees.
Turn the idea into a design decision. New requests go to App B after the target is removed. App B needs enough spare capacity; routing cannot manufacture capacity during an outage. Both copies use the same order system. A repeated booking request carries the same idempotency key so a lost response does not charge twice. When you try the practice task, look for this result. The surviving target receives new traffic, but the database remains shared.
Check your understanding. Check latency and errors at the balancer, application and database. If both applications wait on the database, adding routing rules will not fix that bottleneck. Here is a challenge. You have three replicas each safe at 150 requests/second. Peak demand is 350. Can you survive one replica failing? Pause before the answer. After a failure, two replicas provide 300, below 350. Four total leave 450 after one failure in this simplified model. Also test dependency limits and the actual request mix. For an interview, defend a routing policy, session state and failure headroom. Say what you would measure, not only which tool you would buy.
Start with an everyday picture
A school has three lunch counters. A helper sends each child to an open counter instead of letting everyone crowd around the first. If a counter closes, the helper stops using it. But if all counters share one empty kitchen, the helper cannot create food. Likewise, distributing requests does not remove a shared database bottleneck.
Why SeatSpot needs this
In our fictional SeatSpot ticket app, many people open the same event at once. One application instance becomes overloaded while a second has spare capacity. A load balancer makes the spare capacity reachable through the same public endpoint.
Trace one step at a time
Move through the diagram at your pace. Teal paths are active; unavailable components are labelled.
One public address
Visitors send event requests to one endpoint. The load balancer selects an application target so visitors do not need to know individual server addresses.
Read all six mechanism steps
One public address
Visitors send event requests to one endpoint. The load balancer selects an application target so visitors do not need to know individual server addresses.
Share requests
Alternate requests between App A and App B. This shares turns, but a slow request can still occupy one target much longer than another.
A target fails
App A stops passing readiness checks. Detection takes time; requests already in flight may fail and must be handled safely by the client.
Use the healthy target
New requests go to App B after the target is removed. App B needs enough spare capacity; routing cannot manufacture capacity during an outage.
Protect the booking
Both copies use the same order system. A repeated booking request carries the same idempotency key so a lost response does not charge twice.
Measure the whole path
Check latency and errors at the balancer, application and database. If both applications wait on the database, adding routing rules will not fix that bottleneck.
Pause and make a prediction
App A fails. Can App B always take all its traffic?
Correct a common misunderstanding
Claim to question: Round robin means equal CPU usage.
Better explanation: It shares turns; request cost and connection lifetime can differ.
Route six paper requests
- Draw two app boxes and one shared database.
- Assign six requests alternately to the apps.
- Cross out App A, then recalculate capacity before rerouting.
Check what you can explain
This is your self-check, not an automated grade. Try again without the diagram tomorrow.
0 of 3 goals checked. Choose one remaining idea to practise next.
Teach it back
Without looking, explain what this component changes, what it does not solve and what happens if it fails. Use the animation to check your explanation, then switch to Engineering.
Engineering model and trade-offs
At layer 4, routing works with transport connections; at layer 7 it can inspect HTTP paths, hosts or headers. Pick based on protocol, routing needs and operational cost rather than assuming layer 7 is always better.
Round robin distributes turns, not equal processing time. Least-connections can help with different connection lifetimes; weights can reflect unequal capacity. Health/readiness checks must represent whether a target can actually serve traffic.
Keep login/session state outside individual application memory when requests may reach different replicas. Drain old connections during removal. Retries of booking/payment requests need idempotency keys so a lost response does not create a second purchase.
Work through an example
Suppose one tested replica handles 200 requests/second within the latency objective and the expected peak is 450.
Reason with the numbers
Three replicas provide 600 requests/second in the simplified model. Losing one leaves 400, below the peak. Four leave 600 after one failure. Real capacity must be load-tested with the database and dependencies.
Make a decision
Use four if that failure headroom is required and affordable. Monitor per-target latency, error rate and queueing; do not infer usable capacity from instance count alone.
Implementation sketch
upstream seatspot {
server app-a:3000;
server app-b:3000;
}
server {
listen 8080;
location / { proxy_pass http://seatspot; }
}This minimal reverse-proxy configuration shares requests between two targets. It omits production TLS, timeouts and platform-specific health checks. Run such infrastructure locally or in your own environment, not in the browser compiler.
Where this is used
- Stateless web/API replicas behind one endpoint.
- Spreading reads or connections across healthy service instances.
Common mistakes
- Calling round robin a guarantee of equal CPU usage.
- Making the load balancer a single unprotected instance.
- Retrying a purchase without an idempotency key.
Practise a reasoned answer
Answer aloud or write notes before opening the reference. These prompts teach reasoning and trade-offs; they are not claimed questions from a particular company.
How would you choose a load-balancing policy?
Choose a practice depth
- Foundation
- Explain one request and a failed target.
- Practical
- Defend a routing policy, session state and failure headroom.
- Advanced
- Discuss correlated failures, draining, retries and measured tail latency.
Self-check rubric
- State the required user outcome and the relevant invariant.
- Explain the normal path and one failure path.
- Name a cost or consistency/latency trade-off.
- Say what you would measure or test before trusting it.
For deeper practice, connect this topic to the previous lesson and identify a shared bottleneck. A longer answer is not automatically a better answer.
Try it before viewing the solution
You have three replicas each safe at 150 requests/second. Peak demand is 350. Can you survive one replica failing?
Check one key idea
Your explanation and interview notes
Saved only in this browser when storage is available. Export a backup from the course page before clearing browser data or moving devices.
Loading your local notes…
Official references and next reading
The explanation, fictional examples and animations are original teaching material. Vendor behavior depends on the specific product/version; use its documentation before deploying.