Ch.84: System Design: When Load Balancing Makes Failure Worse

Outline

Transcript

0:00 A load balancer decides which server gets each request. Quick errors can make a broken server look idle, which sends even more traffic its way. We'll design the load balancer for the Final Whistle app. A score read fetches the latest score without changing it. We'll follow one, see which servers can answer, and decide when trying again takes room away from new reads. When a score app gets stuck, my first thought is to try again. In our case one server is slow and three can still answer. If the balancer keeps routing to the slow one, reads that run out of time can turn into enough extra attempts to swamp the other three. The fan's request path starts where the app connects.

0:44 We call that a network entry. If one entry fails, another can forward the request. Behind that entry are proxies, which pass reads to score servers. Those servers read the same published score data, kept in multiple copies. The app knows how to get in. Where does a proxy get its list of destinations? The registry lists where to reach each score server. To see which machines can answer, proxies send small test requests called probes and check how actual reads turned out. At the proxy, that read can go through, get turned away when there's no room, or go to another server if the first one fails.

1:22 We'll come back to this map once we've decided what makes each choice safe. Before picking a server, let's say what the system must do for that waiting fan. Those are our functional requirements. Return the game's most recent published score, with the time it was last updated, or an explicit error. A score server can join or leave while ongoing reads complete. Put the fan's read on the clock. Its deadline is 300 milliseconds from request to answer. Okay, if that clock runs out, what does the fan see?

1:51 Unavailable. I'd rather end the wait than present an old score as current. That deadline is one of our non-functional requirements, the limits this design has to meet. Right. We also limit how long the proxy waits and when it can try the read again, leave room to lose one server when traffic is busiest, and keep registry calls out of each read. Does the server need to remember which fan asked? No. Another system receives score updates and keeps the shared copies. Any eligible server, one cleared to take reads, can answer.

2:29 If the copies take too long to reach, the same deadline applies. Because the score servers are interchangeable, we can choose a server for each request. A network connection is a link two machines keep open to exchange data. Each proxy can reuse those links and still choose where each new read goes. A network engineer could reasonably ask, 'Why send the request through one more forwarding step? Our network entry already spreads the app's connections across proxies.' What extra information does this proxy get?

3:00 The application request itself. HTTP two is a web protocol that lets many reads travel at once over the same connection. Giving each server an equal number of connections doesn't tell us how much unfinished work each received. Right. Think of two buses: one carries a single passenger, the other carries 40. If the passengers are the work, counting vehicles can hide the difference. Yeah, that lets us see individual reads at the cost of more processing and another forwarding step. We run multiple proxies so losing one doesn’t take the service down.

3:33 The source links are in the description. Once requests are visible, round robin sends successive reads to servers in turn. It works well when reads take similar effort and the machines can handle similar amounts. A server's capacity is the work it can finish each second. Trouble starts when that similarity breaks. Suppose one machine pauses while the others keep replying. Round robin continues handing it reads. The assignment counts look fair, but its unfinished requests pile up. Exactly. Take that slow machine: its active requests are reads this proxy sent but hasn't finished.

4:07 A slow destination accumulates more of them, giving us a reason to send new work elsewhere. Okay, I was picturing all the servers from one place. Does each proxy actually know all the work the other proxies have sent? It doesn't. Its local count misses their traffic, and a low count can't tell us whether that server can keep up. Which means we can improve a choice without pretending to have perfect balance. How should we use that partial view? With that local view, let's say we sample two eligible servers by picking them at random. One has two active reads from this proxy, the other has eight.

4:45 Choose the one with two, then add one to its count for our new read. Just 2 samples? Why not inspect every server and always choose the smallest count? Sampling is cheap, and independent random choices help keep many proxies from chasing the same apparent winner. Envoy, a request proxy, uses two by default when the servers have equal weight. Here, equal weight means neither server gets a built-in preference. Ah, so we're improving the odds without asking every proxy to agree on the same server for each read. Yes. Imagine a server that returns errors immediately.

5:18 Would its unfinished count look busy? So its count stays low. Each failed read leaves active work almost at once. Our least-active rule looks for fewer unfinished reads, so it sees space and sends the broken server more work. So how should a balancer stop quick failures from attracting new work? Google's reliability book describes learning this pitfall the hard way. Their approach counted recent errors as active work, so quick failures stopped looking like spare capacity. Oh, that explains a misleading graph: the server looks quickest while the fans get nothing.

5:52 We were treating every completed request as if it were useful. Right, they changed the count. For Final Whistle we stop choosing a server for a while after repeated connection failures or errors from that score server. Then we compare unfinished reads among the servers still eligible. Those outcomes lead to a health policy that also uses readiness probes. Readiness means the server has loaded what it needs to answer ordinary score reads. Probes check that periodically; they aren't an instant picture of the next request.

6:21 For example, the code that answers a health check replies promptly. The code that serves scores waits for a link to storage, but every link is stuck. Like a working reception bell when nobody can serve you: the probe stays green, but actual reads time out when they run past the deadline. Yeah, we watch how the fan's real read went, too. Envoy calls those observations passive health checks because they come from actual requests. Our fan's still waiting. Does that green reply even come from our score app?

6:53 It says 'Other App' where we expected 'Scores'. Envoy can require the right app name, so a reply like this won't clear the server for new reads. I'd be careful about what counts as failure. A valid 'game not found' response shouldn't condemn a machine. And if the same read fails across servers, I'd check whether they all started waiting longer for the same score store. That tells me more than the error code alone. Agreed, we separate connection trouble from valid answers about the game and wait for a pattern before taking a server out.

7:28 But removing a server sends its share elsewhere, so we need to check remaining capacity. With that server out, its work moves to the other machines. For this fictional example, assume we measured how many reads each server could finish. The kinds of reads are the same as in that test, and each kind makes up the same share of the traffic. Each of our four servers can keep up with 120 requests per second. 300 original reads arrive every second. Now remove one machine. How much room is left? Three healthy machines give 360 in total, leaving 60 for variation or extra attempts.

8:06 That's spare capacity we had ready before the incident. Remove two, and we're down to 240. At least 60 of those original arrivals don't fit, before anyone tries again. Changing the destination can't erase that shortfall. Right. Taking one server out doesn't automatically get useful answers flowing again. Steering away from a server that fails fast helps only if the others have room for that work. Yeah, admission controls how much work we let in. We cap incoming work at the proxy and simultaneous reads at each server, rejecting excess early.

8:37 If the servers run out of resources or more reads miss the deadline, we revisit the measured limit. Next, our fan's read fails. A retry is another attempt. It can help if it reaches a server that still has room. Our proxy owns up to one retry for this read, toward another eligible score server, and only when the overall deadline leaves enough time. That sounds reassuring for one fan, but every fan can use that allowance at the same time. If everyone retries once, the attempt count can double. Yes. We also limit how often it can try again and how many extra attempts can run at once. That 60 reads per second is room for both variation and extra attempts.

9:23 Set some aside for variation, then divide the retry allowance among the proxies. A token is permission for one extra attempt, spent from that proxy's share. No token, no extra attempt. Yep, and the app retries, and the proxy retries, and the code that calls the score store tries again... ...until one fan has commissioned a small crowd. We assign this score-read retry to the proxy and prevent independent replays of that operation in other layers. Google's overload guidance makes retry ownership explicit.

9:50 Right, and even after the old attempt runs out of time, it can keep using resources. Where the next service can act on them, we pass along the deadline and send a stop signal. The work still counts until it actually ends. Exactly. The score read doesn't change data, but every extra attempt still costs capacity. If we were taking a payment, we'd also need to prevent charging the fan twice. Now suppose all four score servers wait on the same slow score store. Their request failures look similar. Temporarily excluding one after another would leave fewer servers waiting on the same slow store.

10:29 A storage lead might say, 'Give the request more chances. The next connection could work.' What evidence would justify spending that extra attempt? Evidence of available capacity somewhere the attempt can reach. If every server is at its limit and nothing else can help, we stop new retries, let less work in, and send a clear unavailable response. Yeah. Otherwise we're adding more people to the same waiting room and calling it recovery. I think we spare the fan a useless wait by rejecting here. Time remains on their clock, but another try sends us straight back into that stall.

11:04 That's the tradeoff. Rejection is still a user-visible failure, which means we haven't recovered the score service. We've bounded the damage while we fix the store or add capacity. Now suppose the store can answer again. Our fan refreshes just after a server leaves. The proxy still has its address. The first attempt gets no answer. The clock is still running. Does the proxy get one more try? When the registry sends its next list, it will leave out that address. The version number keeps an older list from replacing a newer one.

11:37 Our saved list expires while the fan is waiting. That leaves no trusted list for another attempt. Addresses can change hands while we wait, so a guess could send our fan's request to whatever moved in. So even with time and a token, do we stop that retry? Yes. The proxy sends unavailable without contacting another server. Once a fresh list arrives, new requests use it. Reads already running elsewhere can finish. Extra copies of the registry help keep updates coming. Next, plan a replacement by considering the old server first.

12:12 A server announces draining: stop sending new reads, finish the ones it already has within their deadlines, then shut it down. Right, Amazon's cloud documentation describes what happens when you take a server out of rotation. If we shut it down first, we cut off fans whose score reads are still in progress. Exactly. For the replacement, finish loading the data and opening the links it needs before it announces that it's ready. Then start it on a few reads as a trial and send more as useful answers come back.

12:46 Hold on. That new server has no read underway yet. If we sample it beside a busy server, won't it win the comparison before it's warmed up? That's why readiness and admission are separate. Once it's ready, let it try the fan's next read. It returns the score in time, so we give it a little more work. Its warmup limit holds the replacement to a small share while we see how it performs. Ah, we open the door a crack without inviting the entire stadium through it. Look at those next reads. What if they start taking longer?

13:21 We back off before they pile up. As long as useful scores keep coming back on time, we can send more work. If all the servers are still warming up, shifting the reads around won't give us a machine ready for its full share. Now return to the complete map. Suppose we've restored useful capacity, and our fan asks again. An available proxy receives the request through whichever network entry is still working. If there's no room, does this read stop at the proxy? Yes. If there's room, the proxy picks a ready server.

13:55 It fetches the score and update time from the shared store. Both go back through the proxy and the entry to the fan. The next request might take a different path. Registry updates, probes and real outcomes shape future choices; draining and warmup control replacements. If that read fails while the fan is still waiting, have we got both time and unused capacity for another? Only with time and retry allowance left, to a different eligible destination. If the servers can't serve it, we return the error instead of creating more unfinished work.

14:28 Now test that map under failure: slow one server, make another fail quickly, then slow their common data source. Track original reads, total attempts, and useful answers completed within the deadline separately. Add a replacement during the test. Check where traffic goes, how much traffic it receives as it warms up, and whether existing requests survive a drain. The healthy-looking graph we distrust is equal assignments with falling successful reads. Right. Load balancing helps when it finds capacity that can finish useful reads.

14:57 When that capacity is gone, a bounded failure keeps the next fan's request from joining an uncontrolled pile. Thanks for listening to Learning Podcasts.