Guide

Hangfire AutomaticRetry explained: attempts, delays and when to stop retrying

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.

The defaults

AutomaticRetryAttribute is registered as a global filter out of the box, so it applies to every job:

How long the retries take

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:

AttemptDelay before itElapsed since the first failure
115–44 sunder a minute
331 s – 2 min1–4 min
54.5–7 min7–14 min
722–25 min40–53 min
969–73 min2.5–2.8 h
101.8–1.9 h4.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.

Changing it per job

// 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() { ... }

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.

Changing the global default

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.

Retries need idempotent jobs

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:

The part retries hide

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.

Recap

← All articles Try QueueHawk free →

Every failed attempt, with its stack trace

QueueHawk records each failure before Hangfire retries it — and alerts when the error rate climbs. Free for one application.

Start free