1
\$\begingroup\$

I'm trying to create a turn based multiplayer online game where players can be grouped in a room/lobby and play (similar to Ludo but number of players can be more than 4). There can be multiple such rooms at a time.

Here is brief overview of the game: The game is not resource heavy. Each player gets his turn to perform some actions for some time (~90 seconds) and meanwhile other players just communicates with server to get the actions performed by the player. There can be multiple game rounds and a winner is declared at the end.

The game state of each room would be different at a given time. Therefore, to manage the game states for all the rooms, I could think of two approaches:

  1. Multithreading approach: Spawn a new thread for each room and run/manage the game loop and corresponding game states in room specific threads. But if there are too many rooms, the number of threads would be very large and this approach is not feasible as per this answer's comment. However this approach is preferable for mass multiplayer games online games (MMO) as per this answer. I also think this should work for considerable number of rooms because most of the time, threads are waiting for player's actions.
  2. Asynchronous approach: Here, the game states of all the rooms would be stored in server's memory and whenever any requests is received from any player, the game state object will be retrieved using player's room ID and based on the state, the server will command players in that room. The state will then be updated. There will be only one main thread here.

So my question is what would be the better approach for this type of games, considering the future scaling requirements? Also suggest any better alternatives which I couldn't think of.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can get the best of both worlds by creating as many threads as you have CPU cores of the server (you might want to make this number configurable in case you have other unrelated threads running at the same time which also require non-negligible CPU load). Then make each thread responsible for managing a set of currently active rooms in sequential order.

To get started you might want to just distribute your game rooms in a round-robin manner. But depending on the usage patterns of the game you might add a more complex load-balancing logic, like assign each room to the thread with the least rooms, or actually measure how much idle time each thread has so you can distribute rooms more evenly.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .