Skip to main content

Queue maintenance

Nexus runs queue maintenance independently from the scheduled-job loop. The job loop therefore does not query every queue on every iteration, and maintenance work cannot delay the scan that starts jobs.

The maintenance worker has two paths:

  • Readiness promotes scheduled and retrying messages whose ProcessAtUtc has elapsed, and recovers expired leases. Nexus tracks retry and lease deadlines created by the current process and wakes at the nearest one. A periodic fallback scan handles messages created by another application instance, direct database changes, and deadlines restored after a restart.
  • Cleanup removes expired processed messages, history, and statistics on a separate cadence. Deletes are split into bounded batches to avoid long transactions and large lock spikes.

The defaults suit most applications. Applications with many queues can reduce idle database work by increasing the fallback interval. Decreasing it reduces the worst-case delay for work created by another application instance.

services.AddNexus()
.AddQueues(options =>
{
options.MaintenanceFallbackInterval = TimeSpan.FromSeconds(30);
options.CleanupInterval = TimeSpan.FromMinutes(5);
options.CleanupBatchSize = 2_000;
options.MaxCleanupBatchesPerRun = 5;
});

CleanupBatchSize * MaxCleanupBatchesPerRun is the maximum number of rows that one cleanup pass deletes from each retention store for a queue. If more expired rows remain, later cleanup passes continue the work.

IQueueItemUpdater.OrganizeMessagesAsync() remains available for applications that invoke maintenance explicitly. It now uses the same split readiness and bounded-cleanup implementation; passing force: true bypasses its compatibility throttle.