Thursday, February 1, 2018

mmo - Is running cron jobs every minute a good practice for browser-based games?


I'm trying to create a browser-based game (made in PHP, MySQL, Js, HTML, and CSS) that is heavily dependent on user's actions being carried out after a certain amount of time passed whether online or offline. Will running cron jobs every second/minute a good practice or a solution to my problem?



Please check out this question first before answering, I ask this new question to focus more on the pros and cons of using cron jobs. To be more specific, here's a list of the things that I want to know:




  1. Scalability? Up to what extent?




  2. Performance? How fast? How slow?




  3. Server Overhead? Will a normal server be able to accommodate the load?





Assuming the game will be an MMO.


I appreciate all the help people! Thanks in advance!



Answer



If you're smart with it, it won't be too resource intensive - but I definitely wouldn't try to run it every second (plus the shortest time interval for a cron is 1 minute). Because you mention actions occurring for a user when offline, a cron is probably your only option for that.


For online actions, I recommend not having that in a cron, that way you can do actions on a per user basis, immediately, rather than batched in a cron.


If you're not already set on PHP as the backend, you should take a look into node.js combined with Websockets or socket.io - it will allow you to keep a persistent connection, so if you want something to happen after a minute you can just use setTimeout. Based on what the other question states, that sounds like it'd be the best option for what you're trying to do.


Alternatively (again based on your other question, you could use setTimeout on the client side, and pass the info to the server to call the action. Of course, you should verify on the server that the correct amount of time has passed. This won't work if the user goes offline before the timeout callback is called (if you really wanted to, the cron could be a fallback for this scenario).


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...