Guarantees
At least once
Section titled “At least once”A job’s message stays in RabbitMQ until a worker has run the job and acknowledged it. If the worker’s app dies, loses its connection or is stopped first, the broker delivers the message again, to that app or another. So every published job runs at least once, and sometimes more than once:
- The app dies right after a job finished. Finished jobs are acknowledged in batches, at most about 100 ms later. A crash in between runs them again.
- A job ignores its cancellation token during a shutdown. The app stops without it; its message is redelivered when the connection closes, while the old run may still be going.
- A periodic job’s consumer fails over. The next app to take over the periodic queue may run the current run again.
- A publish is retried. If the connection breaks while RabbitMQ confirms a publish, the publisher can’t tell whether the message arrived, and publishing again can make a copy.
Write jobs so that running one twice does no harm (idempotent): check whether the work is already done before doing it, use unique keys, make external calls with idempotency keys where the other side supports them.
public override async Task Perform(int orderId, CancellationToken cancellationToken){ var order = await db.Orders.FindAsync([orderId], cancellationToken); if (order is null || order.InvoiceSentAt != null) return; // already done
await mailer.SendInvoiceAsync(order, cancellationToken);
order.InvoiceSentAt = DateTime.UtcNow; await db.SaveChangesAsync(cancellationToken);}Even then there’s a window: the email can go out and the save fail. Jobs with external side effects are at least once; only work done entirely in one database transaction can be exactly once.
Nothing silently dropped
Section titled “Nothing silently dropped”A job leaves the queues only by finishing, or by landing in the failed queue:
- it failed and ran out of retries (25 by default);
- its message couldn’t be read (a corrupt body, arguments that don’t deserialize);
- no app registered its tag for 30 minutes (
UnknownJobTimeout); - RabbitMQ delivered it 20 times without an acknowledgement (a job that crashes the process).
Nothing consumes the failed queue: IFailedJobs shows what’s there, and
requeues or purges it.
The exceptions are periodic runs, which are dropped on purpose when no app defines their job any more (see
Periodic jobs), and a full queue: publishing to a queue at its MaxQueueLength (50,000 by default)
fails, so the publisher knows.
Not transactional with your data
Section titled “Not transactional with your data”MassiveJobs publishes to RabbitMQ, which doesn’t take part in your database transaction. If you publish a job and then roll back, the job still runs; if you commit and the publish fails, there’s no job. Two patterns cover most cases:
- Publish last. Do the work, publish, then commit. A failed publish throws before the commit, so the transaction rolls back. A crash between the publish and the commit leaves a job without its data: the job should check that its data exists.
- Publish after the commit. If losing the job is worse than a job without data, commit first and publish after, with a sweep that publishes whatever was missed.
If a job must exist exactly when your data does, you need it in the same transaction: an outbox table, or a job library that stores jobs in your database, like XactJobs.
There’s no order between jobs. They’re spread over several queues, run in parallel by several apps, and a failed job comes back later. A job that must run after another should be published by it, at its end.