When a Hangfire job throws, it is not failed — it is scheduled again. Ten times, by default, spread over roughly four and a half hours. That is a good default for a flaky network call and a bad one for a job that sends an email before it crashes. Here is exactly what AutomaticRetry does, and how to tune it per job.
AutomaticRetryAttribute is registered as a global filter out of the box, so it applies to every job:
DefaultRetryAttempts = 10).The delay before attempt n is, in seconds:
(n - 1)^4 + 15 + random(0..29) * n
The random part spreads retries out so that a thousand jobs failing on the same outage do not all come back in the same second. In practice:
| Attempt | Delay before it | Elapsed since the first failure |
|---|---|---|
| 1 | 15–44 s | under a minute |
| 3 | 31 s – 2 min | 1–4 min |
| 5 | 4.5–7 min | 7–14 min |
| 7 | 22–25 min | 40–53 min |
| 9 | 69–73 min | 2.5–2.8 h |
| 10 | 1.8–1.9 h | 4.3–4.7 h |
Execution time comes on top. So the default answer to “when do we find out a job is broken for good?” is: about half a working day later — if someone looks at the Failed tab.
// No retries: fail immediately and stay in Failed
[AutomaticRetry(Attempts = 0)]
public Task SendInvoiceEmailAsync(int invoiceId) { ... }
// Three quick retries for a flaky API, then give up
[AutomaticRetry(Attempts = 3, DelaysInSeconds = new[] { 10, 60, 300 })]
public Task SyncCustomerAsync(int customerId) { ... }
// Retry, but delete the job afterwards instead of keeping it in Failed
[AutomaticRetry(Attempts = 5, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public Task RefreshCacheAsync() { ... }
DelaysInSeconds replaces the formula with fixed delays per attempt; once the array runs out, the last value is reused.OnAttemptsExceeded is Fail by default. Delete moves the job to Deleted instead — fine for a cache refresh that the next scheduled run will redo, dangerous for anything you would want to investigate.LogEvents = false silences the per-attempt warnings.OnlyOn and ExceptOn limit retries to certain exception types or exclude some. Retrying a HttpRequestException makes sense; retrying an ArgumentException just fails the same way ten times.The attribute works on the method or the class. One trap: Hangfire reads filters from the type the job was created with. If you enqueue through an interface — BackgroundJob.Enqueue<IInvoiceService>(...) — put the attribute on the interface method, not only on the implementation.
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 3 });
A method-level attribute still wins over the global one. Lowering the global default is worth considering if most of your jobs either work on the second try or not at all — ten attempts over four hours mostly delay the moment someone finds out.
A retry runs the whole method again. If the job charged a card, sent an email or wrote half its rows before it threw, the retry does it again. Two rules keep that safe:
Retries are why a broken job looks harmless for hours: between attempts it waits as a scheduled job — the dashboard lists it under Retries, not Failed — and the failed counter stays at zero until the last attempt is used up. Unless someone opens that page, nothing points at it. If a deploy breaks a job that runs every five minutes, you get a growing pile of jobs quietly waiting for their next attempt.
QueueHawk records every failed attempt as it happens — exception type, message and stack trace — before Hangfire's retry moves the job on, and its error-rate rule alerts when N attempts of a job type fail within a time window. That is the signal that arrives in minutes rather than at the end of the retry chain. More on the alerts in Slack, Teams and email alerts for Hangfire.
(n-1)^4 + 15 + random·n seconds, about 4.5 hours in total, then Failed.Attempts, DelaysInSeconds, OnAttemptsExceeded, OnlyOn/ExceptOn. Put the attribute where the job type is — the interface, if you enqueue through one.GlobalJobFilters.Filters.QueueHawk records each failure before Hangfire retries it — and alerts when the error rate climbs. Free for one application.