A Quick Overview on Hangfire.io

Hangfire.io is a nice tool to perform background processing in .Net. Those background jobs can go from simple method calls you don’t want to block the user interface for up to recurring tasks for maintaining your application.
It’s licensed under the LGPL v3 and can be used in closed source software. Should you need more features or support, you can take the Pro version.

 

Main Features

Hangfire offers 4 types of jobs in the LGPL version:

  • Fire-and-forget: executed only once* and started after creation.
  • Delayed: executed only once* and started after a specified time.
  • Recurring: executed many times with a schedule in CRON style (every third Tuesday in a month).
  • Continuations: executed once* when the parent job is done.

* To be more precise, Hangfire stops doing your job when it succeeds the first time. Depending on your job that may be close enough at “execute once”.

All those types are persisted and will be automatically retried should they fail. You don’t need to write (and test) code for that. You can focus on what type of problem you have, pick the right job type and Hangfire does the rest.

The dashboard shows you what is going on inside Hangfire. You can mount it as a OWIN component and then have a dashboard right inside your web application.

Hangfire uses SQL Server to persist the jobs and their definition. However, if you need a different database or Azure, you can use one of the many storage extensions.

 

Architectural Overview

Hangfire has these 3 main components:

  1. Hangfire Client: to add and create jobs.
  2. Job Storage: to persist and track the state of the jobs.
  3. Hangfire Server: the place where the jobs are executed.

How those parts work together is well explained in this graphic from the official documentation:

You can run the client and the server in the same application. If you use an OWIN module, your requests will not slow down much. But when the application isn’t running, your background jobs aren’t processed.

We use a separate Windows service for the Hangfire server. That service is started automatically and keeps running all the time. That requires a bit more upfront work, but tools like Topshelf make the creation of Windows services easy.

 

Helpful Links

Those links helped me to understand how Hangfire works and what I could do with it:

 

Why not Async/Await?

Async/await solves a different problem. If you want to do a long running task in the background and not block your main (user interface) thread, then async/await is the way to go. If your application restarts while the task is still running, you will lose the work. There is no persisted state and no automatic retry in the case of an error. You can implement all that, but Hangfire offers it out of the box.

 

Why not a Message Bus?

So far I don’t have the need to use a generic messaging system. With Hangfire I can use a tool that implements features I’m used to from Ruby (like Sidekiq, Resque, delayed_job) without much configuration. I don’t get the flexibility of a messaging system, but I also don’t have the complexity.

 

Some Preventable Pitfalls

The dashboard works by default only for localhost. If you try to connect from a different server, you will get a 403 error. If you don’t know that you may spend hours with the IIS configuration.

Background jobs do not magically increase the resources of your machine. If you have long running tasks (especially CPU-bound), then those jobs can block your web server. If you keep the Hangfire server who processes those jobs on the same machine as your web server, you may pay a penalty in form of a longer response time. That can be unrecognisable, small (~100ms) or big (more than a second). Try the simple set-up with the background server on your web server first. If it takes too long, then you can move the background server to a Windows service on a different machine.

 

Conclusion

Hangfire is a well thought-out tool that doesn’t need much configuration. If you need to have a quick solution for background jobs and recurring tasks, then this is a tool worth looking at.

1 thought on “A Quick Overview on Hangfire.io”

  1. Great !!!

    any full sample TopShelf WinService like HangFire Server ?

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.