↑↓ to navigate Enter to open "…" all these words ANDOR to combine

Core patterns · No. 51

Four Ways to Do Work Later: Channels, Cron, the Outbox and Durable Internal Commands

A handler that wants something to happen after its transaction commits has four mechanisms to choose from, and picking the wrong one loses the work or pays for machinery it does not need. Here is the durable one: an ordinary CQRS command written to a table inside your transaction, claimed by a leased processor, and run later through the same decorator pipeline it would have run through inline.


The order just failed its payment. You have the aggregate in front of you, the transaction is still open, and the customer needs an email.

Reach for the obvious thing and you write _ = Task.Run(() => SendEmailAsync(orderId)). It works on your machine every time. It also sends before the transaction commits, so a rollback emails a customer about an order that does not exist; it dies with the process, so a deploy in the wrong second loses it with no record; and it is invisible to operations, so nobody finds out either way.

Reach for the second obvious thing and you put a message on a queue. Better, until you notice a message cannot roll back with the transaction that justified it, which is the dual-write problem the outbox was adopted to solve, arriving from a new direction.

So the question is not "how do I get this off the request thread." That part is easy and there are four ways to do it. The question is which of those four this particular piece of work belongs to, and the answer is decided by one property: what it costs when the work is lost. That is the decision recorded in ADR-114, with its counterweight in ADR-121.

Why it matters

The rubric asks for this twice, from two directions.

§6 (CQRS and Event-Driven Design) wants reads and writes separated through a dispatcher with a real decorator pipeline, an outbox for atomic persist-then-publish rather than "save then publish and hope", and idempotent consumers, with "consumers that aren't idempotent and break on redelivery" listed as a red flag (ArchitectureEvaluationCriteria.md:229-249). §10 (Messaging and Integration Architecture) is a weight-3 category (:349) scoring what happens once work leaves the request. It wants delivery semantics stated per consumer, dead-letter and poison-message handling with "retries with backoff and a bounded attempt count, then a dead-letter path with an operational procedure", bounded and documented retention, and long-running work with persisted state and defined timeouts (:329-341). Its red flags name "retries without idempotency (duplicate side effects) or without backoff (retry storms)" and "no dead-letter story: a poison message blocks the queue or is silently dropped" (:342-347).

Read those two lists and notice what they describe: not an event bus, a job queue. Backoff, an attempt ceiling, a dead-letter path, an operator procedure, retention. Every single one of those properties has to exist somewhere, and the choice is only whether you build them once or five times, once per feature that needed to do something later.

MMCA.Common scores §6 at Maturity 4 / Implementation 9 and §10 at Maturity 4 / Implementation 8 (common-ArchitectureScorecard.md:71 and :75). Both rows are earned by machinery that was already there for events. ADR-114 is the record of pointing that same machinery at instructions.

Four mechanisms, one question

The framework has four ways to do work later, and they are not interchangeable.

The bounded channel (ADR-121) is a Channel<T> singleton with a SingleReader BackgroundService drain, one per job kind. The live-channel queue's full mode is DropOldest, so under pressure it evicts the oldest item and TryWrite returns true until the queue is completed at host shutdown (after which a write is refused just as silently), which means the producer cannot learn that anything was discarded (ADR-121:46-65). The full mode is a per-job choice rather than a rule: a job whose requests coalesce can use a capacity-one channel with DropWrite, as ADC's bookmark cache-eviction signal does (:66-69, :154-157). Either way it is in-process only: it does not survive a restart and does not span replicas, and the live drain's bounded 5-second shutdown flush narrows the loss at a graceful stop without making the queue durable (:83-98, :178-185). That is not a defect, it is the specification. Article 22 in this series covers it in full, along with the live-channel case it exists for.

The recurring scheduler (ADR-074) carries cron occurrences: one row per registered job, and a missed occurrence is deliberately not replayed (ADR-114:45-53). It answers "sweep the audit trail nightly". Article 22 covers this one too, as the sibling contract for work a clock owns.

The transactional outbox (ADR-003) carries events. A row is written by the domain-event interceptor inside the same transaction as the aggregate change, and a processor drains it with a claim lease, exponential backoff, dead-lettering and OpenTelemetry instrumentation (ADR-114:33-43). It answers "this happened, tell whoever cares". It does not answer "do this later", and bending it to do so means expressing an instruction as an event, which is the modelling mistake ADR-007 and ADR-008 spend their length avoiding.

The internal command queue (ADR-114) is the fourth, and it exists because the first three leave a specific gap: a single unit of work, bound to a single aggregate, that must not be lost, and must not fire until the aggregate change actually commits.

ADR-121 states the deciding question in one line: "would losing this item be noticed as anything more than a missed UI refresh?" If yes, it is an internal command; if no, it is a channel item (ADR-121:173-175). The record is blunt about the symmetry of the mistake: putting must-run work on a DropOldest channel loses it silently, and putting a broadcast through a durable command pays a row, a poll interval and a lease for something worth less than the write (:196-198).

The MMCA answer: a deferred execution IS the inline execution

Here is the decision that makes the rest of it cheap.

IInternalCommand is a marker interface deriving from ICommand<Result> and nothing else (IInternalCommand.cs:38). A command that opts in keeps its ordinary ICommandHandler<TCommand, Result>. There is deliberately no second handler contract, no IJob, no ExecuteAsync(JobContext), because the whole point is that running later is the only difference (IInternalCommand.cs:11-16).

InternalCommandDispatcher resolves the closed ICommandHandler<TCommand, Result> from the execution scope (InternalCommandDispatcher.cs:75), so what comes back from the container is already wrapped by Scrutor in the whole ADR-014 command chain: the view-as refusal, registered as the outermost command decorator (ViewAsCommandDecorator.cs:14, ADR-137), then the feature gate, the authorization check, logging, cache invalidation, validation, the timeout budget and the transaction, in that order, unchanged (ADR-114:70-79).

That one decision is what the rest of this article is really about. It means a deferred command gets validated. It means a deferred command can require a permission. It means the logging decorator writes the same line it always wrote. None of that is job-queue code, because none of it is job-queue code anywhere else either.

Compare that to what a general-purpose job library executes: a serialized method invocation, which passes through none of your pipeline until you teach it to (ADR-114:247-254).

Scheduling rides the caller's unit of work

IInternalCommandScheduler exposes ScheduleAsync(command, runAt) and a TimeSpan delay overload, both returning Result<Guid> (IInternalCommandScheduler.cs:45-62). runAt is "the earliest instant the command may run, converted to UTC", with null meaning as soon as possible, and the contract is explicit that the processor never runs a row before that instant and makes no promise about how long after it a busy queue takes to get there (:23-27).

The write goes to the context handed back by the scope's own IDbContextFactory, which is the same instance the calling handler's repositories are using. From there the behavior splits, and the split is the whole reason this mechanism exists (ADR-114:81-111):

  • With a transaction active, the row is only enrolled. The caller's next save writes it, or the transactional pipeline saves it just before the commit when no save follows, so the caller's commit persists it and the caller's rollback erases it (IInternalCommandScheduler.cs:34-43). The processor wake is owed rather than raised: the scheduler records it against the context, and the unit of work releases it once after the commit succeeds and drops it on rollback (InternalCommandScheduler.cs:107-117, EnrolledCommandWake.cs:7-13).
  • With no transaction active, the row is saved immediately, which flushes anything else pending on that context exactly as calling the unit of work's own save would (IInternalCommandScheduler.cs:39-41). The processor is then signalled whether or not the row is due: a due row runs on the wake, and a future-dated row re-arms the processor's smart wait on its scheduled instant (InternalCommandScheduler.cs:125-128).

That first bullet is the outbox's atomicity guarantee applied to an instruction instead of an event: a transaction that aborts schedules nothing. It is also precisely what a separate job store cannot give you, and it is the common thread in ADR-114's rejection of Hangfire, Quartz.NET and a cloud queue. All three keep their state somewhere your transaction does not reach, so an enqueue beside a rollback is a job with no justification (ADR-114:243-268). The record adds a reason of its own to each: Hangfire's dashboard and IJobFilter pipeline duplicate the framework's operator surface and decorators in a second vocabulary, Quartz.NET would mean two schedulers with two configuration surfaces, and a cloud queue adds an infrastructure dependency and a cost line to every consumer (:250-252, :258-260, :266-268). The cloud-queue rejection is the interesting one, because it is conditional: a broker queue is the right answer once the work must cross a process boundary or survive the database being unavailable, and the standard way to enqueue it safely is to write an outbox row that a processor forwards, which is this design plus one hop (:262-266).

The row, and what it remembers

InternalCommandMessage (InternalCommandMessage.cs:21) is deliberately not an auditable entity, for the same reason OutboxMessage is not: it is framework bookkeeping, not domain state, so it carries no soft-delete flag, no audit stamps and no concurrency token. Its concurrency control is the explicit claim lease (:14-19).

The columns fall into four groups.

The instruction: CommandType (:46), which is the identity the payload deserializes against, and Payload (:49), the System.Text.Json output. The payload has to round-trip through JSON, which is a requirement rather than a style preference, and the guidance is to keep it to identifiers and scalars, never loaded aggregates (IInternalCommand.cs:19-24).

The schedule: ScheduledOn (:55), the earliest UTC instant the row may run, equal to CreatedOn (:58) for an immediate schedule.

The outcome: ProcessedOn (:64), Attempts (:67), LastError (:74, truncated to the column width) and DeadLetteredOn (:81), plus the lease pair ClaimedBy (:88) and ClaimedUntil (:96).

The context it was written under: CorrelationId (:102), TraceId (:105), SpanId (:108), TraceFlags (:117, the scheduling request's sampling decision, which the processor rebuilds into the parent context so a parent-based sampler exports the execution span only when the request was sampled), UserId (:124), UserRoles (:131), UserPermissions (:139, the scheduling user's permission claims as a JSON array) and TenantId (:145).

That fourth group is the one people forget to build, and it is what turns a deferred command from a system call into a continuation of a user's request. Before the dispatcher runs, the processor calls the shared AmbientOrigin.Restore (InternalCommandProcessor.cs:545), the same helper every other background hop uses, so every hop restores the same shape. It sets the tenant first, then rebuilds a ClaimsPrincipal carrying the sub claim, one role claim per stored role and one permission claim per captured permission (AmbientOrigin.cs:179-205, fed row.UserPermissions at InternalCommandProcessor.cs:549), and hands it to ScopedUserOverride, a scoped carrier read by ImpersonatingCurrentUserService, which decorates whatever ICurrentUserService the host registered (ADR-114:133-149). With no override set every member reads straight through, so an HTTP request behaves exactly as it would with no queue in the host. The identity is stamped with an authentication type of InternalCommand (InternalCommandProcessor.cs:67), which is what makes IsAuthenticated true and names the hop the identity came back from.

Restore the principal and an IRequiresPermission command becomes schedulable, because the authorization decorator has a real user to evaluate. That is a genuinely useful property and a genuinely uncomfortable one, and the trade-offs section below does not dodge it.

A rename that does not orphan the queue

The row stores the command's assembly-qualified CLR name by default, which means renaming the class, moving its namespace, or moving its assembly orphans every row already scheduled under the old name (InternalCommandNameAttribute.cs:7-10).

[InternalCommandName("Sales.SendOrderPaymentFailedEmail")] replaces that with a stable identity no refactoring changes. It mirrors EventNameAttribute on the outbox side, and the name has to be unique across the commands a host can resolve, because reverse lookup matches on it (:13-17).

Two details matter more than the attribute itself. First, it changes only what new rows store, so applying it to a command whose queue already holds pending rows is a two-step move: drain the queue first, then rename (:13-15). Second, the failure it prevents is loud rather than silent: a rename without the attribute leaves in-flight rows unresolvable, and the processor logs that and dead-letters them rather than dropping them quietly (IInternalCommand.cs:30-36). You lose the work either way. The difference is whether you find out.

The drain: claim, execute, stamp

InternalCommandProcessor is a BackgroundService (InternalCommandProcessor.cs:44). Its loop mechanics (the startup delay, the smart wait, the per-source drain and the retry backoff) are the internal PollingLoop the outbox processor also runs (PollingLoop.cs:12, called from InternalCommandProcessor.cs:86-100); the claim, the execution and the stamping are its own (ADR-114:270-279).

It waits a five-second startup delay before the first cycle (PollingLoop.cs:18, applied at :59), and returns immediately if the host owns no relational sources (:61-65). Each cycle fetches the due candidates per source, claims the due prefix of the batch with a lease token (ClaimDueAsync, InternalCommandProcessor.cs:333), renews that lease on each row just before running it, so the lease bounds one handler rather than the whole batch, and skips with a Warning any row another replica has taken over in the meantime (RenewClaimAsync at :215, LogLeaseLostBeforeExecution at :785-786, InternalCommandsSettings.cs:65-66). It executes each row in a fresh DI scope, and stamps the outcome through a set-based update guarded by that token (StampAsync, :675). That guard is what makes a scaled-out host safe: a replica whose lease expired mid-execution stamps nothing, so its stale outcome is discarded rather than overwriting whatever the new claimant did, and the discard is logged at Warning naming the command (:686-689, LogLeaseLost declared at :788-789).

A row completes only when its handler returns a successful Result (:468-474). A Result.Failure and a thrown exception are the same operational fact, and both consume an attempt (RecordFailedAttemptAsync, :476-479, :482-524). The backoff is RetryBackoffBaseSeconds * 2^(attempts-1) with jitter in [0.8, 1.2], capped at MaxRetryBackoffSeconds (ComputeRetryBackoffSeconds, :701-702, delegating to PollingLoop.cs:197, jitter at :206).

Three failures are terminal on the first attempt rather than consuming the budget: a payload that no longer deserializes against its type (payload_invalid, InternalCommandProcessor.cs:422-431), because retrying cannot change the stored bytes; an unresolvable command type (:440); and a missing handler registration (:462). The outbox retries an unresolvable type once, and the divergence is reasoned rather than accidental: an outbox row's type may live in an assembly that has simply not loaded yet, while a queue row can only ever run on a host that registers a handler for it, so a host that cannot name the type has no such handler and retrying would burn the budget waiting for a fact that will not change (ADR-114:151-169).

The settings mirror OutboxSettings where the semantics match (InternalCommandsSettings.cs): BatchSize 50 (:31), MaxAttempts 5 (:39), PollingIntervalSeconds 2 (:50), ProcessingDelaySeconds 0 (:58, the same default as the outbox's, OutboxSettings.cs:45), LeaseSeconds 300 (:69), RetryBackoffBaseSeconds 10 (:78), RetentionDays 7 (:94), DeadLetterRetentionDays 0, which falls back to RetentionDays (:102), and CleanupIntervalHours 6 (:109). One deliberately differs. MaxRetryBackoffSeconds is its own ceiling of 600 (:87) rather than reusing the lease, because a job queue wants a long lease for slow handlers and a short ceiling on how long a transient failure parks a command, where the outbox gets one number for both (ADR-114:171-182).

The worked example: an email that must survive a deploy

MMCA.Store's Sales module schedules a payment-failure notification. The command is fifteen lines and most of them are documentation:

// MMCA.Store.Sales.Application/Orders/InternalCommands/SendOrderPaymentFailedEmailInternalCommand.cs
// (XML doc comments trimmed; the attribute, the record and the marker are verbatim.)
[InternalCommandName("Sales.SendOrderPaymentFailedEmail")]
public sealed record SendOrderPaymentFailedEmailInternalCommand(OrderIdentifierType OrderId) : IInternalCommand;

One identifier as the payload, a stable name, and the marker. That is the entire contract.

The scheduling site is OrderPaymentFailedSagaHandler, the domain-event handler Article 49 covers as a compensating saga step. It opens its own scope, resolves IInternalCommandScheduler, and calls ScheduleAsync(new SendOrderPaymentFailedEmailInternalCommand(domainEvent.OrderId), runAt: null, ...) (OrderPaymentFailedSagaHandler.cs:32,35). When unpaid-order expiry is enabled, the same handler also re-arms the order's payment deadline, ExpireUnpaidOrderInternalCommand, through ProcessTimeoutScheduler.ArmAsync with a TimeSpan window (:74-78, :83-90), which is the delayed case: a command that should run at a specific future instant rather than as soon as possible.

That second call is the general shape of a process-manager timeout. An IProcessTimeoutCommand<TProcessId> derives from IInternalCommand (IProcessTimeoutCommand.cs:17), and ProcessTimeoutScheduler arms it with one ScheduleAsync call at the clock's now plus the delay, refusing a delay that is not strictly positive (ProcessTimeoutScheduler.cs:13, :29, :38), so a timeout rides this queue's scheduling, decorator pipeline and failure policy unchanged (ADR-114:231-241, ADR-143). How the order process uses that timeout as its compensation deadline is covered in Undo Is a Feature: Saga Compensation and the Reconciliation Backstop.

The handler on the other side is an ordinary command handler with no queue awareness at all:

// Condensed from SendOrderPaymentFailedEmailInternalCommandHandler.HandleAsync (logging trimmed).
public sealed partial class SendOrderPaymentFailedEmailInternalCommandHandler(
    IUnitOfWork unitOfWork,
    ICustomerService customerService,
    IEmailSender emailSender,
    ILogger<SendOrderPaymentFailedEmailInternalCommandHandler> logger)
    : ICommandHandler<SendOrderPaymentFailedEmailInternalCommand, Result>
{
    public async Task<Result> HandleAsync(
        SendOrderPaymentFailedEmailInternalCommand command,
        CancellationToken cancellationToken = default)
    {
        var orders = unitOfWork.GetReadRepository<Order, OrderIdentifierType>();
        var order = await orders.GetByIdAsync(command.OrderId, cancellationToken);

        // A missing order or recipient returns SUCCESS: redelivery would reach the same conclusion,
        // so retrying it would only burn the attempt budget on a fact that will not change.
        if (order is null)
            return Result.Success();

        // An order no longer in PaymentFailed also returns SUCCESS without sending: a resumed
        // checkout has already put the customer on a fresh payment page the email would contradict.
        if (order.Status != OrderStatus.PaymentFailed)
            return Result.Success();

        // Identity for a customer's order; the order's own stored contact for a guest's.
        var contactInfo = await OrderRecipient.ResolveAsync(order, customerService, cancellationToken);
        if (contactInfo is null)
            return Result.Success();

        var subject = $"Payment Failed - Order #{order.Id}";
        var body = "<h2>Payment Failed</h2>" /* ... */;

        // NOT wrapped: a transport fault fails the row, and the framework's backoff retries it.
        await emailSender.SendAsync(contactInfo.Email, subject, body, isHtml: true, cancellationToken);

        return Result.Success();
    }
}

Three judgements carry the whole failure policy, and all three are written down in the source rather than inferred. A missing order or an order with nobody to write to returns success with a log line, because redelivery would reach the same conclusion (SendOrderPaymentFailedEmailInternalCommandHandler.cs:18-19, :44-48, :59-63). The recipient comes from OrderRecipient.ResolveAsync (:56-57), which asks Identity only when the order has a CustomerId and otherwise uses the guest order's stored contact email and name, so "nobody" means a customer Identity no longer knows or a guest order with no email (OrderRecipient.cs:22-39). An order that has left PaymentFailed also returns success without sending, because the resume-after-expiry checkout path retires the expired session and opens a replacement in the same request, so a "Payment Failed" email would contradict the valid payment page the customer is already on (:21-25, :50-54). The SMTP call is not wrapped, so a transport fault fails the row and is retried by the framework (:18-19, :72). And the command's own documentation states the honest cost: delivery is at-least-once with no sent marker kept, so a host that dies after the SMTP call re-sends once the claim lease expires (SendOrderPaymentFailedEmailInternalCommand.cs:10-11).

The expensive case: permission, timeout and a cross-replica claim

MMCA.ADC's AI session scoring is the other end of the range, and it is the case ADR-121 moved out of the in-process channel. One pass issues one paid Anthropic call per session, so losing it is expensive and running it twice is worse.

The command declares four interfaces rather than one (ScoreEventSessionsInternalCommand.cs:36-37):

// MMCA.ADC.Conference.Application/Sessions/UseCases/DecisionSupport/ScoreEventSessions/
// ScoreEventSessionsInternalCommand.cs (doc comments trimmed).
[InternalCommandName("Conference.ScoreEventSessions.v1")]
public sealed record ScoreEventSessionsInternalCommand(EventIdentifierType EventId, DateTime? RequestedAtUtc = null)
    : IInternalCommand, IRequiresPermission, IHasTimeout, IFeatureGated
{
    // The kill switch over the paid pass: with the flag off, the FeatureGate decorator
    // short-circuits before the handler runs.
    public string FeatureName => ConferenceFeatures.SessionScoring;

    // Safe on a deferred command because the row carries the scheduling organizer: the processor
    // restores that principal, so the gate evaluates the operator who asked, not a system principal.
    public string Permission => ConferencePermissions.SessionSelectionManage;

    // MUST stay below the handler's 15-minute distributed-lock TTL: a budget outliving the lock
    // would let the claim expire mid-pass and a second replica start a concurrent, paid pass.
    public TimeSpan Timeout => TimeSpan.FromMinutes(12);
}

That is the payoff of resolving the closed handler through the container. IFeatureGated, IRequiresPermission and IHasTimeout are the ordinary markers every synchronous command uses, and they work here because the decorator chain is the same chain. A gate refusal on the processor is a Result failure like any other, so it consumes an attempt (InternalCommandProcessor.cs:468-479, :482-524).

The second positional member, RequestedAtUtc, is the request instant, and it makes a pass resumable: the pass skips every session whose score was written at or after that instant, so a duplicate queued behind a running pass completes without a paid call, and a retry after a timeout or a killed replica resumes where the attempt stopped (ScoreEventSessionsInternalCommand.cs:23-31).

The trigger endpoint carries the same gate (SessionSelectionController.cs:135), because it only schedules: the decorator fires minutes later on the processor, and without the attribute an organizer who switched the pass off would still be told 202 Accepted for work that is about to be refused (:118-126). Before it writes anything it checks that the event exists, and answers the same event-not-found 404 the dashboard does for an unknown or soft-deleted id, because the pass would find no sessions for that event and complete as a no-op, so a 202 would promise work that can never happen (:128-131, :143-147). It then schedules the command with the service clock's current instant as RequestedAtUtc (:149-153), returns the failure when the schedule write fails (:155-158), and answers 202 Accepted otherwise (:161). Its [NonIdempotent] attribute records why caching the 202 would be wrong: it would report acceptance for a request that never reached the queue, hiding a schedule failure the caller has to act on (:136).

Three pieces of handler discipline are worth copying (ScoreEventSessionsInternalCommandHandler.cs):

  • Lock and skip, not lock and wait. The handler takes a per-event IDistributedLock claim with a 15-minute time-to-live (:63) and a wait of TimeSpan.Zero (:70), and the loser logs and returns Result.Success() (:89-93). Queueing behind a pass already covering the same work would only pay for it twice.
  • Success means every pending session was scored. A Result failure from the pass is returned as a failure (:97-103), and so is a pass that scored some sessions but not others, as AiScoring.PartialFailure (:109-119), so the processor backs off and retries both exactly as it would a thrown exception, up to the attempt ceiling and the dead letter. The retry resumes rather than restarts: the RequestedAtUtc skip leaves the scored sessions alone, so only the failed ones are paid for again, at most MaxAttempts - 1 extra calls per persistently failing session, and the dead letter after the last attempt is the operator signal. An event with nothing to score is not a failure: the runner answers it with zero counts (:24-36, zero counts :35).
  • Idempotent as required. Re-running a pass rewrites the same per-session score rows rather than accumulating them, and the pass receives the command's RequestedAtUtc so it skips sessions already scored for this request (:37-43,95).

The command's own remarks state why the trigger is durable: it is a database row rather than in-process state, so a deploy, a crash or a killed replica between the trigger and the last session's score does not lose the pass, because the row stays claimable until it completes and the framework owns the claim lease, the retry backoff and the dead letter (ScoreEventSessionsInternalCommand.cs:14-19).

Operating it

A queue with no operator surface is a queue that gets hand-edited in production SQL. IInternalCommandAdministration (IInternalCommandAdministration.cs:17) mirrors IOutboxAdministration with four methods: CountPendingAsync (:28), ListDeadLettersAsync oldest first (:42), RequeueAsync (:63) and PurgeProcessedAsync (:80). Every method returns Result<T>, because an unknown source name or an unreachable database is an expected failure an operator screen renders, not an exception (:11-15).

Three of its decisions are worth stealing outright. A requeue puts attempts back to zero and clears the dead-letter stamp and the lease, but keeps LastError on purpose, because the reason a command failed is the first thing anyone asks after a requeue (:48-52). The pending count includes rows whose scheduled instant is still in the future, because they are backlog the host has accepted, not work it has finished (:19-23). And the dead-letter projection, InternalCommandDeadLetter (:99), deliberately omits the payload: it can carry personal data, and nothing an operator decides about a requeue depends on reading it (:86-89).

The telemetry lands on its own meter, MMCA.Common.InternalCommands (InternalCommandMetrics.cs:20). The shared queue reports three counters, two histograms and two observable gauges: internal_commands.processed.count (:38-39), .failed.count (:48-49), .dead_letter.count (:58-59), internal_commands.execution.duration (:67-68), .execution.lag (:76-77), internal_commands.pending.depth (:93-94) and internal_commands.oldest_due.age (:105-106). A dedicated lane adds its own internal_commands.lane.* counters and gauges on the same meter, tagged by lane (:129-164).

Lag and oldest-due-age are the two that answer the question an operator actually has. Depth tells you how much is waiting; lag and age tell you how late it already is.

Trade-offs, honestly

  • One migration per relational data source. The table is in the model of every relational source (SQL Server, PostgreSQL and SQLite after ADR-113), so adopting the queue means dotnet ef migrations add AddInternalCommands per source, applied before deploying (ADR-114:283-287). The mapping is deliberately not gated on InternalCommands:Enabled, unlike the cron scheduler's job table, because a row must be able to commit in the same transaction as the aggregate change, a transaction does not span databases, and a flag that changed the schema would make enabling the queue a migration rather than a deployment decision (:128-131).
  • Execution is at-least-once, and it is a new obligation on ordinary commands. A replica that dies after its handler committed and before the row was stamped releases the row when its lease expires, and the command runs again. That is the same contract the outbox already places on event handlers, so it is not new for anyone using the framework, but a command handler previously only ever ran once per request (ADR-114:289-293).
  • A scheduled command runs with the scheduling user's authority, not the executing host's. That is what makes an IRequiresPermission command schedulable at all, and it means the row is a durable record of an authorization decision. If the user's roles change between scheduling and execution, the stored roles win. The record files that as deliberate (the decision was taken when the work was requested) and as the reason the row is under the same retention discipline as the outbox, because the payload can carry personal data (ADR-114:295-301).
  • The polling interval is the fallback, and production stretches it. A schedule does not wait for the poll to be noticed: a row saved outright signals the processor, and a row enrolled in a transaction owes a wake that the unit of work releases once the commit succeeds (DbContextFactory.cs:508, :661). What the interval bounds is how long an idle processor sleeps without a wake, because the smart wait is capped at it (PollingLoop.cs:117, ADR-114:104-109). PollingIntervalSeconds ships at 2, the same default the outbox ships, so the divergence is a deployment decision rather than a framework one: ADC and Store both run the queue at 60 seconds while pushing the outbox to 300 (MMCA.ADC/infra/main.bicep:2385,2391, MMCA.Store/infra/main.bicep:2057,2064). A host that raises the interval accepts that much latency on any row whose wake never reaches an idle processor.
  • Two poll loops, not one. A host runs the outbox processor and the queue processor side by side, each with its own signal instance so a burst of schedules cannot consume the outbox's single pending wake-up. Both poll spans are suppressed from telemetry export by the same Aspire processor (ADR-114:317-324). Two loops is two idle query streams against every source, which is exactly the cost the 60-second production interval is paying down. A host that registers a dedicated lane (one exists, for outbound webhook deliveries, ADR-142) runs a third loop for that command type, which the shared fetch then excludes (InternalCommandProcessor.cs:315-320, ADR-114:190-229).
  • The duplication with the outbox is deliberate and bounded. Extending the outbox to carry commands was the tempting option and it is rejected on four counts: the two rows want different columns (a scheduled instant, an attempt budget, a captured principal), different indexes, different retention and different terminal semantics for an unresolvable type, and the outbox's poll predicate is the hottest query the framework issues, so widening it would make every outbox change a job-queue change (ADR-114:270-279). The two processors share the loop mechanics through PollingLoop and nothing else: each keeps its own tables, queries, logging, metrics and activities.
  • The payload is JSON, which is a real constraint on what a command can carry. Identifiers and scalars round-trip; a loaded aggregate does not, and should not be attempted (IInternalCommand.cs:19-24). Everything a handler needs beyond an identifier it re-reads at execution time, which is correct anyway, because the world moved on between the schedule and the run.
  • Choosing between this and a channel is a per-job judgement no test enforces. ADR-121's question ("would losing this item be noticed as anything more than a missed UI refresh?") is a rule a human applies, and getting it wrong in either direction is silent (ADR-121:173-175,196-198).

Apply this even without MMCA

The mechanism ports to anything with a relational database and a background worker. Six steps, in the order that matters:

  1. Write the job to a table inside the caller's transaction. This is the whole design and everything else is detail. If your job store is a different database, a different queue or a different service, an enqueue beside a rollback leaves you with a job nothing justifies. One table per database that schedules work, written through the same connection and unit of work the aggregate change uses.
  2. Make the deferred unit the same unit you already execute. If your application has a command or handler abstraction with a pipeline attached, defer that, and resolve the handler from the container at execution time so the pipeline applies. A separate job contract means validation, authorization, logging and transactions get re-implemented for the deferred path, in a second vocabulary, and drift from the first.
  3. Store the context, not just the payload. Tenant, user id, roles, correlation id. Restore them before the handler runs and a deferred job stops being an anonymous system call: your logs correlate, your tenant filters apply, and an authorization check has a real subject. Use one restore helper for every background hop so they cannot diverge.
  4. Give the type a stable name, separately from the CLR type. An attribute or a string constant. Then a rename is a rename, not an orphaning, and make the unresolvable case dead-letter loudly rather than disappear.
  5. Claim with a lease, stamp under the lease token. A SELECT of due rows plus an UPDATE that sets a claim token and an expiry, then a final update guarded by that same token. That guard is what lets a replica that stalled past its lease drop its stale outcome instead of overwriting the new claimant's. Then make handlers idempotent, because a lease bounds concurrency and does not bound re-execution.
  6. Build the operator surface on day one. Count pending, list dead letters, requeue, purge. Keep the error text on a requeue. Meter depth, and also lag, because depth tells you how much is waiting and lag tells you how late it already is. A dead-letter path with no way back into execution is production SQL with extra steps.

The rule of thumb: decide what losing the work costs before you decide where to put it. Ephemeral work on a durable queue is waste you can measure; must-run work on an ephemeral queue is a loss you cannot.


What we covered: why a handler that wants a side effect after its commit has four mechanisms and not one, how ADR-121's question ("would losing this be more than a missed UI refresh?") splits the ephemeral channel from the durable queue, why an internal command is an ordinary ICommand<Result> resolved as a closed handler so the whole ADR-014 decorator chain applies to a deferred run, how ScheduleAsync rides the caller's unit of work so a rollback schedules nothing (and why that property is the common thread in ruling out Hangfire, Quartz.NET and a cloud queue), what InternalCommandMessage remembers about the request that scheduled it and how AmbientOrigin.Restore puts the tenant and the principal back, why [InternalCommandName] makes a rename survivable and its absence loud, how the leased claim-execute-stamp cycle and its jittered backoff bound failure, and what the administration surface and the MMCA.Common.InternalCommands meter have to expose before a dead-letter path is an operational procedure rather than a table.

MMCA.Common is open source. Star the repo, read the 2-minute ADR-114 behind this pattern, or dotnet add package MMCA.Common.API and build the monolith you can extract later.

Previous: Article 50, "The LLM is a dependency: a bounded, guarded, metered boundary for chat completions." Next in the series: Article 52, "Finishing identity: second factor, email confirmation and stored permission grants."

Tags: .NET, C Sharp, Distributed Systems, Microservices, Software Architecture