Search code examples
multithreadingperformancesessionserver-sidelarge-data

Does a game server create threads for each user request (like dota 2)?


For a user base of 100,000 and 4 users per game session, should we create new threads for each request such as create_session, move_player, use_attack, etc. ?

I wanted to know what would be the optimal way to handle large connections because if we create large number of threads, context switching will eat up most of the cycles and if no threads are created each request has to wait for previous request to complete.


Solution

  • I would avoid thread-per-connection if your goal is scalability. It would be better to have a queue of events and a thread pool.

    A game company would probably use a non-connection-based internet protocol like UDP. All requests can theoretically come in on the same socket, so you only need 1 thread to handle that. That thread can assign work to other threads.

    You can have a larger threadpool where any thread can be assigned any job. Or you could further organize the work into specific jobs, each with a threadpool to process a queue of tasks. But I wouldn't launch a new thread for each request.

    How you design your threadpools and task distribution system depends on the libraries for whatever language you're using and the application requirements.