dimanche 23 août 2015

In Node.js, how can I efficiently design a system that performs actions every X seconds?

In my node.js app, I want to perform a function after X seconds.

This is what I'm doing:

  • a user posts an item
  • after 60 seconds, my server needs SMS another user to "vote" on that item.
  • my server will continue to send SMS to different users until someone votes on that item, at certain intervals. (60, 80, 100, 120)
  • once someone votes on that item, the item disappears forever and nothing more is required.

I need a way to do this that is precise, because my app is in real time. I will be doing a lot of these "jobs" (imagine lots of users posting items), so I want something that is reliable and can execute exactly on 60 seconds. (+ - one second is fine, but it can't off by more than 2 seconds).

I've thought about using setTimeout...but I'm not sure if that's scalable when 10 thousand of users are posting every hour. Plus, it seems like a hack because I'm not sure if setTimeout "uses" the main thread, and Node.js only uses one thread.

What is the best way to achieve my goal?

Aucun commentaire:

Enregistrer un commentaire