2

I'm trying to build a NodeJS web service to send out emails at specific times, but in the most efficient way possible.

Google Keep reminder feature is similar to what I want to do. You can schedule a time to be reminded about something. But how does it work?

The only solution I can come up with about how this work is a cron job that runs every min and sends out the notification to all users for reminders scheduled at that time.

But Google must have a database of all of these tasks, and millions of them. Isn't it inefficient to run a task querying through millions of data every min?

So, I'm not convinced that this is how Google Keep reminders work, but cron is the only method I know of.

Vongdarakia
  • 379
  • 1
  • 12
  • 25
  • If you keep a sorted list of upcoming reminders, then it's trivial to check the start of the list every so often to see which pending reminders should fire their notifications now. Depending upon your scale, you can keep the next hour or day of notifications in a sorted list (even in memory) and then on some periodic schedule, you check the database to update the sorted list for the following hour/day, etc... Anytime you add a new event to the database, you see if it belongs in the current sorted list of not too. This way you rarely querying the database to see what is next. – jfriend00 Jun 25 '16 at 22:06
  • You could, of course, have a sorted database table just for upcoming notifications and let the database tell you what the next N items are. – jfriend00 Jun 25 '16 at 22:07
  • @jfriend00 That would solve the frequent querying issue. We are assuming that a user will schedule a task more than an hour/day ahead though. If a user schedules a task for a time between a query that just took place right now and the next query then that task won't be in the list. Also, the cron job will still have to be run every minute to go through that list and send out the notification if an is set for that time, right? Isn't that still inefficient? – Vongdarakia Jun 25 '16 at 22:17
  • You don't have to schedule a chron job for every minute. You can just set a timer for the next scheduled event notification. Then, there's no polling. Your timer just fires when it's time for the next notification. When that fires, you then remove it from the sorted list and schedule a timer for the next one in the list. When you add a new notification, if it ends up at the front of the list, you cancel the current timer and schedule a new timer for the first item. – jfriend00 Jun 25 '16 at 22:28
  • Rather than traditional database storage, think of this as a queue. Whenever a notification is scheduled, you insert it into the appropriate place in the queue. A timer is always set for the next item in the queue. When it fires, it removes itself from the queue and sets a timer for the next item in the queue. If the first item in the queue is ever replaced with a sooner notification, then the current timer is cancelled and new timer is set for now new first notification. – jfriend00 Jun 25 '16 at 22:30
  • @jfriend00 Interesting. Never thought of using timers before. So every time a reminder is added to the database, and if that time is the earlier than the current reminder's time, I reset the timer to the earliest reminder? Lets say that there are hundreds of reminders for one specific time. The process is (send notification -> reset timer to next in queue) * 100. Perhaps grouping up tasks with the same time will make this more efficient? And I can have a job query the database every how to see if the queue correct. How do you feel about this approach? – Vongdarakia Jun 25 '16 at 22:57
  • 1
    When a timer fires, you can easily check the queue and see if there are other events set for the same time or within a close increment of the same time and then fire them in proper order, all on the original timer event. – jfriend00 Jun 25 '16 at 22:59
  • @jfriend00 That's a good solution too! I will play around with this approach, thanks! – Vongdarakia Jun 25 '16 at 23:22

0 Answers0