How lag works in .io games

5/15/19

Lag in Cavegame.io

Lag is a common problem not just in Cavegame.io, but in any .io game. Cavegame.io on its own is a special case though! This is because there is so much dynamic data, since the map is a bunch of blocks that can be changing all the time. I will go over some myths of lag in servers, especially .io games and blocks, and then the reality of how I go about finding flaws in the game's performance.

The Myths

One myth with the efficiency of games is that lots of one type of mob or item will turn into a lag generator. This is true if there is a hugely unreasonable of that item, but a relatively large number is usually never a problem with a well designed game. And this is also the case with the blocks in my game. The reason this is a myth is because games are optimized to handle huge numbers of non-player entities. They are especially optimized when they are off-screen from the player view, because in a multiplayer game, this usually means that they do not have to tick, since there is no one viewing it who cares what happens to that object.

A myth particularly in my game is that lots of one type of block will cause lag. This is absolutely not the case. In fact, I tried turning every stone block into a gold generator on the map. It caused no lag whatsoever. The reason is that with each server tick, there are only a few blocks that the server actually goes through and ticks. There is an illusion that these blocks are always doing something, but the truth is that they are only checked once in a while. This is probably also how Minecraft works when it comes to things like farming or other events.

A final myth is that choppy movements are evidence of slow rendering. In single player games, this is true. However, in multiplayer games, choppy movements are usually not caused by the client-side rendering. Instead, choppiness can be a result of a slow internet connection, or the server not sending data with consistency. This is the problem that I actually have to solve when I say that I am working on fixing lag issues. If the server is doing a lot of work, it will send out messages at strange moments which causes choppiness for the client, since they do not know where their player should be (as the server usually tells them almost instantly).

Diagnosing Lag Issues

To actually diagnose lag issues, I usually go into the server and look for places that the server is doing wasteful work. For example, if I see that the server is looping through all of the mobs, but some of the mobs are not affecting players, I will first go through the players and then identify mobs that are near them. If there are mobs near them, I tick only those mobs.

One of the central ideas of avoiding lag on the server is to used some position-based storage system for all data. Looping through a huge array of data is a wasteful operation, and should be avoided at all costs. Instead, the solution is to use libraries specifically designed for spatial indexing of objects that do not fall perfectly into a grid. As for the blocks in the game, these fit perfectly to a grid, so I can fit them into a 2-D array. Then, I make an effort to never loop through the entirity of this block array. Instead, I locate the exact blocks that I need to access, and never do more than that.

The other key of minimizing lag is minimizing network throughput. The more that the client and server can infer about data, the less that needs to actually pass through the network. This is why I switched to a framework which forces me to enforce the data being sent to specific rules, but once doing so, allows me to reduce how many bytes are sent in each message. The other key is that I should never send data that has already been sent. This is especially essential for block data. Users have over 100 blocks on their screen at once, so I must remember on the server what blocks the user already knows and not send them over again as repeats.

Bots

To test points of lag, I usually use bots, and see what maximum capacity my server can handle before it starts to deteriorate. To simulate this, I have the bots move around and send and recieve messages like normal players. If the server can handle very few bots, I know that I need to make improvements.

Want to learn more? Join the Cavegame.io Discord to chat!