Custom Load balancing servers by number of players - DigitalOcean gaming solutions
Hi, as a hobby I develop multiplayer web games (agar.io type games). After a conversation with a member of the DigitalOcean team where I shared how I load balance traffic to my web games, they encouraged me to share this bizarre problem and its solution on this forum. So here it is!
Typical load balancers spread out requests to servers evenly, attempting to strike a balance so that individual servers are not overwhelmed with traffic. Whilst this works great for almost every application, it does not work for the games I develop. I need to balance servers based on the number of players in each server - a custom metric. Although the load/stress on a server is an indication of how many players are on a server, it is obviously impossible to accurately guess the number of players connected to the server from its load.
When the website loads the static content in the browser, it then connects to the game server with a WebSocket. With standard load balancers the connection could potentially be directed to a server that has already reached the player limit (an arbitrary value that is set to make sure the game doesn’t feel crowded and to balance gameplay). This would mean the connection would be rejected by the game server then the client needs to try again, you can see how this could result in an infinite load loop, especially if the connection keeps getting routed to the same server by the load balancer.
I would love to hear what other people’s solutions are to this problem, I have left my current solution as an answer.
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.×