We left Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

Hangfire: separate background jobs for each user OR just one background job that is run frequently

We are using .NET 6, Hangfire.AspNetCore 1.7.31 and Hangfire.PostgreSql 1.9.9. We would like to implement a way for users to subscribe to email notifications where they can choose their own notification interval. If you are familiar to Jira, it is very similar to its filter subscriptions.

enter image description here

We see two ways of implementing this functionality:

  1. Create a RecurringJob for each user subscriptions, each with its own time interval. How does this scale to 1000s of users? Does Hangfire create a separate thread for each RecurringJob? Can RecurringJobs be added at the runtime when user subscribes to a notification?

  2. Have only 1 frequent RecurringJob (ex. every 1 minute), which checks which users need to be notified and sends an email.

Which option is the preferred way of doing it in Hangfire? The solution should be as efficient as possible.

I would go with the second option. The first option would be tough to maintain. The user could also change their preference and then you’re getting into deleting the job and creating a new one. If the lowest option is in hours you don’t have to check every minute. For each user that matches you can queue a job to process the notification for the individual user.

I don’t think Hangfire has a preferred way. It is just the shell.

I agree with @aschenta in that Option 2 sounds simpler to maintain. Another item to consider is how many users the system must handle? Theoretically, Option 1 requires that many jobs which could be a drain on your server resources. The biggest advantage to Option 1 would be scheduling would be simpler to code initially if you lean on Hangfire and its custom attributes to ensure the same job doesn’t run while the previous job is still running, but having a whole bunch of jobs fire simultaneously could produce unexpected load or results. The biggest disadvantage of Option 2 would be you have to take responsibility for the evaluating cron expressions for the individual user notifications, which makes the initial coding more complex.

Whatever you choose, good luck!