Skip to content

Retry & Dead Letter Queue ​

Retry policies ​

Hangfire durably schedules retries for GrydJobs adapter invocations. GrydJobs captures an immutable retry-policy snapshot in the scheduling envelope when the job is submitted, so later configuration changes do not change retries for work already in the queue. The submission's MaxRetries override wins when supplied; otherwise the normalized per-type policy wins, followed by the default policy.

MaxRetries counts retries after the initial attempt. A value of 3 allows up to 4 total attempts; 0 disables retries and sends a failed job directly to the dead letter queue. Hangfire stores each retry and its backoff as provider state. The durable worker lifecycle records completed attempts and the final job state. A worker shutdown that interrupts an attempt does not create a completed execution row. Processing is at least once, so jobs that perform external side effects should remain idempotent.

csharp
builder.Services.AddGrydJobs(options =>
{
    options.DefaultRetryPolicy = new RetryPolicyOptions
    {
        MaxRetries = 3,
        BaseDelay = TimeSpan.FromSeconds(10),
        MaxDelay = TimeSpan.FromMinutes(5),
        BackoffType = BackoffType.Exponential
    };

    options.RetryPolicies
        .Add<CriticalJob>(new RetryPolicyOptions
        {
            MaxRetries = 10,
            BaseDelay = TimeSpan.FromMinutes(1)
        })
        .Add<IdempotentJob>(new RetryPolicyOptions
        {
            MaxRetries = 20,
            BaseDelay = TimeSpan.FromSeconds(5)
        });
});

The backoff uses BaseDelay, capped at MaxDelay. Fixed keeps the same delay, Linear multiplies the base delay by the retry number, and Exponential doubles it for each retry. UseJitter adds random variation to avoid synchronized retries.

Execution deadlines ​

Jobs use the configured per-type timeout or DefaultTimeout. The execution token is cancelled when the deadline expires, but jobs must observe that token to stop their work. GrydJobs awaits the job task before disposing its scope or allowing a retry. A job that ignores cancellation can delay the retry, but it is not abandoned into background work. A timeout consumes retry budget and can end in the DLQ. Provider shutdown cancellation is distinct: it does not consume retry budget or create a DLQ entry.

csharp
options.DefaultTimeout = TimeSpan.FromMinutes(5);
options.Timeouts[typeof(LongRunningJob)] = TimeSpan.FromMinutes(30);

Dead letter queue ​

After the captured retry limit is exhausted, GrydJobs records the terminal failure and a DLQ entry in the same Jobs database transaction. The entry retains the source provider ID, job type, serialized arguments, argument type, error, attempt count, reason, tenant, correlation, and invocation mode where available.

List entries:

http
GET /api/v1/dead-letter-jobs?page=1&pageSize=20

Retry one entry:

http
POST /api/v1/dead-letter-jobs/{id}/retry

Purge entries, optionally older than a retention period:

http
POST /api/v1/dead-letter-jobs/purge
Content-Type: application/json

{ "retentionDays": 30 }

These examples use API version 1. The controller route is versioned as /api/v{version:apiVersion}/dead-letter-jobs.

The replay is submitted using the original invocation mode and trusted tenant/correlation metadata. An entry is marked retried only after the provider accepts the new submission. If the submission fails, it remains available for another replay.

Automatic cleanup uses DeadLetterRetentionDays:

csharp
options.DeadLetterRetentionDays = 30; // Default: 30 days
options.DeadLetterRetentionDays = null; // Disable automatic cleanup

Delivery guarantees ​

The Jobs database and Hangfire storage commit separately. GrydJobs writes lifecycle tracking before Hangfire publishes an initial runnable state and fails the submission if tracking cannot be saved. If a Jobs definition is missing while the Hangfire job and its envelope remain available, GrydJobs can reconstruct the trusted metadata from that provider job. Reconciliation marks aged tracking-only submissions with no published provider state as failed; it does not recreate provider jobs. Worker crashes and retries still provide at-least-once execution, not exactly-once effects.

Legacy Hangfire calls using old adapter signatures can continue to run when trusted metadata is available in their stored definition or recurring schedule. If required tenant metadata cannot be recovered, execution fails before user work starts and the job should be resubmitted through the GrydJobs scheduler. A job type explicitly configured in GlobalJobs may use global scope. GrydJobs never derives trusted identity from job arguments.

Released under the MIT License.