Skip to content

Queues and workers

With the defaults, MassiveJobs uses these queues on RabbitMQ (all quorum queues, bound to one direct exchange, massivejobs.direct):

Queue For Consumed by
massivejobs.immediate.00 … .03 jobs, scheduled jobs and their retries a worker in every app that runs workers
massivejobs.long-running.00, .01 jobs published with a timeout of 10 s or more a worker in every app that runs workers
massivejobs.periodic the runs of periodic jobs one app at a time, among those that define periodic jobs
massivejobs.failed jobs that ran out of retries, unreadable and unknown messages nobody: see IFailedJobs

NamePrefix goes in front of every name (shop.massivejobs.immediate.00), so separate systems can share a broker or a virtual host.

A publisher sends each job to the next queue of its kind, in turn. The queues fill evenly, whichever app publishes.

That’s why apps that share jobs must use the same queue counts (ImmediateWorkersCount, LongRunningWorkersCount) and the same NamePrefix. An app with 4 immediate queues publishes to .00 … .03; an app configured with 2 consumes only .00 and .01, and the jobs in the other two wait for an app that consumes them. The defaults agree; if you change a count, change it everywhere.

Each app that runs workers (StartWorkers, true by default) has a worker per queue: 4 immediate and 2 long-running. On RabbitMQ several consumers of one queue take turns, so with three apps running, every queue has three consumers, and adding an app adds capacity to every queue.

A worker runs MaxDegreeOfParallelismPerWorker jobs at once: 1 by default. So an app runs, by default, up to 4 immediate and 2 long-running jobs at the same time.

  • More queues raise the throughput of short jobs, and spread a burst over more consumers. On the drain benchmark, 4 queues × 1 job emptied a backlog of no-op jobs at about 95,000 jobs/s; 2 × 2 at 72,000; 1 × 4 at 44,000.
  • More jobs per worker help jobs that mostly wait: on HTTP calls, the database, the disk. With MaxDegreeOfParallelismPerWorker = 8, an app runs up to 32 immediate jobs at once without new queues.

Changing the parallelism is local to an app; changing the queue counts is not (see above).

An immediate worker holds up to BatchSize messages (250 by default, so 1,000 per app with the defaults): received from the broker and not acknowledged yet. That keeps workers busy without asking for every job. Finished jobs are acknowledged together, half a batch at a time, or after 100 ms.

Messages a worker holds are invisible to other apps until it acknowledges or returns them. If an app dies, RabbitMQ gives them to another one; the jobs it had finished but not yet acknowledged run again. A smaller batch means fewer jobs held by one app, at some cost in throughput.

Long-running workers hold twice their parallelism, so long jobs don’t wait behind each other in one app while another is idle.

Each job queue holds at most MaxQueueLength messages (50,000 by default; QueueLength.NoLimit for no limit). Publishing to a full queue fails: the publisher throws. Scheduled jobs and retries count, since they wait in the queues.

Set StartWorkers = false in an app that publishes jobs but shouldn’t run them. It needs the same queue counts and prefix as the apps that run the jobs, and the same jobs registered.

builder.Services.AddMassiveJobs(o => o.StartWorkers = false)
.WithJobsFromAssemblyOf<SendWelcomeEmail>()
.UseRabbitMq(o => o.ConnectionString = builder.Configuration.GetConnectionString("RabbitMq"));