The Hangfire fire-and-forget BackgroundJob.Enqueue method can seemingly immediately return back to a caller but it comes with a cost to the database. Here’s recommendations how to use Enqueue:
- The number of parameters in the passed function in Enqueue should be minimal.
- Using Enqueue for operations that are running sparsely or executed by Platform admin manually is acceptable (import, indexing on demand etc).
- Using Enqueue for operations that are can potentially have high load (are in user called APIs) is not a good practice. This can potentially create a high load to the database and flood it with Hangfire requests. In theory Hangfire wasn’t designed to be used this way, so we recommend looking at the alternatives using the event model (RabbitMQ etc).