Sign in to follow this  

running multiple game instances on nodejs

Recommended Posts

Server does some collision detection,  game logic and handles input from clients.

Nothing too heavy but I can imagine it could start getting slow if there's a lot of game instances.

setInterval(() => {

    for (let game in games) {

}, 1000/30);

Is there any way I can distribute the load to more cores, or because of it's single threaded nature, nodejs was a bad decision for this particular game architecture?


Edited by gerardAlbin

Share this post

Link to post
Share on other sites

With NodeJS there are several ways to benefit from multiple cores, or other process distribution.  My preference is usually "PM2".  Then within your application design you will need to engineer how each instance defines and manages its own responsibilities - e.g. which games it is managing, what happens if that instance fails, and so on.

You may also wish to reconsider using setInterval as a reliable thresholded tick?  An alternative might be to loop faster than desired, measure the cumulative deltaTime within the loop, and if greater than the minimum tick then run the respective game updates (passing the deltaTime forwards), and resetting the cumulative deltaTime.  Additional checks for unacceptably slow deltaTimes can trigger other strategies - such as passing responsibilities to other instances, or spinning up more instances etc.

Many challenges lay ahead in this topic - enjoy the journey!

Share this post

Link to post
Share on other sites
28 minutes ago, gerardAlbin said:

Im not sure what you mean by using setInterval as a reliable thresholded tick. 

From the NodeJS documentation:


The only guarantee is that the timeout will not execute sooner than the declared timeout interval.

That is, setInterval can only "try" to call in the prescribed time.  If it's blocked, then the time elapsed will overrun.  Game updates can be heavy and so can easily create blocking scenarios, even at 30 FPS.

I don't want to provide code because I'm not yet sure I like the use of setInterval for this purpose?  I'd instead suggest reading this and properly exploring all the options:

Plus getting a good understanding of delta-time (in respect to game updates) will be a valuable foundation:


Share this post

Link to post
Share on other sites

Thank you, I learned a lot about event loop timers.

I forgot to mention my game uses socket io so I couldn't get PM2 to work as it should.

I'm going implement the cluster module manually, but there's a couple of issues.

Im trying to make a proxy server on the top of the node workers so when a client connects to the server, the proxy server makes a decision on which worker the client should be routed.

I want to redirect the client to the node instance for which some conditions exist. Like for example if the client wants to join a room search all node instances and connect to the instance the first free room is found or if the client wants to make a room, redirect him to the instance which has the least rooms. I have no idea how to make a proxy server though, is this even a good idea?



Edited by gerardAlbin

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.