|
nanoMMO 0.97.8
Building an MMO: The Engineering Series
|
How reliable the game should be, how we measure it, what we do when it isn't, and what breaking it on purpose taught us. Why we do it this way is in decision 39.
A service level indicator is a number that says how the game is doing for players, as a share of events that went well. A service level objective is how much of the time it has to be, over 30 days. What's left over is the error budget, which is how much may go wrong in a month before we've broken the promise.
| Indicator | Good, out of all | Objective | Budget, a month |
|---|---|---|---|
| Ticks on time, each world | Ticks that ran within their 50 milliseconds, out of the 20 a second a world owes | 99.9% | 51,840 ticks, as long as 43 minutes frozen |
| Logins answered | Requests the login server answered within five seconds, a refusal such as a wrong password counting as answered, out of all | 99% | 1% of them |
| Saves fresh | Time with the saver's last good flush under 30 seconds old | 99.9% | 43 minutes, 12 seconds |
43 minutes is 0.1% of 30 days, from the SRE book's Appendix A, "Availability Table".
They come from three things the servers measure, the ticks' histogram, nanommo_tick_duration_seconds, the login server's nanommo_login_duration_seconds on port 9467, and the saver's nanommo_saver_flushed_timestamp_seconds. A world that's down, or gone from Prometheus's targets, though it was there in the last day, misses every tick, and a saver that's gone is stale. The 20 is kTicksPerSecond in server/main.cc.
A world that stalls lets the ticks it missed go once it's more than a second behind (NextTick, server/tick.h), so they're missed. Before episode 89 it ran them back to back afterwards, which made up the count, and a world frozen for twelve seconds counted one tick missed.
monitoring/alerts.yml's slos group records each indicator's share of failures every 30 seconds, for each world's ticks over the last minute, and from those, over five minutes, 30, an hour, six hours and three days, as world:nanommo_tick_errors:ratio_rate5m and so on, and for the logins and the saves over the same windows. rate() divides by its whole window, so a Prometheus that's just started would read an hour's rate over its first minutes as a world that missed nearly every tick. To avoid that, a world's minute counts once it has a minute's samples, and each longer window once Prometheus has watched for half of it.
A burn rate says how fast a budget's going. At 1, a month's lasts exactly the month; at 14.4, it's gone in two days. As the SRE Workbook's chapter 5, "Alerting on SLOs", has it, each alert below compares a long window and a short one, a twelfth as long, so that it fires when the burning is real, and stops soon after the burning does.
| Alert | Severity | Burn rate | Over | Spends of the month's budget |
|---|---|---|---|---|
| TicksBurningBudget, LoginsBurningBudget, SavesBurningBudget | page | 14.4 | an hour, and the last five minutes | 2% in the hour |
| the same | page | 6 | six hours, and the last 30 minutes | 5% in the six hours |
| TicksDrainingBudget | ticket | 1 | three days, and the last six hours | 10% in the three days |
A freeze of a few seconds burns fast over five minutes, but not over the hour, and pages nobody, as alerts_test.yml checks. The logins' alert waits for ten logins in the hour, as a handful can't tell a bad hour from bad luck. Each alert's value is its burn rate, and its severity a label; for now Alertmanager mails them all to the same place.
The reliability dashboard, nanoMMO reliability, at http://localhost:3000/d/nanommo-reliability, has each budget left this month, from what Prometheus has, the burn rates over the hour and the last five minutes with lines at 6 and 14.4, the ticks, logins and saves as they go, the bots' world's age, the players dropped, errors by service, the alerts firing, and each chaos experiment as an annotation.
A spent budget means the next episode is about reliability, whatever else was planned, until the budget's back. A budget with plenty left is room for risk, such as a deploy, an experiment or a new feature. With one maintainer, that's the whole policy.
Chaos engineering, as the Principles of Chaos Engineering put it, is "the discipline of experimenting on a system in order to build confidence in the system's capability to withstand turbulent conditions in production". An experiment says what the steady state is, guesses what a fault will do to it, puts the fault in, and watches. tools/bench/chaos.sh does that, one fault at a time, in staging, while bots play (Benchmarking has how to run it, and what its report says).
The principles also say to run experiments in production, as Netflix's Chaos Monkey does, killing instances at random. Each of their services runs many instances behind a balancer, so losing one should go unnoticed. We run one instance of each world, on one server, so any kill there is an outage for every player in it. We never break production on purpose, and the tool refuses a deployment's stack. Staging is one machine, with bots, which says what our code does when Redis or MySQL fails, not what a real network does.
With 100 bots in Ember, ten more logging in during each fault, and Frost, with nobody in it, as a control. Before is 0.88.0, with the measurements and the tool and none of the fixes; after is 0.89.0, with all of them. The reports are JSON, and each has its trace ids.
| Experiment | Before | After |
|---|---|---|
| Redis frozen 5 s (redis-pause 5) | Every player in Ember froze, their world 5.9 s old at its worst. Ember ran the 100 ticks it owed back to back afterwards, so it counted 1 tick missed | With the timeouts alone (0.89.0's step 1), Ember crawled at 3 ticks a second instead, its players' world 0.7 s old, and the indicator counted 88 ticks missed. With failing fast too (step 2, measured with all the fixes), Ember missed 4 ticks, the world 0.3 s old at its worst, and nobody dropped |
| Redis frozen 12 s | Ember froze, and dropped all 101 players when Redis was back, as the leases had run out. 1 tick missed by the count. Back to steady 38 s later. The postmortem | Ember ticked on, 10 ticks missed, its world 0.5 s old at its worst, and still dropped all 101 when Redis was back, as their leases had run out in Redis meanwhile, which is what a lease does when Redis is gone for longer than it lasts. Open, in the postmortem |
| Redis stopped 15 s (redis-down 15) | The worlds ticked on, as each call failed at once, and Ember dropped all 101 once Redis was back. 28 logins failed | The same. Ember ticked on, and dropped all 101 once Redis was back. 29 logins failed |
| A trade while Redis is down, for 1.1 s | The giver left and kept the sword in Redis, the taker kept it too. One serial in two bags in MySQL, twice out of twice, and ItemsDuplicated fired | Called off. The save failed, so the world dropped both, unsaved, and each came back with their own sword. No serial twice |
| MySQL frozen 15 s (mysql-pause 15) | The worlds didn't notice. Nobody could join, as the login server needs MySQL first, and it hung instead. It timed one login at 13.7 s, and the rest as quick | Each login failed after MySQL's three seconds, and then at once for five. The login server counted 43 failures, and LoginsBurningBudget paged. The saver fell 25 s behind, short of the 30 that counts against the saves. The worlds still didn't notice |
| MySQL 50 ms slower for a minute (mysql-slow 60) | Each join held Ember's thread between ticks, which the ticks' indicator can't see. No tick missed, and the bots' world 0.23 s old at its worst, from 0.06 | Much the same, with 8 ticks missed and the world 0.27 s old at its worst. Loading players off the tick's thread is open |
| The saver killed mid-flush (saver-kill 8) | 88 bots' rows in MySQL left behind Redis's for good, while the saver said nobody was waiting, and its flushes were fresh | Nothing lost, as the restarted saver put the batch back and saved it. No row behind |
| The login server frozen 20 s (login-pause 20) | Every login waited the twenty seconds, and the login server counted them all as answered in time | Each login gave up after five seconds, and the bots counted 3 timeouts, then logged in once the server was back. The login server's own metrics still saw nothing, which is why the bots count theirs |
| Consul stopped 30 s (consul-down 30) | Play went on; logins went through, with no worlds to list, and 54 lines of errors from the login server | The same for play. The login server now gives the worlds it listed in the last half a minute (not measured, as the bots don't read the list) |
| Alloy frozen 60 s (telemetry-pause 60) | The worlds didn't notice, as the tracer never waits on its collector; 221 spans dropped | The same, with 230 spans dropped, and nothing else |
Before the fixes, an hour of these spent almost none of the ticks' budget by the indicator, 100% left to a tenth of a percent, though one of them dropped every player in Ember, and the logins' budget three times over. The indicator measured the wrong thing, which is what 0.89.0's catch-up cap is for.
Not every experiment found a bug. Frozen telemetry did nothing, as episode 87 meant, and saying so is part of the result.
Episode 92's experiment, war-replay, checks what the saver does with a batch it reads twice. The 100 bots go to war, in two guilds, for a minute; MySQL answers 200 milliseconds late, so that a flush waits on it for a couple of seconds; the bots leave; and as soon as the saver has taken a batch with kills still in the stream, Redis hangs for ten seconds. MySQL commits the batch, and the saver can't take its kills out of the stream after, so the next flush reads them again. The bots leave first so that no kill happens, or fails to be saved, while Redis is gone, and the report compares the kills the worlds counted, nanommo_war_kills_total, with what the scores in MySQL came to, once the saver's caught up.
| Run | Before, at 0.92.0's step 2, with kills added to counts | After, at step 3, with a row for each kill |
|---|---|---|
| 1 | 196 kills, a score of 200 | 294 kills, a score of 294 |
| 2 | 264 kills, 265 | 274 kills, 274 |
| 3 | 267 kills, 281 | 275 kills, 275 |
Three times out of three, the batch Redis hung on was counted twice. After the fix, the saver read kills again just as often, and MySQL skipped 30 it had already over the three runs, by INSERT IGNORE's warnings, so the scores matched the kills. A trade's record would have been counted twice the same way, had it not had an id of its own from the start (Saving players).
The postmortem has some of these as its open action items.
Google's Site Reliability Engineering is the book most of this page's words come from. Most of it is about a large company running many services with teams on call; nanoMMO is one maintainer and one server. This is where the game stands against it, chapter by chapter, honestly, as of episode 89.
| Chapters | What it's about | What we have | What we don't |
|---|---|---|---|
| 3, Embracing Risk; 4, Service Level Objectives | How reliable is reliable enough, as a number | Three SLOs and their error budgets (episode 88). Risks we took on purpose before, such as dropping players whose lease ran out (0031) | An agreement with players about them. They're our own goals |
| 5, Eliminating Toil; 7, The Evolution of Automation at Google | Doing by hand what a machine should | scripts/deploy.sh and scripts/release.sh (episode 19), scripts/handover.sh (36), deploying on push (37), scripts/doctor.sh (65), the Dependencies workflow, Flyway (18), tools/bench/chaos.sh (88) | Rolling back is a command in Deployment, by hand; runners' tokens and certificates are renewed by hand |
| 6, Monitoring Distributed Systems; 10, Practical Alerting | The four golden signals; alerting on symptoms | Latency (ticks, saves, stories, logins, the bots' world age), traffic (messages, bytes, players), errors (stderr in Loki, refused chat and stories, failed logins), saturation only as ticks a second out of 20; alerts on symptoms (WorldFallingBehind, ItemsDuplicated, the burn rates); promtool tests in CI; logs, profiles, traces. Prometheus descends from Borgmon, chapter 10's subject | No memory or CPU metrics of our own binaries; no exporters for Redis or MySQL; nothing watches the watchers, which means Prometheus, Alertmanager, Loki and Alloy can be down and nothing says so; no runbooks |
| 8, Release Engineering | Builds you can repeat, from pinned inputs | Pinned dependencies and image tags, builds and tests in Docker (0004), sanitizers, fuzzers and clang-tidy (28), versions and a changelog (0020) | The images are built again on the server, so what runs isn't the binary CI tested |
| 9, Simplicity | Less code, fewer moving parts | One thread for a world (0008), one server (0016), decision records | |
| 11, Being On-Call; 13, Emergency Response; 14, Managing Incidents; 16, Tracking Outages | People who answer, and how | Alertmanager mails every alert to oncall@nanommo.example; the objectives' alerts carry a severity, page or ticket | Nothing routes on the severity yet, and the mail goes to Mailpit unless smtp_smarthost says otherwise, so nobody is paged; one maintainer, no rotation, no incident roles, no log of outages. Chapter 13's "test-induced emergency" is what a chaos experiment is |
| 12, Effective Troubleshooting | Hypotheses, tested | Tracy and perf (8), Pyroscope, logs beside metrics, traces (87), the red-team client (72) | |
| 15, Postmortem Culture: Learning from Failure | Writing it down, blamelessly | Postmortems, from episode 89, after the book's Appendix D | Only the first |
| 17, Testing for Reliability | Tests up to disaster tests | Unit tests, the servers' tests against real MySQL and Redis (29), the load test (46), netem (9, 68), the red-team client, config tests (promtool, Loki's and Alloy's), and chaos experiments (88) | They're run by hand, in staging, not continuously |
| 19, Load Balancing at the Frontend; 20, in the Datacenter | Spreading players over servers | Worlds as shards a player picks (21), dungeon copies (38) | One machine |
| 21, Handling Overload | Saying no before falling over | Token buckets on chat, stories, items, guilds and duels; input queue caps; 64-kilobyte frames; each player sent what's near them | No admission control, so a world takes whoever joins; a session's outbox has no limit |
| 22, Addressing Cascading Failures | Deadlines, failing fast, backing off | Consul on its own thread with timeouts; the storyteller's timeouts; the gateway's stale list; from episode 88, bots that back off with jitter, and from 89, timeouts on Redis and MySQL, failing fast after one, and a login's deadline | A join still loads the player on the tick's thread; Redis is shared by everything |
| 23, Managing Critical State: Distributed Consensus for Reliability | Agreeing on who owns what | Leases with fencing numbers (0031) | One Redis, which saves to disk once a second, so after a crash a fencing number can go back; Consul runs in development mode, in memory |
| 24, Distributed Periodic Scheduling with Cron; 25, Data Processing Pipelines | Jobs that must run, and must finish | The saver and the recorder, and from episode 88 the saves' freshness as an SLO, and from 89 a saver that keeps its batch until MySQL has it | |
| 26, Data Integrity: What You Read Is What You Wrote | Not losing or corrupting data | MySQL as the truth (0010), transactions (0028), serials and ItemsDuplicated, trades recorded once | No backups, and so no restore drill. MySQL, Redis and ClickHouse are volumes on one disk |
| 27, Reliable Product Launches at Scale; Appendix E | Launching carefully | The version rule, handovers without downtime (36) | No canary, so a deploy hands every world over, one after the other, with no time to compare; no launch checklist |
| 28 to 32, Management | Teams | The getting-started page and the decisions | Mostly not ours, with one maintainer |
| Appendix A, Availability Table | What a number of nines allows | 99.9% of 30 days is 43.2 minutes, which is where the ticks' and the saves' budgets come from | |
| Appendix D, Example Postmortem | A postmortem to copy | The template |