When is it advisable to use setTimeout vs Cron?

Hi guys I am creating a Meteor app that uses mongo database.

I have a collection that could potentially have 1000 documents that need to be updated at different times ...

Do I run setTimeouts on creation or a cron job that runs every second and iterates over each document?

What are the advantages and disadvantages of each?

to put this in context:

I am creating an online tournament system. I can run 100 tournaments, which means I can have 1000 matches.

Each match must absolutely be completed at a specific time and may end earlier on condition.

+3


source to share


2 answers


Using an OS level cron job will not work because you can only check with 60 seconds resolution. So for example "cron job" I think you mean one setTimeout

(or synced-cron ). Here are some thoughts:

Single setTimeout

strategy . Every second wakes up and checks for a large number of matches, updating those that are complete. If you have multiple servers, you can prevent all but one from checking through synced-cron.

The advantage of this strategy is that it is easy to implement. Disadvantages:

  • You may end up doing a lot of unnecessary database reads.
  • You must be extremely careful that your processing time does not exceed the length of the period between checks (one second).

I would recommend this strategy if you are confident that the runtime control can be controlled. For example, if you can index your matches on endTime

, so only a few matches need to be checked in each loop.

Multiple setTimeouts



strategy : add setTimeout

for each match when creating or starting the server. At the end of each timeout, update the appropriate match.

The advantage of this strategy is that it potentially removes a significant amount of unnecessary database traffic. Disadvantages:

  • This can be a little tricky to implement. For example. you should consider what happens when you restart the server.
  • A naive implementation does not scale per server (see 1).

I would recommend this strategy if you think you will be using a single server for the foreseeable future.


These are the compromises that happened to me given the options you chose. A more robust solution is likely to come with off-stack meteor / mongo technology. For example, keeping the match time in redis and then listening for key notifications .

+3


source


It all depends on preference, to be honest with you.

I'm a big fan of writing small, independent programs that each do one thing and do it well. If you are that similar, it might be better to write separate programs to run periodically via cron.



This way you get guaranteed time-to-time accuracy and small, simple programs that are easy to debug outside the context of your webapp.

This is just a preference.

+1


source







All Articles