Batched Completions (Dispatcher Mode)
In dispatcher mode (UseDispatcher = true, the default since 8.0), each worker buffers job completions in memory and flushes them as a single multi-row transaction. This collapses N per-job commits into one, cutting database round-trips on high-throughput workloads.
Setup
Batched completions are automatic in dispatcher mode. Tune the batch size and flush interval if the defaults don't fit your workload:
builder.Services.AddWarpServer<AppDbContext>(config =>
{
config.CompletionBatchSize = 50; // default: 50
config.CompletionFlushInterval = TimeSpan.FromMilliseconds(100); // default: 100ms
});
Set CompletionBatchSize = 1 to start a write as soon as anything is buffered. To turn batching off entirely, set UseDispatcher = false (single-worker mode: each worker writes its own completion).
How It Works
Each worker group has one completion buffer and one flusher that writes it. When a handler finishes:
- The worker builds a
PendingCompletioncontaining the mutatedJobrow (with its new terminal state), the counters to add, and any JobLog entries, adds it to the group's buffer, and goes straight back to its channel. Workers never write completions themselves. - The flusher writes the buffer in one set-based statement when it is full (
CompletionBatchSize), when its oldest entry has waitedCompletionFlushInterval, or the moment the group has nothing left claimed, so a lone job never waits for the timer. - While a write is in flight the next buffer fills behind it. Backpressure: once the group holds four buffers' worth of unwritten completions (4 ×
CompletionBatchSize, and never fewer than 200), a worker that finishes a job waits for the write in flight to end before taking its next one. It keeps its slot while it waits, so the group claims nothing new. Under normal load the buffer never gets that far. With a slow or hung database the group slows down instead of piling up finished jobs that a crash would run again. A stopping worker stops waiting, and its completion is written by the final flush. - On graceful shutdown, once every worker has stopped, the flusher writes what is left on a budget of its own, so SIGTERM doesn't strand the in-flight batch.
FlushAsync always commits to completion — there is no caller-visible way to cancel a drained batch. The DB operations inside use CancellationToken.None so a shutdown mid-flush still persists. The transactional commit + split-on-poison recursion means a single bad row in a batch of 50 isolates and drops only the poison entry; the other 49 good rows still commit.
Poison Entry Isolation
If a batch commit fails with DbUpdateException (e.g. one of the 50 rows was deleted by another server between processing and flush), FlushRangeAsync recursively splits the batch in half and retries each half. Single-entry failures are logged and dropped — StaleJobRecovery will later observe the orphaned Processing row and decide whether to requeue or fail it based on the CanBeRestarted metadata.
Non-DbUpdateException failures (connection drops, timeouts) propagate up and the drained entries are lost for this flush cycle. StaleJobRecovery handles those too.
Trade-offs
Batched completions are a throughput optimization, not a latency optimization. Consequences:
- At-least-once semantics — if the process crashes between handler completion and the batch flush (at most 4 ×
CompletionBatchSizejobs per group and never fewer than 200, plus the write in flight), the job is observed as stillProcessingby the stale-recovery task and (depending onCanBeRestarted) requeued. For non-idempotent handlers, pair with[NoRestart]or a handler-internal idempotency check. - Delayed visibility — a job's terminal state doesn't appear in the dashboard until the batch commits (up to
CompletionFlushInterval). For 100ms flush intervals this is imperceptible; if you raise it significantly, operators see stale "Processing" rows. - Batch size vs. lock contention — larger batches amortize round-trips but hold row-level locks on more rows per transaction. 50 is a sensible default for PostgreSQL and SQL Server; tune down if you see lock-wait metrics climbing.
When To Turn It Off
Dispatcher mode is the default because it cuts statements, allocations and connections per job several times over. Set UseDispatcher = false (single-worker mode, one commit per job) when:
- A finished job must read as finished immediately — something polls a job's state and cannot tolerate up to
CompletionFlushIntervalofProcessing. - Handlers are not idempotent and cannot be marked
[NoRestart]— single-worker mode narrows the window in which a crash re-runs a finished job to the one job each worker holds.
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
UseDispatcher | bool | true | Dispatcher mode + batched completions; false selects single-worker mode |
CompletionBatchSize | int | 50 | Max completions buffered before an automatic flush |
CompletionFlushInterval | TimeSpan | 100ms | Max age of the oldest buffered completion before flush |