Question

I'm working on a website which only registered users can access, and they are manually added to the site, since there is no registration enabled. Think of it as a private shop. The number of users is below 1000.

Each of the users can set a couple of events, such as birthday. They usually add less than 10.

I want to inform the user by email about their events, when there is a couple of days remaining to reach that event.

Now the question is, is it performance-wise better to run a single cron job every day at 00:00 and loop through all users and check their events ( stored as metadata ), or should I set a yearly cron job for each event of each user ( which ends up in having a thousand of cron jobs )?

Was it helpful?

Solution

Run a single cron, it's much more manageable and more stable, and performance shouldn't be an issue since you do 99% of the work of selecting which user should be notified in MySQL, which will be extremely fast.

I'd use a table to save each day (and each user on that day) that my cron has successfully run for, to make it more resilient. Plan for failure, not for success, because it will fail at some point, and the importance is how bad that failure will be and how much time it will take you to fix the fallout.

Server crashes right before midnight and is rebooted at 00:05? No problem, just run the job manually, or wait till the next day, it'll run two days in one run!

Server crashes while executing the cron? No worries, the next time it runs, it'll pick up right where it stopped last time.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top