Search code examples
c#.nethangfire

Can Hangfire be configured to use multiple storage simultaneously?


I am using Hangfire in my application and I want to configure it to use Redis and Memory as the storage for different queues. The reason I want to do this is that I want to handle some tasks which are not important for me to keep their data in Memory storage, which has a faster speed, and some other tasks in Redis storage, which has a higher delay but is more reliable in case of application restarts.

It's important to note that I want to use Hangfire's "fire and forget" feature only.

Is it possible to configure Hangfire to use multiple storage simultaneously for this purpose? If so, how can I achieve this?


Solution

    1. for multiple dashboards use different stores, refer to https://docs.hangfire.io/en/latest/configuration/using-dashboard.html#multiple-dashboards
    2. for multiple job servers, https://docs.hangfire.io/en/latest/background-processing/running-multiple-server-instances.html, in addition to that, initialize redis storage and memory storage, create two job servers by AddHangfireServer and pass in storage.
    3. for scheduling to different job storage, resolve IBackgroundJobClientFactory from DI (assume AddHangfire() was done), and use GetClient(JobStorage storage) to create different IBackgroundJobClient for redis and memory storage, from here, you can enqueue job to different storages.