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.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.
Click below to sign up and get $200 of credit to try our products over 60 days!