nanoMMO

MMORPG game analytics with ClickHouse and Grafana

What players do, as events, from the C++ server through a Redis stream into ClickHouse, a column-oriented database, and onto a Grafana dashboard of the game.

Watch on YouTube

We add game analytics to our MMORPG: each kill, drop, quest taken and turned in, level reached, potion drunk and death is an event, recorded by the world on the C++ server. Once a tick, the server adds them to a Redis stream, so the game never waits on the analytics, and a recorder takes them from there, as a consumer group, in batches, into ClickHouse, a column-oriented database that counts millions of rows quickly. Then a Grafana dashboard of the game, from ClickHouse: kills a minute, the quests taken against the ones done, the items that come into the game against the ones used, and where players die. This is episode 47 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.

Topics: Databases, DevOps

The code

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

  1. 2:30ep047/step-1ClickHouseBrowseChanges
  2. 5:08ep047/step-2Events in the worldBrowseChanges
  3. 10:46ep047/step-3A stream in RedisBrowseChanges
  4. 17:20ep047/step-4The recorderBrowseChanges
  5. 30:15ep047/step-5A dashboard of the gameBrowseChanges

Transcript

Intro

0:00 Hello and welcome back to episode forty-seven, where we're going to record what the players do in our game, as events, and keep them in ClickHouse, for the game's analytics. Let's begin with a quick look at where we're going to be by the end of the episode.

0:14 A hundred and fifty bots play our game, and a dashboard of the game shows what they do, such as the kills a minute, by monster. And it has how many of each quest are taken, against how many are done, all of it from ClickHouse.

0:29 On the website of the series you can find more information about the project. The code for the project is open source on GitHub, tagged at each step of each episode so that you can follow along.

Last time

0:41 Now, before we get to today's work, let's quickly go over what we did last time. The bots learnt to quest, taking quests, hunting for them and turning them in, with each bot putting its world together from its parts, like the client. Pyroscope keeps our servers' profiles all the time, sampled from the kernel with eBPF, with no change to our code, as a flame graph on our dashboard. And we put two thousand questing bots on the server, and saw that its time goes into sending.

Today

1:09 Our metrics tell us how the servers are doing, but not what the players do. When you run a game, you want to know which quests players finish, how long they take, what drops, and where they die. That's how you balance a game, and catch a bug, say a quest that hands out too many potions. So today, the world is going to record what happens in it as events, such as a kill, a drop or a quest turned in. Once a tick, the server is going to add them to a stream in Redis, so the game doesn't wait on the analytics. A new process, the recorder, takes them from the stream in batches, into ClickHouse, a database built for counting a lot of rows. And Grafana gets a dashboard of the game, from ClickHouse. All right, let's get started, with the database the events are going to go into.

ClickHouse

1:55 ClickHouse is an open-source database, and its README calls it column-oriented, for analytical reports in real time. MySQL keeps each row together, which suits our players, read and written a row at a time. ClickHouse keeps each column on its own, so a query that counts events by their kind reads the kind column and nothing more, out of all the columns. And a column of a few values that repeat, such as the kind of event, compresses to almost nothing. That's what we want for events, as there are going to be a lot of them, written once and then counted, in different ways, for as long as we keep them.

2:30 The events table goes in a folder of its own, analytics, as events dot s q l. Create table if not exists, in the nanommo database, so that running it again changes nothing. The columns come one a line, each with its type, which ClickHouse is strict about, as it stores each column on its own, in its type. Each event has its time, to the millisecond, in UTC, and the world it happened in. Low cardinality is ClickHouse's way of keeping a column with a few values as a dictionary of them, and a small number for each row, which is what a world, a land and a kind of event are. What is the name of what the event's about, a monster, an item or a quest, so that our queries read by name. Then the player, by name, where they stood, as x and z, and the ids of the monster, the item, how many, the quest, and the level, with zero for what an event doesn't have. Merge tree is ClickHouse's main table engine, which keeps a table as parts. Each insert writes a part, sorted, and ClickHouse merges the parts in the background, which is why inserting a batch at a time matters, as we're going to see. A partition a month keeps each month's parts apart, so that dropping an old month is cheap. And the sort order is the kind, then the time, as most of our questions are about one kind of event over some time, such as the kills in the last hour. Those rows then sit next to each other on disk. TTL deletes the rows older than a year for us. In compose dot yaml, ClickHouse gets a service, from its official image, on its twenty six point eight release, which is supported for longer. The environment makes our database and a user for it, with a password, and the data goes on a volume of its own. Our analytics folder goes where the image runs scripts the first time it starts, on an empty volume, so it makes the table for us. A change to the table later is going to need an alter table of its own, as the script won't run again. Port eighty-one twenty-three is ClickHouse's HTTP interface, which is how our recorder is going to talk to it. ClickHouse opens a lot of files, so its documentation asks for a higher limit on them. And the health check runs select one in its client, so that the services that need ClickHouse wait until it answers. It gets a volume in the list at the bottom. In production, ClickHouse listens on the server's localhost, like Loki, so its port isn't open to the internet.

4:54 We start ClickHouse on its own, and wait until its health check passes. Describe asks ClickHouse for the table's columns, which shows that the script ran as it started, with the world, the land, the kind and what as low cardinality strings.

Events in the world

5:09 The world is where things happen, so the world is what records them, the way it keeps notices for its players. In world dot h, an event goes after the notice, the other thing the world hands the server each tick, and it's plain data, which makes it a struct. Like the other structs in the world, its fields have defaults, so an event sets what it's about and leaves the rest. Its kind says what happened: a kill, a death, loot from a monster, a prize from a quest, a level reached, a quest taken or turned in, and an item used. Then who, by name, and where, the land by its place in our list of lands, and x and z. The position is where the player stood, which is what a map of where players die is going to need. The player's name is a copy, so an event stays whole after the player has left. The land is a number in the event, to keep it small, and the server turns it into the land's name for the row. And what it's about, by its ids in the content, starting with the monster killed, or that killed the player. The ids keep an event small, and the names come later, from the content, as the server turns it into a row. An event about a monster has its kind, which is all ClickHouse needs to count the kills of each. Then an item and how many, for a drop, a prize or a use. Then the quest, for taking one, turning one in, or a prize, and last, the level reached, for a new level. A kill fills in the monster, a drop the monster and the item, and a quest the quest, so most of a row is zeros, which ClickHouse compresses to almost nothing. A use fills in the item and a count of one, and a level the level, and leaves the monster at zero. A single struct for all the kinds keeps it a single row in ClickHouse too, which is how analytics tables usually look, wide, with a column for each thing an event can be about. Take events hands over what's happened since the last time, like take notices. And a private helper, record, starts an event of a kind, for a player, where they are, and returns a reference to it, so that the caller fills in what it's about. The events wait in a vector, next to the notices. In world dot c c, gain adds a player's experience, and heals them as they reach a new level. It returns whether they did now, as a level reached is an event of its own. Using an item records the item and one of it, once the item has passed all its checks, so a refused potion isn't counted. Taking a quest records the quest, once the player has it. Turning one in records the quest, then each of its prizes, an event each, with the item and how many, as those items come into the game. And when its experience takes the player to a new level, the level they reached, which level for looks up from their experience after the gain. A monster's death is a kill for the player who hit it last, with the monster's kind, and again a level, when its experience took them to one. Loot records each item that went into the bag, with the monster that dropped it, and how many fitted, so that what was left on the ground isn't counted. Take events exchanges the vector for an empty one, and returns what it held, so that each event goes once. Record adds an event, with designated initialisers, and the player's name, land and position, to the vector, and returns it from emplace back. And a player killed by a monster is a death, with the monster's kind. A world is a copy of a land, so in copies dot h, copies gets take events too, which moves each copy's events into one vector, as it does with their notices.

8:40 The world's test gets a test at the end, with a world made from the test content's gear and loot, and the bones quest, and a guard to give it. In it, alice, in the Ashlands, takes the guard's quest, then kills a skeleton, which drops two bones and a blade, as in the loot test. Take events then hands over everything the world recorded, in the order it happened. Whether the kill took her to a new level isn't what this test is about, so erase if drops the level events first. A level depends on the test skeleton's experience, so leaving those events in would tie this test to that number. That leaves four events, which the test checks by their place in the vector. The quest taken, by alice, in the Ashlands, the land at place one, then the kill, of the first kind of monster, then the two bones, and the blade, each an event of its own. Each loot event has the item and how many, so that ClickHouse can add up what came into the game. And each event goes once, so taking them again gives nothing.

9:40 We build the server's tests at this step, and run the world's. The new test passes, and the world's other tests still pass, with gain returning whether a player reached a new level.

A stream in Redis

9:51 Why not insert into ClickHouse straight from the tick? A tick has fifty milliseconds, and ClickHouse wants its rows in batches, as each insert writes a part on disk, which it merges later, so thousands of tiny inserts a minute would keep it busy merging. And while ClickHouse restarts, the game would wait, or lose the events. So the tick adds its events to a Redis stream, which we talk to already, in a single round trip. A stream is Redis's append-only log, where each entry gets an id from its time, and it stays there for whoever reads it, unlike a message on a channel. The recorder reads the stream as a consumer group, which is how Redis shares a stream between readers, and remembers what each one has taken and not acknowledged yet. And it inserts a batch at a time into ClickHouse. If ClickHouse is down, the events wait in the stream until it's back. That's the saver's idea again, with Redis between the game and the slower store.

10:47 The events get a file of their own in the server, events dot h. It takes string, string view and vector, abseil's status and time, our content's header, for the names, and our Redis and world headers. The stream's name is a constant, events, which the recorder is going to read too. The constant lives in the header, so the server and the recorder agree on it. Row turns an event into a row for ClickHouse, with when it happened, the world, and the name of what it's about, from the content. Then the class the server adds the rows through, events. It holds a reference to our Redis connection. The stream keeps about the newest million, so that it can't fill Redis's memory while the recorder is down. Each row is about two hundred bytes of JSON, so a million of them stays at a few hundred megabytes. And add puts the rows on the stream, all of them in a single round trip. Events is all the server needs from the analytics, a way to add rows, so the server doesn't know about ClickHouse at all. In events dot c c, we take nlohmann's JSON, which we use for Consul's answers already, and the list of lands. Name gives each kind of event the word it's going to have in ClickHouse, with a switch over the kinds. With minus W all, the compiler warns about a switch over an enum class that misses one of its values, so a kind of event left out here shows up in the build. Each case returns a view of a string literal, which lives as long as the program does, so returning a view of it is safe. After the switch, an empty string covers a value the enum doesn't name, which a cast from a number could make. What looks up the name of what the event is about, in the content, as the event has its ids, and the row is going to have a name. Names make the queries readable, and a row keeps the name it had, even if the content renames something later. Each case finds the id in its table of the content, and returns its name, or an empty string for an id the content doesn't have, as an event shouldn't stop the game. What is a second helper in the unnamed namespace, next to name, for row to call. Its switch names each kind too, so the compiler warns here as well when one's missing. The cases that share a lookup sit together: a kill and a death are about a monster, so they look in the monsters' kinds, and return the kind's name. Loot, prizes and uses are about an item. Each has braces of its own, as a case that declares a variable needs a scope for it. Then the quests, taken or turned in, by the names the players see in their quest window. The one kind left is about none of them, so it has no name. Row builds a JSON object, from a list of pairs of a key and a value, which nlohmann's JSON turns into an object, with each value's type from its C plus plus type: a string, a float or a number. The time is formatted the way ClickHouse reads a date time sixty-four with milliseconds, in UTC. The land is by name, checked against our list of lands first. Then the kind's word, and what, then the player and where they were, as floats, and the ids, the count and the level, as numbers, which ClickHouse reads into its columns by their names. Dump writes the object out on a single line, so that rows joined by newlines make newline-delimited JSON, the body the recorder sends. Add makes an X ADD command for each row, the command that appends to a stream. Max length with a tilde trims the stream to about a million, a block at a time, which costs Redis less than trimming it to the entry. The star asks Redis for the entry's id, from its clock, and the row goes in a field called row. Pipeline sends them all before reading any reply, like our saves, so a tick costs a single round trip to Redis. In copies dot h, copies gets an accessor for the content its worlds share, as the server needs the names from it, and the content doesn't change as the game runs. In server dot h, the server takes the events by reference, in create and in its constructor, like the players, and a private function, record events, does it once a tick. In server dot c c, abseil's clock, for the time now, and its time, for the time's type. Create and the constructor pass the events along, and the tick records the events right after it hands out the notices, so they go out in the tick they happened in. Record events takes the copies' events, and returns straight away when there are none, which is most ticks with few players. The time is the tick's, a single one for all its events, and each event becomes a row, with the names from the content the copies keep for their worlds, through an accessor copies gets. If Redis can't take them, the error goes to the server's log, and the events go, as the game goes on without its analytics. In main dot c c, the events use the server's Redis connection, and go to create. In the CMake file, events dot c c joins the server's library, which now links abseil's time, and its test joins the tests, with the content folder, for the names. And the server's tests pass an events of their own to each server they create, on the test's Redis, so their worlds add their events to a stream, like the real server's.

16:01 The events get a test file of their own, events test dot c c, which takes the events header, string, abseil's time, our content, GoogleTest, and nlohmann's JSON, to read the row back. The test of a row makes an event, as the world would: alice looting an Ash Buckler, item seven, from an Ash Skeleton, the second kind of monster, in the Ashlands, at one and a half, minus two. It reads the game's own content, for the names, like the bot's tests do. The row's time is a moment we pick, in Unix milliseconds, so the test knows the date it's going to read back. The row has to be a single line, so that rows joined by newlines make newline-delimited JSON, the body the recorder sends. Then the test parses it back, and checks each field one by one, with expect equal, which compares a JSON value with a C plus plus value directly. The time comes back to the millisecond, in UTC, as midnight on the thirtieth of October, then the world, and the land by name, and the kind's word. What is the item's name, from the game's content. Then who, where, and the monster, the item and how many, as numbers. And a field the event isn't about is zero. Parsing the row back, instead of comparing its text, keeps the test from depending on the order nlohmann writes the fields in, or on how it writes a float.

The recorder

17:21 Reading a stream gives replies our Redis class doesn't read yet, lists inside lists, as command and pipeline give us text, or lists of text. So in redis dot h, an entry of a stream is its id, and its fields and values, one after the other, as Redis sends them. A stream's entry is like a small hash, a few fields and their values, and ours have a single field, the row. And read runs an X READ or an X READ GROUP on a single stream, and returns its entries, or none when it waited and nothing came. In redis dot c c, read reconnects if it lost the connection, and sends the command, like command does. It appends the command, then reads the single reply, with next reply, which hands us a smart pointer that frees the reply when it goes out of scope. An error from Redis becomes an error status of ours, with Redis's words in it, such as when the group we read as isn't there. The reply is nil when it timed out, or else a list of streams, each a list of its name and its entries, and each entry a list of its id and its fields. So anything but a list with something in it is the timeout, and gives no entries. We read a single stream, the first, and a reply that isn't shaped like one is an error, as it means we sent a command that read doesn't know, and go over its entries. A read that blocks holds up whatever is sent after it on the connection, so read sends its command on its own, and not in a pipeline. We go over the entries, taking each one's id, and its fields, as text. Each element's type is checked before we read it, as a reply we didn't expect would otherwise have us read past the end of a list, and crash the recorder. An entry trimmed away by the max length since it was read has nil for its fields, which leaves the entry's fields empty. Then recorder dot h, with the class that does the recording. It takes chrono for the wait, functional for the sink, string and string view, abseil's status, and our Redis header. The recorder takes the events from the stream a batch at a time, to a sink. Taking them is the recorder's job, and storing them the sink's, so that the recorder doesn't know about ClickHouse, and a test can give it a sink of its own. The sink is a function that stores a batch of rows, or says why it couldn't. Std function, like the storyteller's callback, holds the lambda that main or the test hands it. In the recorder's main, it's ClickHouse, and in the test, it's a lambda, so the test needs no ClickHouse. The consumer group is called recorder, after what reads as it. Redis gives each entry to one consumer of a group, and remembers which it's given out and not had acknowledged, so two recorders share the stream, without either getting the events the other has. A batch is up to ten thousand rows. The constructor takes the connection, the consumer's name, the sink, and how long to wait for events, a second by default. And record records a batch, first what it took before and couldn't store, then what's new, and returns how many it stored. The members are the connection, the consumer's name, the sink and the wait, and whether we've made the group yet, as that happens once. Pending says whether to look at what it took before first, which is where it starts. In recorder dot c c, we take string and string view, for the rows, utility, for moving things in, and vector, for the commands, then abseil's string matching, and the events header, for the stream's name. The constructor keeps what it's given, and moves the consumer's name and the sink in, as the recorder owns them from here. The wait is a duration in milliseconds, which the read turns into block's number with count. The first record makes the group, with X GROUP CREATE, from the start of the stream, and MK STREAM makes the stream if no server has added to it yet. A group that's there already is fine, which Redis says with busy group, so a recorder that restarts carries on. Then X READ GROUP, as our consumer, up to a batch. The command starts with what any read of ours has, the group and the consumer, and count adds the batch's size. For new events, block waits up to a second for some to come, so the recorder isn't spinning. Without block, a read with nothing to give returns straight away, which is what we want for the pending ones. The id we read from is zero for what this consumer took and hasn't acknowledged, and the greater than sign for what nobody in the group has taken yet. Nothing pending means we move on to new events. Each entry's row goes into one string, a line each, with a newline after it, as JSON each row expects, and the string grows as we go, and its id into an X ACK, which acknowledges them. The loop over the fields steps two at a time, as fields and values take turns, and keeps the value of the field called row. The sink stores the batch, which for us means ClickHouse inserts it. If it can't, we return its error, and the next record looks at what's pending first, so the batch comes back. Once the batch is stored, we acknowledge it, so it doesn't come back. If the recorder stops between the two, the batch comes back once more, so an event can be stored twice, but it isn't lost, which is the side to err on for analytics. That's called at least once, and it comes from acknowledging after the insert, and not before. And record returns how many entries it took, for the log.

22:39 In recorder main dot c c, we take cpp httplib, which we use for Consul, and the recorder. Along with them come atomic, chrono, c signal and thread, for the loop and for stopping it, and abseil's status, string cat and time, for the errors and the log. Its settings come from the environment, like our other servers', with the hosts of Redis and ClickHouse, and the password. The port is a constant, as compose and production both keep ClickHouse on eighty-one twenty-three. It takes queries as plain HTTP requests, with the query in the URL or the body, which keeps the recorder to a small library we have already. Insert takes the client by reference, as main keeps a single client for all the batches. It returns a status, which is ok once ClickHouse has the batch, so the recorder knows when to acknowledge it. Insert has the signature of the sink, with the client in front, so main can wrap it in a lambda. It's in the unnamed namespace, like the port and the retry, as nothing outside this file needs them. It sends ClickHouse the batch, and keeps httplib's result, which holds the answer, or an error when there was none, such as a refused connection. Httplib's client sends the request and waits for the answer on this thread, which is fine, as the recorder has nothing else to do meanwhile. The request has the query in the URL, insert into events, format JSON each row, and the rows as its body. The query's spaces are written as percent twenty, as a URL can't have spaces, and the body's type is N D JSON, newline-delimited JSON, which is what JSON each row is. Without a result, the request got no answer at all, such as while ClickHouse restarts, and that's an unavailable error, so the loop tries again later. Any answer but two hundred is an error, with ClickHouse's words in it. Then main, which sets everything up, like the saver's. It connects to Redis first, and stops with the error if it can't, as there's nothing to record without it. Compose starts the recorder once Redis is healthy, so the connection is there when main makes it. The client takes ClickHouse's host from the environment, with setting, which falls back to localhost, like our other settings. The client is cpp httplib's, which opens a new connection for each batch, as keep-alive is off unless we turn it on. Then main sets up the client with our user and password, and the database, in a header ClickHouse reads. The consumer's name comes from the environment, recorder by default, and it doesn't change from one container to the next. Redis keeps what a consumer took and didn't acknowledge by its name, so a recorder in a new container, after an upgrade, say, takes back what the old one left pending. The sink is a lambda that captures the client by reference, and inserts the rows through it. Std signal takes a plain function for each signal, and a lambda that captures nothing can be one, which is why stopping is a global. It's an atomic flag, as a signal can arrive in the middle of anything, and a signal sets it. The loop checks it after each try, which is within a second, as read waits a second, or five after an error. The loop records a batch, and logs how many it recorded, and how long that took. On an error, it logs it, and waits five seconds before the next try. A batch of nothing logs nothing, which with a second's wait would otherwise be a line a second. In the CMake file, the recorder joins the server's library, and gets an executable of its own, nanommo recorder, and its test joins the tests. In the Dockerfile, the recorder gets a stage, like the saver's. Compose runs it, with the hosts of Redis and ClickHouse, once both are healthy, and in production, it restarts like the others.

26:21 The recorder's test, recorder test dot c c, takes chrono, memory, string, string view and vector, abseil's status and GoogleTest. It takes our events too, for the stream's name and for adding to it, and the test services, which connect to the Redis that compose starts for the tests. It runs against Redis, like the tokens' and the players' tests, and the fixture skips the tests when there's no Redis to run them against, with a message that says how to give it one. Each test starts from no stream, which deletes its group with it. That way, what an earlier run left in the stream, or a server's test, can't show up here. A helper makes a recorder for the tests. Its sink keeps the batches it stores in a vector, or fails while fail is set, as ClickHouse would while it's down. The lambda captures both by reference, so the test can look at what was stored, and set fail while the recorder runs. The sink gets the rows as a string view, so the lambda copies them into a string it keeps. And the recorder waits ten milliseconds for events, so the tests are quick. The first test has a vector for what's stored, and fail stays false, so the sink stores everything it's given. It makes the group, with a record that finds nothing, as nothing was added yet. Then it adds two rows through events, like a server, with the events class from earlier. The next record stores both, in a single batch, a line each. Value or minus one turns a failed record into minus one, so the assertion reads the count either way. And they've been acknowledged, so the next one finds nothing. The second test is the one that matters for a database that goes down. It starts like the first, with the group made, and a row added. With fail set, record takes the row, can't store it, and returns the error. Once it can, the next record stores the row it took first, though nothing was added since, as it was pending. That's what keeps the events through a ClickHouse restart, so it's the behaviour worth pinning down.

28:23 We build the tests at this step, and run the events' and the recorder's tests, with Redis, which compose starts for the tests. The row is right, and the recorder stores the events in batches, and keeps what it couldn't store.

Asking ClickHouse

28:36 Let's see it all together, from the game to ClickHouse. We start the servers and the recorder, which brings ClickHouse and Redis with it, and a hundred questing bots, for two and a half minutes. The recorder's log has the batches it recorded, a few events at a time with this many players, as it reads them as they come. Then we ask ClickHouse our questions, with its client, in the container. Counting the events by kind, with how many players did each, is a group by, like in any SQL database, and pretty compact is one of the client's formats for a table we read. Taking quests comes first, with the loot and the kills close behind, then the potions drunk and the levels reached, and a single quest turned in so far. Then the quests, by name, taken against done, with count if, which counts the rows that match a condition, in one pass over the table. Grouping the quest events by what gives a row for each quest, and ordering by how many took it puts the most taken first. Bones by the Gate is taken the most, as most bots start in the Meadows by its guard, and one bot has finished it in this time, as each needs three skeleton kills of its own. And the items, the ones that came into the game, from loot and prizes, against the ones used. Sum if adds up the counts of the rows that match, like count if does, and the items that came in most go first. Health potions come in from skeletons and quests, and go out as the bots drink them when they're hurt. More went out than came in here, as each new player starts with three, which isn't an event. If an item came into the game a lot faster than it went out, this is where we'd see it, and a query by kind would say whether from loot or prizes.

A dashboard of the game

30:16 Grafana reads ClickHouse through a plugin, Grafana's own, so in compose dot yaml, Grafana installs it as it starts, before it reads its data sources, pinned to a version, like our images. ClickHouse becomes a data source, next to Prometheus, Loki and Pyroscope. The plugin talks ClickHouse's native protocol, on port nine thousand, inside Docker's network, so that port isn't published, with our user and database, and the password in secure JSON data, which Grafana keeps encrypted. Then a dashboard of its own, nanoMMO game, next to the server's, as it's for whoever runs the game, not whoever runs the servers. Each panel is a query in ClickHouse's SQL. The dashboard opens on the last fifteen minutes, and refreshes each ten seconds, slower than the server's, as these queries count rows, and nothing here changes that fast. Each panel has an id, and a place on the grid, with the kills a minute along the top, beside the players. Its target names the ClickHouse data source by its uid, and has its SQL as raw SQL, with a format, a table or a time series, which tells the plugin how to hand the rows to the panel. The first counts the players who did something in the dashboard's time range, with uniq, ClickHouse's approximate count of distinct values, which is quick, and close enough for a dashboard. The number is a fixed green, as Grafana colours a stat by its thresholds otherwise, and a count of players has none worth colouring. Kills a minute groups the kills by the minute they happened in and by monster, as a time series, a line for each kind of monster. Its format is a time series, zero, which the plugin reads as a series for each value of what. To start of minute rounds each time down to its minute, so the group by gives a count a minute for each monster, ordered by time for the panel. Time filter is the plugin's macro for the dashboard's time range, so ClickHouse reads the parts of the table in it. Quests taken and done is what we asked ClickHouse by hand in its client. Its rows have a quest's name and two counts, which come out side by side for each quest, ordered by name, so each quest stays in its place from one refresh to the next. So are the items, in and out, with a bar for each item. It reads three columns, the kind, what and the count. Sum if adds up the counts of the rows that match, the loot and the prizes as what came in, and the uses as what went out. Ordering by what came in puts the items the game hands out most at the top. Where players die in the Meadows puts each death there at its x and z, as an X Y chart, with both axes across the whole land, so it reads as a map. Each land has a map of its own, so the query keeps to one. It returns two columns, x and z, and reads the kind, the land and the time besides, five of our thirteen, which is where a column store does well. Its where clause is on the kind, like the others, so ClickHouse reads the death events and nothing else, as the events are sorted by kind. Deaths are rare at first, so the map fills in as the bots play on. The chart's options take its columns as they come, x then z, as points a few pixels wide, and both axes go from minus to plus a hundred and eighty metres, which covers the land. And levels reached counts how many times each level was reached, which shows how quickly players go up. To string makes each level a label, so the bar chart has a bar for each, in order. Grafana's bar chart takes its labels from a text column and its values from the numbers, which is why the level is made a string. The panels' types are Grafana's own, a stat for a single number, a time series, bar charts and an X Y chart, and each reads the rows it gets back by their columns.

34:02 With a hundred and fifty bots playing for a few minutes, the first panel counts the players who did something. Kills a minute has a line for each kind of monster, with the skeletons around the middle of the Meadows on top, as that's where the bots start and the guard's quest sends them. The quests, taken against done, show how many of the players who took each quest have finished it so far. The items show what the game hands out, against what's used. Where players die marks each death in the Meadows on its map. In these few minutes, none of the bots has died there, so the map is empty, but a spot that's too dangerous for the players around it would stand out here. And the levels reached show how far the bots get in a few minutes.

Wrap-up

34:46 So, today we added ClickHouse, a column-oriented database, with a table of the game's events, sorted by kind and time. The world records what happens in it as events, kills, drops, quests, levels, potions and deaths. Once a tick, the server adds them to a Redis stream, in a single round trip, so the game doesn't wait on its analytics. The recorder takes them from the stream as a consumer group, in batches, into ClickHouse, and what it couldn't store comes back on the next try. We asked ClickHouse which quests players finish and what comes into the game, and Grafana has a dashboard of the game, from ClickHouse.

Playing it

36:03 Let me know in the comments 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.