nanoMMO

Chaos testing and site reliability engineering for an indie MMO

We freeze Redis, MySQL and the login server, and kill the saver, while bots play our indie MMO, and find a sword that existed twice and players dropped while our numbers said all was well, with Google's SRE book as the guide.

Watch on YouTube

Chaos testing and site reliability engineering for our indie MMORPG, in staging, with bots. We measure what players get as three service level indicators, ticks on time, logins answered and saves fresh, make them objectives with error budgets in Prometheus, and alert on how fast a budget burns, as Google's SRE Workbook does. Netflix's Chaos Monkey switches off servers in production at random; we run one instance of each world, on one server, so we break things on purpose in staging instead, with a tool that freezes Redis, MySQL, the login server or the telemetry, or kills the saver, one at a time, while bots play. Before any fix, a five-second Redis freeze froze every player and our tick indicator counted one tick missed, twelve seconds dropped everyone, a trade during a one-second Redis outage copied a sword, and a saver killed mid-save left 88 players' rows behind in MySQL. We go through the SRE book chapter by chapter, with what we do of it and what we don't, and the next episode fixes most of what we found. This is episode 88 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.

Topics: DevOps, Testing

The code

Each step on screen is a tag in the game's repository. The code at the end of the episode is ep088/end.

  1. 7:14ep088/step-1What players get, measuredBrowseChanges
  2. 20:36ep088/step-2Error budgetsBrowseChanges
  3. 35:39ep088/step-3A tool that breaks thingsBrowseChanges

Transcript

Intro

0:00 Redis, where all our worlds save their players, is frozen here for twelve seconds, in the second of these two purple marks, and the world the bots see gets about fifteen seconds old. When Redis comes back, Ember drops all one hundred and one of its players. No alert fires, and the error budget for ticks on time still says one hundred percent left. We build that budget in this episode, and it took a frozen Redis to show me it counts the wrong thing.

0:27 Welcome back to the series, and to episode eighty-eight, where we're going to break our own game on purpose, freezing Redis, MySQL and the login server, and killing the saver, while bots play, to see what players would go through. It turned up a sword that existed twice, along with a few things I'm not too proud of, and I think that makes it one of the more useful episodes so far. This one stops at what we found, and the fixes are in episode eighty-nine.

0:53 The code for all of this is open source, on GitHub, and the series' website links to it. Each episode has a page there as well, with the code at each of its steps.

Last time

1:03 A quick reminder of where we left off. Our servers started sending spans to Alloy, which hands them to Tempo, for Grafana to show. We wrote a small tracer of our own that speaks OpenTelemetry, and a player's way in became one trace across two servers, carried in the token's record in Redis. Trades and deploys got traces too, log lines got their trace ids, and Alloy started keeping the slow and failed traces. And the first traces caught MySQL reads taking two round trips, so a connection now keeps the reads it's prepared.

Why break it on purpose

1:37 I wanted to make this one because, over eighty-seven episodes, the game has picked up quite a few moving parts, and they all lean on each other. Twenty times a second, a world's tick saves the players who changed, into Redis, and renews their leases there once a second. The saver takes them on to MySQL, the login server needs both before anyone can play, and Consul tells it which worlds there are. We've tested each piece, with real MySQL and Redis in the tests, and we've load tested the lot. What I've not done is ask what our code does when one of them stops answering while people play. A crash is the easy case, as a closed connection fails straight away. A process that hangs, say on a slow disk or a machine that's swapping, just goes quiet, and I had no idea what happens then. Episode seventy-nine stopped a world with SIGSTOP for a few seconds, and that was the first taste. I'd much prefer to find the rest out on a quiet afternoon, on purpose, than from players at three in the morning.

Chaos Monkey, and why not here

2:37 The best-known version of this idea is Netflix's Chaos Monkey. It switches off instances of their services in production, at random, so the engineers have to build services that carry on when one goes. It runs on working days, and by default from nine in the morning to three in the afternoon, Los Angeles time, which I think is a nice touch, as that's when people are around to fix things. I like it a lot, as it takes a sentence like "we'd survive losing a server" and makes it something that happens to you on a Tuesday afternoon.

3:07 Chaos Monkey is part of what grew into chaos engineering, and the Principles of Chaos Engineering put it as an experiment in four steps. You say what the steady state is, as a number that shows the system working. You guess it'll hold, in a control group and in an experimental group, while you bring in something that goes wrong in real life, such as a server that crashes. And you try to prove the guess wrong. Our control group is going to be Frost, the second world, which the bots don't play in. Further down, they say to run the experiments in production, on real traffic, and to keep the blast radius small.

3:44 Chaos Monkey isn't something I'd run on our own servers, though. Netflix runs many instances of each service behind a load balancer, so when the monkey takes one away, the rest carry its traffic, and nobody should notice. We run one instance of each world, on one server, so there's nothing to take over: any kill there is an outage for everyone in that world. Killing things at random in production would be hurting our players at random, so we won't. Instead, we'll run deliberate experiments, one fault at a time, against our own stack on this machine, which is our staging, with bots as the players. A disclaimer, though: staging is one machine, so it tells us what our code does when Redis or MySQL fails, and not what a real network does.

The SRE book, and where we stand

4:27 The other half of today comes from Google's book, Site Reliability Engineering, which you can read for free on their site. What I took from it is that reliability is a target you choose, and then an engineering problem like any other. Quite a few of its chapters describe things we've done already without calling them that, so I went through them and checked where nanoMMO stands. Service level objectives we have none of yet, which is today's work. Monitoring we've done a lot of, from episode four's tick duration on, along with alerts on symptoms, such as a world falling behind. Release engineering is mostly there, with pinned versions, builds in Docker and a list of changes for each version, though the server builds its own images, so what runs there isn't the binary CI tested. Being on call is easy to score: our alerts go to an inbox on the server, so nobody is paged. We've not written a single postmortem, after any of the bugs so far. Testing we have, from unit tests to load tests and netem, short of breaking things on purpose. Cascading failures is where today's bugs live, as it's about deadlines and failing fast, which our calls to Redis and MySQL don't have. Data integrity is the one I'm least happy about, as MySQL is our source of truth, and we have no backups at all. And our deploys hand players over without downtime, but there's no canary: each world gets the new version, one after the other, without a moment to compare.

5:54 So, here's the plan, over this episode and the next. We start by measuring what players get, and turn those numbers into objectives with error budgets, with alerts on how fast a budget goes. Next is a tool that puts in one fault at a time while bots play, and we break the game with it before fixing anything, to see how it really behaves. The fixes, the experiments once more and our first postmortem come in the next episode. Right, let's start with what we're going to measure, and why.

What players get, measured

6:24 An objective needs a number that says how the game is doing for players, which the book calls a service level indicator. Ours are each a share of what went well, and I picked three, each one something a player would feel. The first is ticks on time, in each world. A world owes twenty ticks a second, and a tick that runs within its fifty milliseconds is a good one. The second is logins answered: requests to the login server answered within five seconds, where a wrong password counts as answered, as that's the player's mistake and not ours. The third is fresh saves, the share of the time MySQL is no more than thirty seconds behind Redis, as that's what a player would lose if Redis went. And on the side, the worlds count the players they let go of without saving them, by why, which since episode seventy-nine has gone into their logs and nowhere else.

7:14 As usual, the version goes up first, in the top CMake file, to nought point eighty-eight, as each episode's code is a minor version while the major is still nought. In the server's CMake file, the login server links prometheus-cpp's pull library, which has the little HTTP server that Prometheus scrapes, as the login server hasn't served any metrics until now. In the metrics' header file, the world's metrics get add dropped, for a player the server let go of without saving them, by why. The why is their lease, which isn't this instance's any more, or their trade, which couldn't be saved. It's kept as a family of counters, which in Prometheus is one metric name with a counter for each value of its labels, here one for each why. Below it comes a class for the login server, login metrics, which times how long it took to answer each request, by what came of it. Its constructor registers the metrics in a registry of its own. Observe request takes the result and the time it took. A result is ok, or refused, when the reply says why not, such as a wrong password, which is the player's mistake, or failed, when the servers it needs couldn't answer, which is ours. Like the world's metrics, it hands out its registry for the exposer to serve, and it keeps a family of histograms, one for each result. In the metrics' source file, the logins get buckets of their own. A login takes tens of milliseconds, most of it hashing the password with Argon2id, and one that takes over five seconds is one the player's given up on. So the buckets go from fifty milliseconds to ten seconds, with one at five, which the objective is going to count against. In the world's metrics, after the histogram of how long stories take, the new counter is built like the others, with prometheus-cpp's builder. It's nanoMMO players dropped total, with help text that says players let go of without saving them, by why. Both whys are added at zero as the metrics are made. That way, the first player dropped counts as one more than none, which a rate or an increase can see. A series that turns up already at one looks to Prometheus as if it had been one all along, and the drop would be lost. Add dropped finds its why's counter in the family, and adds one to it. The login metrics have a registry of their own, made as a shared pointer, as the exposer takes a weak pointer to it, which has to come from a shared one. Its histogram family is called nanoMMO login duration seconds, the time to answer each request, by what came of it. Each result is added with the buckets from the start, at zero, so a rate of failures is zero before the first failure, and not missing. That matters for the logins' objective, which divides the failures by all the requests, and would come out as no data at all on a quiet day without it. The login server gives its version too, which helps when we want to know which servers a deploy has reached. It's a gauge called build info, with the version in a label and a value of one, the way the worlds report theirs. Observe request finds the result's histogram, and observes the time, converted to seconds as a double with a chrono duration, as Prometheus wants seconds. The login server's header file brings in the metrics' header, for the class we've just made. Outcome turns what handling a request gave back, a reply or an error, into one of those three words. A reply that says why not, as for a wrong password, is refused, as that's the player's mistake, and failed is when the servers couldn't answer it. It's a free function, so the tests can call it on its own. Create takes the login metrics now, as each request it answers goes into them, with how long it took. The constructor takes them too, and the server keeps a reference to them, as main owns them and outlives the server, so a reference is safe here. In the login server, a request we couldn't handle, say as MySQL was down, failed, and otherwise the reply decides between ok and refused, from its own ok field. Create and the constructor pass the metrics along, into the member, as the header said. In accept, the lambda that runs for each message decodes the request, and once it has, we note the time, on the steady clock, which doesn't jump when someone sets the machine's clock. The list of worlds is answered on its own, and it's ok, or failed when Consul couldn't list them, with the time since the start. For the rest, we take the outcome before the reply is moved out of what we handled, and observe the request once its reply is on its way. Keep this in mind, as we're going to come back to it: the time runs from when the server read a request, so a request it doesn't get to read isn't timed at all. In the login server's main, we bring in exception and optional from the standard library, Prometheus' exposer, and the metrics' header. Main makes the login metrics, how long each request took to answer and what came of it, for Prometheus. It serves them on port ninety-four sixty-seven, unless nanoMMO metrics port says otherwise, next to the saver's ninety-four sixty-six. The exposer is prometheus-cpp's small HTTP server, and as in the worlds' main, it's made inside a try, in an optional, which holds an exposer or nothing yet. The exposer throws when its port is taken, and our code doesn't use exceptions, as the style guide has it. So the catch turns that into a line on standard error, with the port and why, and an exit code of one. The exposer serves the registry at slash metrics, its default path, and main says so in its log. Next comes the tracer, from last episode, with one change. Tracing takes the registry as well, so the login server counts the spans it drops, like the worlds and the saver do. The login server is created with the metrics to fill in. In the compose file, Prometheus gets two changes, to keep enough history for a month of objectives. Prometheus keeps thirty-one days of samples, over its default of fifteen, as an objective over thirty days needs all thirty of them, and a day to spare. They go on a volume of their own, so they outlast the container, and the volume joins the list at the bottom. And in Prometheus's config, a new job, nanoMMO login, scrapes the login server on port ninety-four sixty-seven, inside Compose.

13:25 Now the saves, and in the players' header file, a new function, unsaved, says how many players are marked unsaved in Redis, waiting for a flush. In players, that's S card on the set of unsaved players, which counts what's in it without reading any of it, in one quick round trip. And Abseil's simple A to I reads the count, which our Redis class hands back as text. A count that isn't a number would be an internal error, as Redis shouldn't send one. In the saver's main, we bring in Abseil's clock, for the time now. The saver's metrics become a struct of three gauges, what the saver's metrics say, so flush can take them together. A gauge is a number that goes up and down, as opposed to a counter, which can't go down. They're references, as the registry owns the gauges: the duplicates from episode seventy-nine, when the last good flush ended, and how many players are waiting. Flush takes the struct now, in place of the duplicates' gauge alone, and it reads a lot like before. When a flush ends without an error, the flushed gauge gets the time, in seconds since nineteen seventy. That's when MySQL last had all that Redis had as the flush began, so an alert can work out its age. An age gives away a saver that's failing and one that's stuck, where a count of errors would miss the stuck one, and Prometheus's up series says when it's gone. The line about how many it saved stays as it was, inside the success branch. After each flush, good or not, the unsaved gauge gets how many players changed since, waiting for the next flush, as long as Redis can say. A saver that's falling behind shows up there first, as a number that keeps growing. In main, the gauges are made with designated initialisers, which name each field of the struct as it's set, in order. The duplicates' gauge is built as it was before, with its name and its help text, and the two new ones follow it. The second is nanoMMO saver flushed timestamp seconds, when the last flush that saved everyone ended, which is a timestamp in seconds, as Prometheus names them. The third is nanoMMO unsaved players, the players in Redis waiting for the next flush. The flushed gauge starts at the saver's start, so its first good flush has as long to happen as each one after it, and a saver that's just started doesn't look stale. Both calls to flush take the gauges now, the timer's and the last one as the saver stops. In the server's header file, drop takes a kind of why, as a C string, lease or trade, for the counter's label, so each drop is counted by its kind. In the server, the two drops of a trade that couldn't be saved say trade, and the two of a lease that isn't this instance's any more say lease. And drop counts each player it lets go of, right after it logs who and why.

16:12 Next, a handful of quick tests, to pin down what we've just measured, starting with the metrics. The first counts the players dropped, by why, on a world's metrics of its own, made just for the test, so nothing else has dropped anyone yet. To start, we scrape the metrics, with a helper the file has already, which writes them out as Prometheus would read them. Both whys are there before anyone's dropped, at zero, and the file's has helper looks for each line in that text, the lease's at nought, and the trade's at nought as well. The metrics added both as they were made. We drop two players for their lease, and one for a trade, and scrape again, and the text says two for the lease and one for the trade, which is what a rate in Prometheus would see go up. The second gives the login metrics a request that was ok after sixty milliseconds, and one that failed after seven seconds, and writes out what Prometheus would scrape, with prometheus-cpp's text serialiser. The ok one isn't in the fifty millisecond bucket, and it is in the hundred millisecond one, as a histogram's buckets count what's at or under their bound, so each bucket holds the ones below it as well. Seven seconds is past the five a player waits, so the failed one isn't in the five second bucket, and it is in the ten. It's too slow as well as failed, though the objective counts it once, as a failure. We note above it that nobody's been refused yet, which shows up as a count of zero, and not as a missing series, as we added each result at zero. In the login server's tests, we bring in Abseil's status, to make an error by hand. This test tells the player's mistakes from ours. With an account made, logging in with the right password is ok, and a wrong one is refused, as it's answered, and the mistake is the player's. And MySQL not answering, which we make as an unavailable error, is a failure, which is ours. Expect string equal compares the words, as they're C strings, where expect equal would compare the pointers. In the players' tests, we count the unsaved players first, as other tests share this Redis. Saving a player into Redis makes the count one higher, and after a flush it's back to zero. And the server's test of a lost lease, from episode seventy-nine, brings in the text serialiser too, to read the metrics as text. That test plays a player on two instances, and the first loses the lease to the second. It now also checks that the drop was counted, as a lease.

18:39 Let's build at this step and run those five tests. A few of them need MySQL and Redis, so Compose starts those first, along with the migrations, and the build takes a few minutes, as the image builds the whole game. The player's mistakes are told from ours, and all five pass.

Error budgets

18:56 Each number needs a target, and the book's first appendix has a table of what each level allows. At ninety-nine point nine percent, a month allows forty-three point two minutes of things going wrong. That's the error budget: what's left over from the objective is ours to spend, on a deploy that goes badly, an experiment, or a bug, and once it's spent, we've let players down. This is the idea from the book that won me over, as it turns reliability into something with a balance you can check, and argue about.

19:27 So, here are our three objectives, each over thirty days. Ninety-nine point nine percent of each world's ticks on time leaves a budget of fifty-one thousand, eight hundred and forty ticks a month, as long as forty-three minutes of a world frozen. Ninety-nine percent of logins answered in time leaves one percent of them, and the saves fresh ninety-nine point nine percent of the time leaves forty-three minutes of stale saves. For one maintainer and one server, I think that's about right. A few minutes of trouble a month is something players forgive, and a few hours isn't. To alert on a budget, the SRE Workbook, the book's follow-up, watches how fast it's going, its burn rate. At a burn rate of one, a month's budget lasts the month. At fourteen point four over the last hour, that hour spent two percent of the month's budget, and at that pace it's gone in about two days, so that pages someone. Six over the last six hours, five percent of it, pages too, and one over three days, ten percent, is a ticket, for a slow leak. Each alert also needs a short window, a twelfth as long, to be burning as fast, so it stops soon after the burning does, and doesn't page for an hour about something that's over.

20:37 The objectives go in Prometheus's rules file, alerts.yml, after the alerts we've had since episode thirty, in a new group, named for our service level objectives. What players get, as our objectives measure it, is worked out here as the share of what went wrong, over each window the alerts are going to compare. These are recording rules: Prometheus works each one out on a schedule, and keeps the result as a series of its own, so the alerts and the dashboard read a number, instead of doing the sums again each time. A recorded series is cheap to read, so the dashboard can draw a month of it without redoing a month of sums. A rule can also build on another, as the longer windows further down build on the minutes. The alerts sit in this group too, as a group's rules run in order, so each alert reads what the rules recorded at that evaluation. With two groups, they raced each other, and the tests failed about one time in four, which took me a while to understand. Each window is as much of it as Prometheus has, which matters in a moment. The group's rules are worked out once each thirty seconds. First comes each world's share of ticks that weren't on time over the last minute, named the way Prometheus suggests, with the labels it keeps, the metric, and what was done to it over the window, separated by colons. It counts the ticks that didn't run within their fifty milliseconds, out of the twenty a second a world owes, as K ticks per second is twenty in the world's main. In a handover, two instances tick, so it's capped. A rate divides by its whole window, so a series younger than the window would look like a world that missed its ticks, and a world counts once it has a minute's samples. And a world that's down, or gone from Prometheus's targets, though it was there in the last day, misses all its ticks, the way World Down has it since episode thirty. The expression is a long one, so it's a folded block, over several lines. It's one minus the share that was on time. We sum by world, as a world's instances each have their series. Inside the sum is the rate of the tick histogram's bucket at nought point nought five, which counts the ticks within fifty milliseconds, as a rate a second. The and keeps a series once it has twenty-nine samples in the minute, at two seconds a scrape, which is all but one of them. The sum is divided by twenty, and clamp max caps the share at one, as two instances in a handover tick forty a second between them. The or brings in the worlds that have no ticks at all, at a share of one, for each world Prometheus has had a series for in the last day, unless it's up now. The longer windows the alerts compare come from those minutes, starting with five minutes. Each counts once Prometheus has watched for half of it, as a Prometheus that's just started has a few minutes' samples, and those can't speak for an hour. It's the average of the minutes' shares, once there are five of them to average. As the rules run twice a minute, half a window is half its samples, five of the ten in five minutes, and thirty minutes come next, with thirty of their sixty. That rule about half the window came out of a mistake. With plain rates, a Prometheus that had just started read an hour's rate over its first five minutes as a world that had missed nearly all its ticks, and all the alerts fired as the stack came up. The hour needs sixty samples, which is half an hour of watching. Six hours need three hundred and sixty, which is three hours of it. Each of these rules is the one before it with a longer window, and more samples to wait for. Three days need four thousand three hundred and twenty, which is a day and a half of watching before that window counts at all. Three days is the window of the ticket, for a slow burn, so waiting for it costs us little. The logins' share comes next, with its own windows. What counts as gone wrong is a request the login server didn't answer within five seconds, or couldn't answer at all, out of all of them. A wrong password is answered, as it's the player's mistake and not ours. And this next part bit me, while I was writing it. Prometheus three writes each bucket's bound as a float as it scrapes it, so five is five point nought, and a rule that asked for le equals five matched nothing at all, without a word of warning. So it's one minus the requests answered within five seconds, leaving out the failed ones, whatever their time, out of all of them. So a refused login counts as answered, and a failure or a slow answer is what counts against us. Thirty minutes, an hour and six hours come next, each its own rule with its own window, written out in full, as Prometheus has no way to loop over windows. There's no three days for the logins, as they don't get a ticket, just the pages. And there's no half-window count here either, as the logins' share divides one rate by another over that one window, so a Prometheus that's just started shortens both alike. The saves are different, as there's no stream of events to count, just a saver that's either keeping up or not. It's one while MySQL is more than thirty seconds behind Redis, that is, while the saver's last good flush ended over thirty seconds ago, or the saver's gone, and nought while it isn't. Its average over a window is the share of that time the saves were stale. It's the flushed gauge's age, the time now less when the last good flush ended, compared with bool, so it's one or zero, and not left out. It's also one when the saver's gone, though it was there in the last day, as there's no gauge to read then, and a saver that's gone saves nothing. The on with empty brackets matches the two sides without any labels, as both are a single number. Its averages follow, over five minutes, thirty, an hour and six hours, once Prometheus has watched for half of each, with the counts the ticks have. Each fits on a single line this time, and the average of a flag is already a share, with nothing to divide. An hour's average of nought point five would mean MySQL was behind for half of that hour, which at that pace spends a month's budget in under an hour and a half. Now the alerts, and over the first, we write down what each budget is: nought point one percent of the ticks, one percent of the logins, and nought point one percent of the time for the saves. These alert on how fast a budget's going, where a burn rate of one spends it in thirty days, as the Workbook's multiwindow, multi-burn-rate alerts do. The page comes at fourteen point four over an hour, which spends two percent of a month's budget in that hour, or at six over six hours, five percent. Each needs its short window to burn as fast, so the alert ends soon after the burning does. And the ticket comes at one over three days, ten percent. Ticks burning budget divides the hour's share by the budget, nought point one percent, which gives the burn rate, and fires past fourteen point four. The last five minutes have to burn as fast, or it's past six over six hours, with the last thirty minutes. There's no for clause, as the windows already take their time. Its severity is page, as a label, which Alertmanager could route on, and its summary says which world, and how many times as fast as its budget allows it's missing ticks. The value is the burn rate, and printf rounds it to a whole number. Its expression is shorter, as it has a single pair of windows: the three days' burn rate past one, and the last six hours' past one too. Ticks draining budget is the ticket, at one for three days, and for the last six hours, with its severity a ticket, and its summary to one decimal place, as a slow leak has a small burn rate. A ticket is something to look at this week, and not at three in the morning. The logins' alert pages on their budget, one percent, over those four windows, which is the ticks' expression with the logins' shares in it, and their one percent in place of nought point one. It's inside brackets, so the next part applies to all of it. Its severity is page as well, as players who can't log in is something someone should hear about, whatever the hour. A handful of logins can't tell a bad hour from bad luck. One failure out of three logins would read as a third of them failing, which is thirty-three times the budget. So it waits for ten logins in the hour before it says anything. Its summary says how many times as often as their budget allows logins fail or take over five seconds. And saves burning budget pages for the saves, over the hour and five minutes, or six hours and thirty, like the ticks. Its summary says how many times as long as its budget allows MySQL has been over thirty seconds behind Redis. That's the first alert that a saver that hangs would set off, as a hung saver writes nothing to its errors, which the old alert on its logs needed.

29:07 Rules like these are easy to get wrong without noticing, as I found twice already, so they get tests, in promtool's test file, beside the alerts' tests from before. Each test makes up a few series, and says what the rules and the alerts should make of them at given times. In the first, Frost freezes after an hour, while Ember ticks on time all along. Ember gets forty ticks between scrapes two seconds apart, and Frost's counter of ticks on time stops after an hour. There's a sample each two seconds, as Prometheus scrapes our worlds. In promtool's notation, Ember's bucket starts at nought and goes up by forty a sample, two seconds of ticks on time, two thousand seven hundred times, which is ninety minutes. Frost ticks like Ember for an hour, and then its count of ticks on time stops where it is, as a frozen world's would. Both worlds are up all along, as a frozen world still answers its scrapes from another thread. A promql expression test checks a rule's value at a time: at seventy-five minutes, Ember's five-minute share of missed ticks is zero, and Frost's is one. Each expected sample names its series by the rule and the world, so a typo in a label fails the test too. An alert rule test checks which alerts fire, with their labels and their summaries, at each time it names. Before the freeze, at fifty-five minutes, nobody's paged, and at seventy-five minutes, Frost pages, and Frost alone, at two hundred and forty-two times as fast as its budget allows. Ember, which carried on, doesn't page, so we know the alert is about the freeze, and not about the test's setup. Next comes a freeze of six seconds, which misses a hundred and twenty ticks. Over five minutes, that's about twenty times the rate the budget allows, well past fourteen point four, but over the hour, it's about two, so nobody should be paged. Times two gives three samples, as the count is how many come after the first, so Frost's count stands still for three samples, six seconds, and then goes on from where it was, forty a sample. The test checks that the five minute burn rate is past fourteen point four, with bool, so it comes out as a one, and that the hour's isn't, so it's a nought. And no alert fires, which is what the long window is for: a blip that's over is no reason to wake anyone. In the third, Frost goes away altogether, and Prometheus forgets its series. It should then miss all its ticks, and not none. Its bucket ticks on for an hour, as before, and promtool writes the forgetting as stale. Its up series goes stale with it, so Prometheus has no target for Frost either, which is what happens when Consul forgets a world. Frost misses all its ticks, and pages at two hundred and fifty times its budget, where a world that vanishes would otherwise miss none, as it has no series left to miss them in. The fourth is a Prometheus that's just started. It has a few minutes of samples, not an hour's, and none of the windows should mistake that for a world that missed its ticks. Frost's series run for ten minutes, a sample each two seconds, with its ticks all on time, and its up at one all along. There's no Ember in this one, as a single world is enough to show it, and the test is about the windows, and not about the worlds. At three minutes in, neither the page nor the ticket fires. That's the test that would have caught the alerts firing as the stack came up, had I written it first. In the fifth, the saver flushes as the test goes, its last good flush at most fifteen seconds old, and an hour in, it stops flushing. The interval is fifteen seconds here, coarser than the real saver's five, which is plenty to see thirty seconds go by. Its flushed timestamp goes up by fifteen seconds a sample, two hundred and forty times, and then stays at an hour. At thirty minutes, the saves' indicator is nought, at sixty-two minutes, two minutes after the last flush, it's one, and by seventy-five minutes, the saves' alert pages, at two hundred and forty-two times its budget. Before the hour, at fifty-five minutes, it doesn't, so it's the saver stopping that sets it off. And the last test is about the logins' alert. It has twenty logins a minute, two of them refused, as a wrong password is, which is fine, and from an hour in, half of them fail. Over the hour, that burns the budget about twelve times as fast, short of fourteen point four. Over the six hours, which have had seventy-five minutes, it's about nine, past six. The interval is a minute this time, as logins come slower than ticks. Each result has a count and a five second bucket, which here are equal, as nothing's slow. The ok ones go up by eighteen a minute for an hour, and by eight after that, so the ok logins drop from eighteen a minute to eight as the failures start. The refused ones go up by two all along, count and bucket alike, as a refusal still counts as answered, and the failed ones stay at nought for an hour, and then go up by ten a minute. So from an hour in, half of them fail. At fifty-five minutes, nobody's paged, and at seventy-five, the six hours' window pages, where the hour's misses. There's a new dashboard too, nanoMMO reliability, which I'm pasting in, as it's over six hundred lines of Grafana's JSON. Along the top, there's each budget left this month, from what Prometheus has. Under them are the burn rates over the hour and the last five minutes, with lines at six and fourteen point four. Below those come the ticks, the logins and the saves as they go, what the bots see, who was dropped, the errors by service and the alerts firing, with a row of its own saying nothing is, when that's so. The purple marks are the chaos experiments, which the next step's tool adds as it runs them.

34:38 Compose has a service called alerts for this, which runs promtool's test command on the rules, in Prometheus's own image, and all the tests pass, the six new ones and the alerts' tests from before.

A tool that breaks things

34:50 Each experiment the tool runs follows the principles' steps. It checks the steady state first: both worlds at twenty ticks a second, the bots' world under two hundred milliseconds old, all the bots in, no item twice, and the saver's last good flush under fifteen seconds old. While the fault lasts, ten more bots log in, as players keep coming. The fault goes in, as a container frozen, stopped, slowed down or killed, and it comes out on time, however the script ends. The tool waits for the steady state to come back, and times how long that takes. And it prints what the metrics, logs and traces saw, as JSON, and marks the experiment on our dashboard. It refuses a deployment's stack, and it puts in one fault at a time, which I'd keep that way, as two at once would leave me guessing which one did what.

35:40 Before the tool, the bots need a change. Until now, a bot that lost its world, say as the world dropped it, stayed gone, so after an experiment the swarm didn't fill up again, and nothing could tell us the game was steady. So the bots are going to log in again, as a player would, and the new backoff header file brings in chrono and random for it. Backoff says how long a bot waits before it logs in again, after a number of tries that failed in a row. It takes a Mersenne Twister, one of the standard library's random generators, which the swarm owns. It's a second to start with, twice that after each failure, up to thirty, and of that a random part, from half to all of it. That way, bots that were dropped together don't all come back together, and hit the login server in one go. In backoff's source file, after its header come algorithm, for clamp and min, then chrono and random. The function's body is a few lines, in nanoMMO's namespace, with two constants first. The first wait is a thousand milliseconds, and the longest is thirty thousand. The wait is the first one times a one shifted left by the number of failures, which doubles it each time, capped at the most. Clamp keeps the shift between nought and five, as past five doublings it's at its most already, and a shift much further would overflow the integer. A uniform distribution picks a number of milliseconds from half the wait to all of it, the random part, and that's the wait that comes back. The bot's CMake file adds backoff to the bots' core library, and its test to the bots' tests. In the bot's main, we bring in random and the backoff header file. A bot that's logged in gets five seconds to reach its world, and one that's lost its world takes that long to notice, before it logs in again. At most five bots log in again each second. A swarm logs its bots in on the thread they play on, so each login holds up the others, and a hundred at once would stall the whole swarm. Each login waits for the login server's answer, and the bots' frames wait with it. The swarm's random generator is seeded with its first bot's number, so a second swarm, with numbers of its own, waits differently. In add, a bot that logs in gets its grace before anything can retry it, and one that couldn't log in waits a first backoff, with one failure counted. Rejoin is new, for a bot that's lost its world, say as a world that stalled past its leases dropped it, or one that couldn't log in at all. It logs in again, as a player would, after a wait that doubles with each try that fails, and a few a second at most. It goes through the bots until it's tried five, and a bot that's connected gets its grace again, from now. A bot that's moving to a world's next instance, in a handover, is left alone, and so is one whose wait isn't over. Otherwise it logs in again, under its own number, which is its own account and character, as a bot's name comes from its number, and when that fails, it says so on standard error, and waits longer next time. Once it has a token, the old bot leaves, and goes into the list of bots that have left, which stays until the swarm stops, as its handlers may still be waiting on its sockets. A new bot takes its place, with its number, and starts with the token. It's a fresh bot, as the old one's connection and what it knew of the world are gone, and the count of those that came back goes up by one. And the swarm says how many came back. Report, which runs once a second, calls rejoin after it's counted who's connected, and after follow, which takes bots that are moving to a world's next instance. Among the swarm's members, each bot has a retry, by its place in the list of bots: when it may log in again, and how many of its tries have failed in a row. It's a small struct with two fields of its own. The first is a time point on the steady clock, which rejoin compares with now. The second is the count of failures, which starts at none. The retries are kept in a vector beside the bots, and the swarm keeps its random generator next to them. In the bot's metrics, the histogram of how old a bot's newest world is gets more buckets. It had steps of ten milliseconds up to two hundred, and a few after that up to five seconds. Now there are more between one and five seconds, at one and a half, two, three and four, and past five, at six, eight, ten, fifteen, twenty and thirty. A stall runs well past five seconds, and without them, all of a stall's ages would land past the last bucket, where a percentile can't tell five seconds from twenty. And in the compose file, the bots take their first bot's number from bots first, one unless it's set, which the bot's main reads as its fifth argument, so a second swarm can have numbers of its own. Last, two quick tests for the backoff, which bring in chrono, random and set, along with GoogleTest, and a using declaration for milliseconds, so the checks read more easily. The first runs it a hundred times, with a generator seeded with one. A first wait is between half a second and a second, after two failures it's between two and four seconds, as that's the second doubling, and after forty, it's between fifteen and thirty. That's where the clamp comes in, as a shift of forty would overflow. The second is the reason for the random part. A hundred bots, each after three failures, as if they'd been dropped together, put their waits into a set, which keeps each value once. It ends up with more than ninety different waits, all between four and eight seconds, and spread over more than three of them, so the bots come back over a few seconds, and not all at once.

41:08 The backoff's tests need nothing but the bots' code, so dash dash no deps keeps MySQL and Redis out of it, and the game builds at this step first, as before. Bots dropped together come back spread out, and both tests pass.

41:23 Now the tool, a bash script in the bench folder, next to the load and loss scripts we've had for a while. It starts with the line that says bash runs it, and set dash e u o pipefail, which I put at the top of each script. Dash e stops at the first command that fails, dash u makes a variable nobody set an error, and pipefail fails a pipe when any command in it fails, and not just when its last one does. Above it goes what the script is for. It runs chaos experiments, one fault at a time, put into the Compose stack on this machine while bots play, and taken out again, and it prints what the metrics, the logs and the traces saw, as JSON. It's for staging: it refuses a deployment's stack, as production is not where we break things on purpose. You run it with the experiment, and if you like, how many seconds the fault lasts, and restore undoes whatever a run that was killed left behind. There are eight experiments, each with how long its fault lasts. Redis pause freezes Redis with docker pause, for five seconds unless you say otherwise, and Redis down stops it and starts it again. MySQL pause freezes MySQL, and MySQL slow delays the answers MySQL sends by fifty milliseconds, with netem. Saver kill hangs MySQL and has the bots leave, so the saver takes them to it, and kills the saver mid-flush. Login pause freezes the login server, consul down stops Consul, and telemetry pause freezes Alloy, so no spans or logs get through. A hundred bots play, arriving over thirty seconds, and ten more log in while the fault lasts, as players keep coming. Keep bots leaves the swarm running for the next experiment, and the addresses of Prometheus, Loki, Tempo and Grafana can be set, with this machine's ports by default. The first argument is the experiment, and the colon and question mark in its braces stop the script with the usage line when there isn't one. The rest come from the environment, where colon dash gives each one a default when it's unset or empty: a hundred bots, a ramp of thirty seconds, ten probes, and the four services' addresses on localhost. That's Prometheus on ninety ninety, Loki on thirty-one hundred, Tempo on thirty-two hundred and Grafana on three thousand, as Compose publishes them on this machine. A fault lasts five minutes at the most, whatever the second argument says, and the game gets three minutes to be steady, before and after. Say prints a line to standard error, starting with chaos, so the JSON on standard output stays clean for a file, or for jq. Container gives a service's container id, from docker compose ps, whether it's running or not, which is what dash a is for. Refuse production is the first safety catch, as breaking production on purpose is the experiment we don't run. You can tell a deployment's stack two ways. It's started with production's compose file, which the deployment's env file names, and Docker on another machine is set in the docker host variable. When Redis has a container, docker inspect reads the compose files it was started with, from a label Compose puts on each container. The label lists each file the stack was started with, so a stack started with production's file on top shows it there. If the docker host is set, or the compose file variable, that label or the env file mentions production's compose file, it says this looks like a deployment and not staging, and exits with two. Next come a few small functions for asking Prometheus. Query gets the answer to a query, now, or at a time when there's a second argument, and prints its first value, or nothing. It's curl, to Prometheus's query API, with dash f to fail on an HTTP error, s and capital S to be quiet but still show errors, and ten seconds at most, so a frozen Prometheus can't hang the script. Colon plus adds the time when there is one, and jq takes the first result's value, where slash slash empty gives nothing when there's no result. By gets the values at a time by a label, as one JSON object, such as Ember and Frost with a number each. Jq's dash dash arg passes the label in, each result becomes an object of its own, and add merges them. Range asks the range query API for a query's values between a start and an end, which it takes as unix times, along with a step, a sample each two seconds, which is how we'll get a timeline. And largest takes such a range and gives the largest value it had, or null, leaving out the not-a-numbers a quantile gives when nothing happened. Steady is the heart of it: the game as it should be, which the principles call the steady state. Each world that's up runs its twenty ticks a second, the bots' newest worlds are under two hundred milliseconds old, they're all connected, no item has a copy, and the saver's last good flush is under fifteen seconds old. It prints what isn't, and fails if anything isn't. Up counts the worlds whose scrape answered, and ticking counts those that ran nineteen and a half ticks a second or more, over the last ten seconds. Age is the ninety-ninth percentile of how old the bots' newest world is, from our own swarm's bots, picked out by their address. Connected is how many bots are in, copies is the saver's count of serials held twice, from episode seventy-nine, and behind is the age of the saver's last good flush, from step one's gauge. Each one that's off goes into an array of what's wrong, as a short message, such as one of two worlds running its twenty ticks a second. The bots are checked when we expect some, as the saver's experiment sends them all away. The other messages say how old the bots' worlds are, how many of the bots we expect are connected, how many serials are held twice, and how old the saver's last good flush is. Each check is a test, and when it fails, the double bar after it adds its message to the array. An age that's not a number, as when no bot has a world yet, counts as off too. Bash can't compare decimals, so jq does, with dash n for no input, and dash e for an exit code that follows the answer. What's wrong is printed on one line, and steady returns one. Wait steady checks once each two seconds until the game is steady, and prints when it was, as a unix time, or gives up after three minutes. Seconds is bash's own count of seconds since the script started, which makes a deadline easy, and on the way out it runs steady once more, without hiding what it prints, so we can see what wasn't right. Start bots runs bots in a container of their own, which Prometheus finds by the name bots. The world's age we measure is the main swarm's alone, as a swarm waits for each of its logins, so the probes' age would measure their logins as much as their world. They join the stack as it is, as otherwise Compose starts what the bots depend on, the login server and the world, and makes them again if their configuration has changed, in the middle of an experiment. I learned that one the hard way, when a run of mine quietly made the login server again halfway through. It's docker compose run, detached, and removed when it stops. No deps is what keeps the stack as it is. The bots get the service's alias on the network, which is the name Prometheus looks up, and two labels, one for anything the bench started, and one naming the swarm, so we can find it again. Stop bots removes the containers with a swarm's label, through xargs dash r, which runs nothing when there are none. Next comes the part I'm happiest with, the backstops. A fault comes out a little after it should have ended, even when the script is killed before it gets there: each backstop runs a command then, unless restore kills it first. Each runs under a name, which restore finds it by, so a killed run's backstops go too, before they can undo a later run's fault. Each backstop is a shell started with nohup, so it outlives the script and its terminal, and the ampersand runs it in the background. It sleeps fifteen seconds longer than the fault lasts, and runs the command that undoes it. The word after the command, nanoMMO chaos backstop, is that name, the shell's dollar nought, which shows in its command line. Without the backstops, a script killed outright, say with kill dash nine, or stopped again while it restores, would leave Redis frozen until someone happened to notice. And without the name, a killed run's backstops could fire in the middle of the next run, and take its fault out early. Pause freezes a service's container with docker pause, which uses the kernel's freezer for a group of processes. Its processes stop where they are, with their sockets open, so from outside it looks like a process that's hung, and a connection to it doesn't fail, it waits. Docker stop is different, as it ends the process, so its connections close and whoever's on the other end finds out at once, and docker kill sends the kill signal, which a process can't catch. Pause keeps the command that undoes it, and starts a backstop with it. Delay slows what a service sends by some milliseconds, with the netem image, which the run builds first. It's a docker run, detached and removed when it's done, with the bench's label, the network admin capability that tc needs, and the service's network namespace, so the delay is on that container's traffic. Its entry point is a shell, and that container takes the delay off on its own, fifteen seconds after the fault should have ended, as its own backstop. Undelay takes it off at once, deleting netem's rule with tc, in a container of its own, in that network too. Tc is the Linux tool that sets netem up, and its qdisc del removes the root queueing rule from the container's network device, eth zero. Restore puts back whatever a fault left: paused containers thawed, a delay taken off, stopped services started, and the backstops gone. Running it twice does no harm, which matters, as it runs on the way out, however the script ends. First comes the fault's undo command, if there is one. Next, for each container of the project, docker inspect reads whether it's paused, and one that's still paused is unpaused. A delay is taken off, and its container removed, and any of our services that has exited is started, with docker start, and not with Compose. Compose would first wait for what each one depends on to be healthy, and MySQL isn't, for a moment after it's thawed. Last, the backstops are killed, as their faults are out. P kill dash f matches each process's whole command line, so it finds them by their name. Finish runs as the script ends, however it ends: restore, the probes stopped, and the main swarm stopped too, unless keep bots says to leave it for the next experiment. Run with restore, after the production check, it works out the netem image's name from the project's, assumes a delay on MySQL, and restores, which kills any backstops a killed run left. It takes a delay off Redis too, stops both swarms and says so. Otherwise, a case statement gives each experiment its length, unless the second argument says. It's five seconds for freezing Redis, seven for stopping it, fifteen for MySQL, a minute for its slow answers, eight for the saver, twenty for the login server and thirty for Consul, and a minute for the telemetry. Anything else isn't one of the experiments, and the script says so. A fault longer than five minutes is refused, with a line that says so. Trap runs finish when the script exits, whatever the reason, and control C, or a kill, makes it exit with a hundred and thirty, so finish runs then too. Now the run, which builds the bots and netem images, quietly, and works out the netem image's name, which Compose makes from the project's name. When there's no swarm running, kept from an experiment before, it starts one, from bot number one. It looks up the swarm's address on the network, which picks the swarm's metrics out from the probes', by the instance label Prometheus gives each target. And it waits for the game to be steady. If it isn't, there's no point, as an experiment on a game that's already struggling can't tell us what the fault did. The probes, ten more bots numbered from a thousand and one, start logging in over the fault's seconds, unless it's the saver's experiment. The time the fault goes in is noted. And in it goes: Redis paused, or MySQL, the login server or Alloy. Redis or Consul is stopped, with a backstop to start it again, or MySQL delayed by fifty milliseconds, unless delay says otherwise. The saver's experiment takes a few steps, in order. MySQL hangs first, so that no flush saves the bots as they leave. Each leaving marks a bot unsaved in Redis, and the saver's next flush, within its five seconds, takes them out of the set and waits for MySQL with them. That's the moment we want: a saver holding a batch it's taken, with MySQL not answering, as one would be when someone restarts it to unstick it. So the bots leave, and the steady state expects nobody. The script sleeps for the fault's length, kills the saver, for that experiment, and restores. It notes when the fault came out, and waits for the game to be steady again. How long that took is the recovery time, and a game that isn't steady again within three minutes gets null. Next, it marks the experiment on Grafana, through its annotations API, from when the fault went in to when it came out, tagged chaos and the experiment's name. That's the purple stretch on the reliability dashboard, so anyone looking at a graph later can see a fault was ours. A Grafana that's down doesn't stop the run, as the mark is nice to have. For the saver's experiment, we check what it lost: each bot's character, as Redis has it and as MySQL does, which should match once the saver's caught up. After ten seconds, which is two flushes, MySQL gives each bot's id, position and experience in Ember. Awk turns each row into an H M GET of that player's hash in Redis, for Redis's command-line client to run, and paste puts each player's three answers on one line. Paste puts the two side by side, and awk counts the players whose position is more than a hundredth of a metre out, or whose experience differs, between the two. It says when each of those happened, and waits fifteen seconds, so the traces of the fault's last moments are kept, as Alloy decides on each trace ten seconds after its first span. The wait matters for the metrics too, as a service that was frozen might not have been read by Prometheus since it was thawed, even with the game steady. The window starts a second before the fault, and ends now. Count sums a metric by a label, at a time. The ticks come first, as JSON that jq builds from the numbers it's given. Dash dash arg JSON passes a value in as JSON, here the count of ticks each world has run, at the start of the window and at its end. Next are the ticks on time, within fifty milliseconds, at both, and the time of each world's last sample, at both. Timestamp gives when a sample was scraped, so what a world owed comes from how long Prometheus watched it, and a world that's gone from its targets by the end isn't counted. For each world there at the end, select keeps those that have a count, a bucket and a sample at the start too. A world owes twenty ticks for each second between those samples, rounded, the ticks it ran are the difference in its count, and those on time the difference in its bucket. Missed is what it owed, less what ran on time, and late is what it ran, less what ran on time. Keep missed in mind, as it's the number the ticks' objective is built on, and it's going to surprise us. A world that wasn't there as the window began, such as one that started in it, has nothing to count from, so it's left out, like one that's gone by the end. Without the select, jq would stop with an error, as it can't take null away from a number, and set dash e would end the run right there. The players dropped come next, by world, from step one's counter, as its value at the end less its value at the start, which jq's with entries does for each key. The logins are by result, from the login server's histogram, worked out like the drops. Over five seconds is all the requests in the window, less those in the five-second bucket, summed across the results, at both ends of the window, as for the drops. Jq adds up the counts with add, and those, with the failed ones, are what the logins' objective counts. There's one case more, which I added after a run of mine crashed right here, at the very end. Each result is there from the login server's start, so an end without counts is one where Prometheus couldn't read it, and then the logins are null, instead of jq stopping the run with the error from before. The spans each service dropped come from episode eighty-seven's counter, by job. Job is the label Prometheus gives each group of targets, such as nanoMMO login or nanoMMO server, so it reads as which service dropped them. The alerts are those that were pending or firing in the window, from Prometheus's ALERTS series, which has one for each, with its name and its state, kept once each. Its range runs from the window's start to now, so an alert that fired after the game was steady again still shows. Errors asks Loki how many lines our services wrote to standard error, by service, with count over time. Alloy labels each line with the stream it came from, so stream equals standard error picks out the errors, from our servers and the bots. It's counted over a range as long as the window, at a time in nanoseconds, which is what Loki's API takes. It's asked for the window and for as long before it, as some services write errors all the time, and a number reads better next to what's normal. Jq turns Loki's answer into one object of lines by service, or null when Loki doesn't answer. The errors go into the report twice, as during and before. Search asks Tempo for the traces that match a TraceQL query, from a start we give it to the window's end. Tempo three point one has a bug I ran into while making this. A search that starts thirty seconds ago, to the second, is refused, as Tempo splits a search at that point, and one part comes out empty, starting and ending at once. So traces searches from the window's start, and when Tempo refuses, from a second earlier. For each trace, jq keeps its id, its root service and span, and how long it took, the slowest first, out of the hundred at most that its search API gives back. We want the traces that failed, and those that took over half a second, so the report can point at a trace to open. Last, a timeline, which is the part I look at first: each world's ticks a second, worked out over four seconds, the bots' world age at the ninety-ninth percentile, and how many bots are connected. Label replace gives those last two a world label of age and bots, so one range query returns all of them, and jq can tell them apart by that label. It runs from ten seconds before the fault to the end, a sample each two seconds. Jq makes each sample an object of its time and its value, rounded to hundredths, under the series' world label, grouped by time, in seconds from the fault. And jq puts the report together, from each piece passed in. There's the experiment, its seconds, when the game was steady, when the fault went in and came out, and when it was steady again. After those come the ticks, the drops, the logins and the stale rows, the spans, the alerts, the errors, the timeline and the traces, each from a variable worked out above. Along with them go the oldest the bots' world got, as the largest of the swarm's percentile over the window, the fewest bots connected, and how many are connected at the end. Grep dot passes that answer through when there is one, and otherwise echo gives null. Next are how far behind the saver got, and the most serials held twice. The report is one JSON object, with its times as dates. Restored after is how long the fault lasted, recovered after is how long the game took to be steady again, and the window runs from the fault to the end. After them come the ticks, the players dropped, the oldest world, the fewest bots and the bots after, the logins, how far behind the saver got, the stale rows and the copies. Next are the spans dropped, the lines on standard error and the alerts, and how many traces failed, with the first five, the three slowest, and the timeline. Tee sends the JSON to standard output and a copy into jq, which writes one line of it to standard error. The line has how many ticks each world missed, how old the bots' world got, who was dropped, and how long the game took to be steady again. That line is what we'll read as each experiment ends, and the file has the rest.

Breaking it

1:01:49 So, the first experiment, at this step, before any fix: Redis frozen for five seconds while a hundred bots play in Ember. Keep bots, in front of the command, leaves them playing when the run's over, for the next experiment, and the report goes into a file. The tool waits for the game to be steady, freezes Redis, thaws it, and waits for steady again, which takes under a minute. Its last line says Ember missed one tick, and that the bots' world got about five point nine seconds old. Let's ask jq for Ember's ticks from the report. Over the window, Ember owed six hundred and eighty ticks, and six hundred and seventy-nine of them ran on time. So the indicator we built earlier counts one missed, while all the players in Ember stood still for five seconds. The indicator is wrong, and the timeline shows why.

1:02:40 Here's the report's timeline, with a point each two seconds. Ember drops to no ticks at all while Redis is frozen, as each tick waits on its saves. Once Redis is back, it runs about sixty-six ticks a second for a moment, about one hundred ticks it owed, back to back. Each of those catch-up ticks was quick, so each one counted as on time. On paper, the world made up for the lost time, while its players lived through five frozen seconds. Frost sits at twenty ticks a second all along. It's our control, though a weak one for Redis, as a world with nobody in it has nothing to save.

1:03:15 On the dashboard, the purple marks are two runs: Redis frozen for five seconds, and then for twelve. The bots' newest world gets about six seconds old in the first, and about fifteen in the second. Twelve seconds is past the ten seconds a lease lasts, so when Redis came back, the world's first save found none of its players' leases its own, and dropped them all, one hundred and one players, without saving them. The bots log back in by themselves now, five a second at most, so it was about thirty-eight seconds before the game was steady again. And no alert fired, as world falling behind needs a whole minute, and nothing alerts on players dropped. The ticks' budget still says one hundred percent left, as Ember ran all the ticks it owed once Redis was back, each one on time.

1:04:00 Next, MySQL frozen for fifteen seconds, which is the result that surprised me. I expected the worlds to stall on the next player who joined, as loading a player reads MySQL on the world's thread. They didn't: both worlds kept to twenty ticks a second, and Ember missed zero. Nobody could join, as the login server needs MySQL first, to check a password, before anyone gets a token for a world. So the login server hung on its one thread, the logins with it, and the worlds saw no joins at all. Tempo's traces show what that wait looked like, from the inside. The slowest login in that window took about thirteen point seven seconds, nearly all of it waiting for one MySQL statement. The login server timed that one, and the rest it counted as quick, as it didn't read them until MySQL was back.

1:04:50 The login server frozen for twenty seconds is the next one, while ten more bots come to log in. The report has the logins as the login server counted them. Twenty answered ok, and zero over five seconds. Meanwhile, the bots that came to log in waited the whole twenty seconds, and so would a player's window. The login server didn't read their requests while it was frozen, as the kernel held the connections for it, and once it was thawed, it answered each one in milliseconds, so a request it doesn't read, it can't time. That's the difference between white-box monitoring, what a server sees of its own work, and black-box, what a player sees, and so far we've had the first kind alone.

1:05:32 A quick one: Alloy, which collects our spans and logs, frozen for a whole minute. Let's ask jq for Ember's missed ticks, and for the spans each service dropped, by job. Ember didn't miss a single tick, and the players didn't notice a thing. The login server dropped one hundred and forty spans and the world ninety, counted as they went. This one found nothing wrong, and I think saying so is part of the result. It's what last episode meant to build: the tracer doesn't wait for its collector, and a tick doesn't wait for the tracer.

1:06:06 The saver next, which takes a bit of choreography, and the tool does it for us. MySQL hangs first, and the bots leave, which marks each of them unsaved in Redis. The saver's next flush takes them out of the set, and waits for MySQL with them, and that's when the tool kills the saver, the way someone restarting a stuck container would. Afterwards, the tool compares each bot's character in MySQL with the one in Redis, and eighty-eight of them are behind. They've logged off, so they stay behind until they play again. And Redis's set of unsaved players is empty, so as far as anything can tell, nobody's waiting. The saver's flushes are fresh again, its gauge says nobody's unsaved, and nothing's lost until the day we lose Redis. Without backups, that's the day those rows matter.

1:06:54 And now the one I liked least of all the experiments. Behind the scenes, two players from episode seventy-two's red-team client are in Ember. The giver offers its sword, and leaves the moment a trade is over, while the taker takes it, and stays. They're in the middle of a trade, with one ready to go, when Redis goes away, and about a second later, it comes back. The taker's lines say the trade went through, and it has two swords. We ask MySQL for their swords, and the giver still has its sword in its main hand, while the taker has one in its bag with that serial too, which makes one sword in two characters. The trade's save failed while Redis was down, and the world read the failure as a save that went through. The giver left, and its save failed too, so Redis kept its bag from before the trade. The taker stayed, and once Redis was back, its next save went in with the sword. It took a one-second outage at the wrong moment, and it happened two times out of two.

1:07:51 A minute later, items duplicated fires, the alert from episode seventy-nine, which watches MySQL for a serial that more than one item has. So the copy was caught, though by then MySQL had the sword in both characters.

1:08:05 That's an afternoon of breaking things, before any fix. When Redis hangs, the worlds hang with it, and the indicator we'd just built can't see it, while anything past ten seconds drops all the players. When MySQL hangs, logins hang, and the login server's own numbers can't tell. A saver killed at the wrong moment loses its batch, and a trade during a second of outage copies an item. And frozen telemetry did nothing to the game, as intended. The common thread, I think, is that our code assumed Redis and MySQL always answer, and soon, so it waits for them for as long as they take. The book's chapter on cascading failures answers that with deadlines, a limit on how long each call can wait.

Wrap-up

1:08:47 So, in this episode, we measured what players get, as ticks on time, logins answered and fresh saves, and made those objectives with error budgets, with alerts on how fast a budget burns. We wrote a tool that breaks a single piece at a time, in staging, while bots play, and broke the game with it. Our new indicator for ticks counted a frozen world as fine, and a second without Redis, at the wrong moment, copied a sword. In the next episode, we fix most of it, and run these experiments again to see if the fixes hold.

Playing it

1:10:10 If you've broken your own servers on purpose, I'd love to hear what you found, in the comments, along with what you'd like to see next. And if you want to follow along with the series, subscribe to the channel. I hope you find this useful, and thanks for watching.