Asynchronous Communication: From Outbox to Consumer

Choose when to use asynchronous messaging and follow an order event through an outbox, delivery, idempotency, retries, dead-letter handling, and monitoring.

published: reading time: 7 min read author: Geek Workbench updated: November 5, 2026
Quick Summary

Async messaging moves work out of a request, but someone still owns delivery and recovery. Follow an order event through an outbox, relay, idempotent consumer, retry policy, dead-letter queue, and monitoring. A short chooser helps decide when to use async work, queues, pub/sub, or an orchestrator.

Asynchronous Communication: From Outbox to Consumer

Asynchronous messaging lets a service hand off work without keeping the original request open. A durable broker can hold that work while a consumer is down, and the request may return sooner if it does not need the downstream result. The handoff adds failure points: a message may be delayed, delivered twice, or remain unprocessed while a queue grows.

This guide is for teams deciding whether to move work out of a request and how to make that handoff safe. The example follows one order from database commit through downstream processing.

Choose the pattern before the broker

First ask whether the caller needs the downstream result before it can respond.

Need Pattern to consider
The caller needs an immediate decision, such as whether inventory is available Keep that operation synchronous, or define a separate pending state if the business can accept delayed completion
A database change and its event must not get out of sync Write both in one transaction with a transactional outbox
One worker should handle each job from a pool Use a work queue with competing consumers
Several independent services need the same fact Publish an event to a topic or other pub/sub channel
A workflow has branches, timeouts, or compensating actions Use an orchestrator to own the workflow state; keep simple independent reactions as event consumers

A queue or topic moves the failure boundary. Before moving a step after the response, decide what the caller sees if the order is saved but fulfillment is pending, and how ordering works when events arrive late. The API guide to partial failures, ordering, and eventual consistency covers those API decisions.

For the example below, PlaceOrder is a command sent to the order service. After the order is committed, OrderPlaced is a fact that downstream services can consume. Keeping that distinction clear helps prevent a consumer from treating a request as if it had already succeeded.

Follow one order through the system

1. Commit the order and its event together

The order service must save the order and the fact that needs publishing. Writing the database first and publishing second creates a gap: the process can fail after the commit and before the publish. Publishing first has the reverse problem; consumers can see an event for an order that never committed.

Use a local database transaction for the business row and an outbox row:

BEGIN TRANSACTION
  write Order(status = "accepted")
  write Outbox(event_id, type = "OrderPlaced", order_id, version, payload)
COMMIT

This is pseudocode, not a database-specific schema. Keep a stable event_id, an order_id, an event type and schema version, and only the payload fields consumers need. Avoid putting secrets or unnecessary personal data in the event.

The API can return once the order is committed, but its response should match the product contract. If fulfillment has not run yet, describe that state as pending rather than implying that every downstream action is complete.

2. Relay the outbox row to the broker

A relay reads committed outbox rows and publishes them. It can poll a table or read a database change stream. The relay should mark a row published only after it has evidence that the broker accepted the message.

There is still a duplicate window. The broker might accept the message, then the relay can crash before updating the outbox row. On restart it publishes that row again. The safe default is at-least-once delivery: keep retrying until delivery succeeds and make consumers tolerate duplicates.

If order matters within an order, include a sequence number and use the order ID as a routing or partition key when the chosen transport supports that model. Decide what happens when one event fails: pause later events for that order, or let them proceed and require consumers to detect gaps. Do not assume a broker gives global ordering across all messages.

3. Make the consumer idempotent

The inventory consumer may receive the same OrderPlaced event more than once. Give each event a stable ID and store that ID with the consumer’s own state change in one local transaction:

BEGIN TRANSACTION
  if event_id is new:
    reserve inventory
    record event_id as processed
COMMIT
acknowledge the message

Enforce uniqueness for the processed event ID. If the event has already been handled, skip the state change and acknowledge the duplicate. A crash after commit but before acknowledgement will cause a redelivery; the next attempt sees the recorded ID and does not reserve stock again. Acknowledging before the transaction commits risks losing the work.

A database transaction cannot cover an external payment API. Pass a stable idempotency key to that API when it supports one, and define how the workflow recovers if the request times out after the provider has acted. The API idempotency and safe replays guide goes deeper on request keys and duplicate effects.

4. Retry transient failures and isolate poison messages

A temporary database timeout may succeed on another attempt. A malformed event will not. Classify failures, use bounded retries with backoff for transient errors, and send messages that exceed the retry policy to a dead-letter queue (DLQ). Immediate requeue loops can keep a bad message hot while preventing useful work from moving forward.

Keep enough DLQ context to investigate: event ID, order ID, schema version, attempt count, timestamps, and a concise error. Restrict access if payloads contain sensitive data. Treat a DLQ as work that still needs an owner. Review it, fix the cause, and redrive only after the consumer can handle the message.

If events for an order must stay in sequence, define how a dead-lettered event affects later events for that order. Blocking a whole topic may be too broad; silently processing later events may corrupt state. The right policy depends on the business invariant.

5. Observe the whole order path

Propagate a trace or correlation ID across the request, outbox row, relay, broker message, and consumer. Keep the event ID and order ID in logs so an operator can connect each handoff without logging the whole payload.

Track the age and count of unpublished outbox rows, relay publish failures, queue depth and oldest-message age, consumer failures and redeliveries, and DLQ count and age. Also measure the time from order acceptance to the downstream state the customer cares about. A queue can be deep during a planned burst; rising message age or a growing DLQ is a clearer sign that the workflow is falling behind.

Alerts should follow the service’s completion target. For example, if an order should reach fulfillment within a defined window, alert on the age of the oldest unprocessed order event before customers start reporting missing orders.

Broker-specific references

Choose the transport after the workflow’s delivery, ordering, and replay needs are clear. The references below describe broker-specific behavior. Your application still needs an outbox and an idempotent consumer.

Category

Related Posts

Event Sourcing: Projections, Replay, and Recovery

An operations guide to event streams, idempotent projections, schema changes, snapshots, lag, and safe read-model rebuilds.

#microservices #cqrs #event-sourcing

GraphQL Federation: Subgraphs, Routing, and Trade-offs

Learn how GraphQL federation composes subgraphs, routes operations, and affects service ownership, query planning, latency, failure handling, and authorization.

#graphql #federation #microservices

Amazon Architecture: History and Service Design

Separate documented Amazon and AWS milestones from popular retellings, then apply the lesson about service boundaries to a current request flow.

#microservices #amazon #architecture