Custom Load balancing servers by number of players - DigitalOcean gaming solutions

Hi, as a hobby I develop multiplayer web games ( 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.

Submit 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!

Sign In or Sign Up to 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.

Want to learn more? Join the DigitalOcean Community!

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.

My current custom load balancing solution: Each game server reports the number of players it has whenever the number changes to a serverless function in AWS. This function stores the number of players this server has in a database. When the browser client loads, before trying to connect to a game server via WebSockets, it first sends a http request to a serverless function that looks up the server in the database with the least number of players and returns the IP address. The browser then connects to this server. This solution does have some extra steps but it means it scales beautifully because it is all done through serverless functions - it also means I get brilliant metrics on how players are distributed.

Note: Some people might wonder “why not host the game server with AWS as well?”. The reason not is because low tier AWS servers throttle really badly if you have an intensive application running non-stop (like a game server). DigitalOcean is one of the only providers I found that doesn’t throttle like this at low price levels. I would also like to say that if DigitalOcean did start to offer serverless functions I would switch to using them instantly!