AWS Builder Center

Implementing the Transactional Outbox Pattern on AWS: Solving the Dual Write Problem

Introduction Event-driven architecture enables distributed services to communicate asynchronously.

AWS provides services such as Amazon SQS, Amazon SNS, AWS Lambda, Amazon EventBridge, and Amazon DynamoDB to build scalable distributed systems.
However, one important challenge remains: ensuring that database transactions and event publishing remain consistent.
Consider an application that updates an order in a database and then publishes an event to Amazon SQS.
What happens if the database transaction succeeds but the SQS publishing operation fails?
The business operation is completed, but downstream services never receive the event.
This problem is known as the Dual Write Problem.
The Transactional Outbox Pattern provides a reliable approach to address this problem.

1. Understanding the Dual Write Problem

Imagine an e-commerce application processing customer orders.
The application performs two independent operations:
Operation 1: Save the order in the database.
Operation 2: Publish an OrderCreated event to Amazon SQS.
These operations do not share a single atomic transaction.
There are two possible failure scenarios:
Scenario A: Database succeeds, message publishing fails.
  • Order is successfully stored.
  • SQS message is not published.
  • Inventory and notification services never receive the event.
Scenario B: Message publishing succeeds, database transaction fails.
  • Downstream services receive the event.
  • The order transaction is rolled back.
  • Consumers process an event for a business operation that never committed.
Both scenarios can create data consistency problems.

2. What Is the Transactional Outbox Pattern?

The Transactional Outbox Pattern solves this problem by storing business data and the corresponding event in the same database transaction.
Instead of directly publishing an event, the application writes the event into an Outbox table.
Example database structure:
Orders Table
order_id
customer_id
status
1001
C101
CREATED
Outbox Table
event_id
aggregate_id
event_type
status
E1001
1001
OrderCreated
PENDING
Both records are committed within the same transaction.
If the transaction fails, neither record is committed.
If the transaction succeeds, both records are persisted.
This prevents the database update from succeeding without also recording the event that must eventually be published.

3. AWS Architecture Using Amazon RDS

A common architecture uses:
  • Amazon RDS – Business database and Outbox table.
  • Outbox Publisher – Reads pending events.
  • AWS DMS or another CDC mechanism – Captures supported database changes.
  • Amazon SNS – Distributes events to subscribers.
  • Amazon SQS – Buffers messages.
  • AWS Lambda – Processes events.
  • Amazon CloudWatch – Monitors the pipeline.

Workflow

Step 1: Business Transaction
The application receives an order request.
Step 2: Database Transaction
The application inserts the order and corresponding Outbox event in one database transaction.
Step 3: Transaction Commit
Both records are committed atomically.
Step 4: Event Capture
A polling publisher or CDC mechanism detects the new Outbox event.
Step 5: Event Publishing
The publisher sends the event to SNS or SQS.
Step 6: Consumer Processing
Lambda or another consumer processes the event.
Step 7: Delivery Tracking
The system tracks publication and processing status and supports retries where necessary.

4. Implementing the Pattern with Amazon DynamoDB

DynamoDB provides another approach using transactional writes.
Consider two DynamoDB items:
  • Order item.
  • Outbox event item.
Use  TransactWriteItems  to write both items atomically.
Conceptual example:
import boto3
import uuid
from datetime import datetime, timezone

dynamodb = boto3.client("dynamodb")

order_id = "ORDER-1001"
event_id = str(uuid.uuid4())

dynamodb.transact_write_items(
TransactItems=[
{
"Put": {
"TableName": "Orders",
"Item": {
"order_id": {"S": order_id},
"status": {"S": "CREATED"}
}
}
},
{
"Put": {
"TableName": "Outbox",
"Item": {
"event_id": {"S": event_id},
"order_id": {"S": order_id},
"event_type": {"S": "OrderCreated"},
"created_at": {
"S": datetime.now(
timezone.utc
).isoformat()
}
}
}
}
]
)
If the transaction succeeds, both items are written.
If it fails, neither write is committed.
DynamoDB Streams can capture the Outbox item insertion and trigger downstream processing.
A Lambda function can then publish the event to SQS or SNS.
Important: DynamoDB Streams and Lambda event source mappings provide at-least-once processing behavior. The publisher and downstream consumers must handle retries and duplicate events.

5. Why Idempotency Is Essential

The Transactional Outbox Pattern does not guarantee exactly-once message delivery.
Consider this scenario:
  1. Publisher reads an Outbox event.
  1. Publisher sends it successfully to SQS.
  1. Publisher crashes before marking the event as published.
  1. Publisher restarts.
  1. The same event is published again.
Now the consumer receives a duplicate message.
This is expected behavior in an at-least-once delivery architecture.
Implement idempotency using a stable event ID.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article