Skip to content

Move slow work into a queue

10 min reading

Separate interactive acceptance from background completion; derive backlog growth

Page details

System Design · Move work through time

10 min reading · 23 min with practice

Sources

Published · Last edited:

Prerequisite: Find eligible results through an index. For basics, use the independent System Design foundations.

More
20px

Hypothetical scenario. A seller edits a Northwind product while image preparation slows. Dev’s synchronous path keeps the seller waiting for work beyond storing the edit. Maya agrees that some work can happen later, then asks what the success message will mean.

Dev: “A queue could let us return earlier.”

Priya: “Earlier than which completed obligation: storing the edit, accepting processing, or publishing the projection?”

Choose an acceptance boundary before drawing the background worker. Carry forward the search projection and image-readiness states, then derive backlog and recovery margin for their selected work. A shorter wait must not silently turn accepted work into completed work.

What does the seller’s acknowledgement promise?

A synchronous operation waits for its required downstream work before completing. An asynchronous operation can acknowledge an earlier durable state and perform later work separately. That is a product-contract choice. Returning success must identify whether the edit is stored, whether processing is accepted, or whether the new searchable representation is already ready.

For this hypothetical design, the authoritative product edit can complete before its search projection changes. The application exposes that indexing is pending rather than claiming immediate search visibility. An image becomes public only after the verification and publication path from Page 17 completes. Queue acceptance does not weaken those readiness rules.

A queue holds work awaiting consumption. A worker obtains an item, performs the selected operation and records progress under a delivery contract. That arrangement can absorb a temporary difference between arrival and processing rates. It does not create unlimited processing capacity or make an unbounded delay acceptable.

Keep the intent connected to the edit

There is a gap if the application stores a product change and then independently tries to publish a job. It can crash after the first operation and before the second. Reversing the order creates a different gap: work may be published for an edit that never becomes authoritative.

One design records the product change and a pending downstream-work intent in one local database transaction. A dispatcher later publishes that intent and tracks publication progress. This pattern is commonly called an outbox. Here, a local transaction means the selected records commit together in one database boundary; the later transaction lesson examines the enforcement details.

The dispatcher still can publish twice after an uncertain response. An outbox connects an authoritative edit to durable intent; it does not establish that every downstream effect occurs only once. The next lesson develops acknowledgement and recovery at precisely that boundary.

Accept an edit before completing a derived projectionA local product edit and pending intent commit together. A dispatcher publishes the intent to a queue. A worker updates the versioned search projection and records completion. Acceptance precedes projection readiness.Edit + intentLocal commitDispatcherQueueWorkerProjectionAccepted does not mean searchable yet.Every transition needs observable progress.
Move the waiting boundary while preserving a recoverable obligation.

Name the rows on the outbox path

Hypothetical scenario. A product edit creates revision 12 and an outbox row E12 in the same authoritative database transaction. E12 contains the product identity, source revision, event type and stable event identity. The API acknowledgement promises the committed edit and its durable publication obligation, not immediate search completion.

Name the rows on the outbox path — selected design evidence
RecordExample fieldsAtomic owner
Product rowQ7, source revision 12Source transaction
Outbox E12event_id, Q7, revision 12, pendingSame source transaction
Search targetQ7, applied revisionTarget conditional update
The dispatch progress write can be lost after step three; a stable event identity survives the retry.The dispatch progress write can be lost after step three; a stable event identity survives the retry. Source DB to Dispatcher: 1 · committed outbox E12; Dispatcher to Broker: 2 · publish stable E12; Broker to Dispatcher: 3 · publisher confirm; Broker to Search worker: 4 · deliver E12.Source DBDispatcherBrokerSearch worker1 · committed outbox E122 · publish stable E123 · publisher confirm4 · deliver E12
The dispatch progress write can be lost after step three; a stable event identity survives the retry. Enlarge the diagram to inspect its labels; the surrounding records and explanation give the text alternative.

A dispatcher sends E12 to a broker and marks dispatch progress. If it crashes after broker acceptance but before recording progress, it can send E12 again. Publisher confirmation closes one broker boundary; it does not atomically update the outbox database. The target therefore needs a version or event-identity rule that protects its own effect.

CDC is an alternative way to extract committed changes, not permission to ignore transaction boundaries or schema meaning. A consumer needs a stable interpretation of the extracted record and a retained position for recovery. Track oldest unfinished obligation alongside depth: one old blocked product can violate freshness while many newer jobs complete.

Work through a maintenance burst

Hypothetical worked example. Keep the selected normal maintenance rate of two product changes per second. Choose one search-update job per change and a worker pool completing three qualifying jobs per second. Normal drain margin is 3 − 2 = 1 job per second when a backlog exists.

For a separate selected maintenance burst, let changes arrive at eight per second for twenty seconds. Arrivals during the burst are 8 × 20 = 160 jobs. The workers complete 3 × 20 = 60 under the fixed-rate model. Backlog increases by 160 − 60 = 100 jobs, agreeing with (8 − 3) × 20 = 100.

After arrivals return to two per second, the pool clears that backlog at one net job per second. Time to empty is 100 ÷ 1 = 100 seconds under the selected assumptions. Dividing by three would ignore continuing arrivals; increasing queue storage would not change the one-job-per-second recovery margin.

Do not confuse time to empty with the waiting time of every job. Under a simplified fluid FIFO model, one new job arriving behind one hundred jobs waits approximately 100 ÷ 3 = 33⅓ seconds before its turn, ignoring its own processing time. Continuing arrivals join behind it. Different scheduling and job durations change that estimate.

Measure age as well as depth

A queue containing one expensive blocked job can violate a freshness objective even when its depth is small. A larger queue of fast independent work may clear quickly. Record the age of required unfinished work and its completion rate, alongside counts and worker occupancy.

Northwind’s sixty-second price-evidence rule does not automatically become a sixty-second queue-retention setting. The search result still needs the eligibility and price-selection rules from Page 18. An old pending index update can create stale discovery even when a final detail lookup shows a current price.

Likewise, expiry must follow the work’s meaning. An obsolete projection version may be superseded by a newer complete version under an explicit rule. A required publication or audit obligation cannot be silently discarded because a timer elapsed. Define what may be coalesced, skipped or reconciled.

Separate work that has different resource needs

Search updates and image processing may require different CPU, memory and external dependencies. One shared unconstrained worker pool can let large image jobs delay small required index updates. Separate admission or worker budgets can prevent one work class from consuming every execution slot.

That separation also introduces a capacity decision: idle image capacity may not immediately help search, and vice versa. Use a concrete fairness and borrowing policy where sharing is useful. Do not label a system isolated merely because its jobs have different names in the same overloaded process.

In the wild: a queue has documented operations

RabbitMQ’s queue documentation describes a concrete broker abstraction. Read the selected queue type, persistence and consumption behavior before relying on it. The broker’s documented behavior is one part of Northwind’s end-to-end workflow, not proof that the product edit and search update remain connected.

Hypothetical failure scenario. Northwind returns success after recording a product edit, then crashes before publishing the only search-update message. The broker remains healthy and empty while search stays old. A queue-depth alert cannot detect an obligation that was never durably represented in that queue.

The outbox intent makes that gap observable and retryable. Monitor unpublished intents as well as broker backlog and completed projections. The useful question is which accepted work remains unfinished, not whether one transport component currently reports a green state.

Use asynchrony with an honest response

Introduce a queue when work can complete after an explicitly acknowledged acceptance point and buffering helps a declared demand shape. Keep an operation synchronous where the caller must know the final effect before proceeding and the dependency is part of that promise. Neither arrangement removes failure; it changes where the service exposes it.

Northwind now has durable edit intent, a bounded processing path and a recovery calculation. The next question follows a worker crash: when should an item be acknowledged, and how can a new attempt distinguish unfinished work from an effect already applied?

Challenge the proposed mechanism

The product edit committed but the broker was unavailable. Which row proves that work remains?

Reveal the design reasoning

The outbox obligation does. Recovery scans or tails committed undispatched work and republishes it. An in-memory task list would lose that obligation on restart. Keep dispatch confirmation distinct from target completion.

Decision and reconsideration: Commit edit and publication intent together. Select polling or CDC using recovery and ownership needs, with duplicate-safe targets in either design.

Inspect acceptance and completion

  1. The selected burst leaves one hundred jobs. Why does recovery take one hundred seconds rather than about thirty-three?

    Subtract continuing arrivals for emptying time

    Three completions per second minus two new jobs gives one net job per second. Emptying the queue differs from waiting behind one hundred existing FIFO jobs.

  2. The queue is empty, but an accepted edit is missing from search. Has the queue proved completion?

    Find the durable obligation

    No. Publication may never have occurred. Compare accepted edit intents, dispatcher progress and applied projection versions, not broker depth alone.

  3. May every old job be discarded to meet a queue-age target?

    Apply the operation’s meaning

    No. Superseding a projection version and abandoning a required effect are different decisions. Define expiry or coalescing under the application contract and retain unresolved obligations.

Once a worker owns the deferred obligation, failure can interrupt its effect or acknowledgement. Page 20 examines those two windows before deciding how redelivery should behave.

Northwind decision log

Acknowledge stored edit intent without claiming immediate projection readiness.
Measure arrival, completion, age and recovery margin for each work class.
Carry operation identity and crash recovery into the delivery contract.

Go deeper: events and derived state