#43 Multiplayer Servers: Network Ping
When I was kid I learned programming out of necessity (you can read about it here). Then I learned to get better because I wanted to create…
This is post #43 of my #365 day series.
Press enter or click to view image in full size Photo by Lorenzo Herrera on Unsplash
When I was kid I learned programming out of necessity (you can read about it here). Then I learned to get better because I wanted to create stuff, namely video games. Honestly, it was some of the best training I’ve ever had as a programmer. Why? Because it required I understand so many different programming and other computer science concepts. I had to learn about memory management because back then you couldn’t just throw game assets up and hope that you had enough RAM or VRAM (if you were so lucky) to handle it all. I had to learn about pixels and how to create masks and other features that are easily implemented by game engines these days. And most importantly (at least for this article), I had to learn about how to deal with network packets and managing user state between a centralized server and its user clients.
So what people don’t understand about the difficulty around multiplayer is that we have to maintain data and its changes in basically real time between clients. Have you ever thought about what it takes to ensure that a player’s location and movement on their device is synced up in real time on a multiplayer server PLUS synced with every other client connected to the same multiplayer server? Would it surprise you if I told you that it’s actually a lot of guess work that goes into it? No joke, that’s how it’s done. Take a simple scenario of a game of pong played across the internet.
Player A connects to Server S and so does Player B. Player A starts the game and hits the ball at a 45 degree angle (or whatever the unit of measurement) and sends that data to Server S, the server then updates it’s own data and calculates where the ball should be in space and relays that data to Player B. Already we’re dealing with issues. So the physical speed limit for the transmission of data is the speed of light, but we’re no where even close to that when it comes to transmitting that data over the internet (or even in a local area network). There’s the concept of network ping that we have to account for, which is essentially the time it takes to send one packet of information on a roundtrip from the client device (think your laptop or cell phone) to a server, and for that server to send you confirmation back. Typically (on a good server connection) floats around 10ms in a best case scenario. This matters because by the time data travels from Player A to the Server S, we already have issues with the data not being in sync with each other. By stacking the ping delay, we have about 20ms delay between Player A and Player B.
So how do multiplayer videogame servers deal with this asymmetrical information? By taking an educated guess. So in a more advanced example, Server S would calculate the ping and account for the speed of the ball and position of the hit by Player A to calculate where the ball should be in space given the delay between Player A and the server. Then it would send that data to Player B, taking into account the time it will take to get to Player B’s device. The takeaway from this approach is that multiplayer is not perfect in video games. It’s just a lot of really good guesswork, but with the magic of math and statistics, we can be really good at guessing.