Bird
Raised Fist0
Microservicessystem_design~12 mins

Outbox pattern for reliable events in Microservices - Architecture Diagram

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
System Overview - Outbox pattern for reliable events

The Outbox pattern ensures reliable event delivery in microservices by storing events in a local database table (the outbox) as part of the same transaction that updates the business data. A separate process reads these events and publishes them to the message broker, guaranteeing no events are lost even if failures occur.

This pattern helps maintain data consistency and reliable communication between services.

Architecture Diagram
User
  |
  v
API Gateway
  |
  v
Service A (Business Logic + Outbox DB)
  |
  v
Outbox Poller
  |
  v
Message Broker
  |
  v
Service B (Event Consumer)
  |
  v
Service B Database
Components
User
user
Initiates requests to the system
API Gateway
api_gateway
Receives user requests and routes them to Service A
Service A (Business Logic + Outbox DB)
service
Processes business logic and writes events to the outbox table in the same transaction
Outbox Poller
service
Reads events from the outbox table and publishes them to the message broker
Message Broker
message_queue
Delivers events asynchronously to interested services
Service B (Event Consumer)
service
Consumes events from the message broker and updates its own database
Service B Database
database
Stores data updated by Service B based on received events
Request Flow - 7 Hops
UserAPI Gateway
API GatewayService A (Business Logic + Outbox DB)
Service A (Business Logic + Outbox DB)Service A (Business Logic + Outbox DB)
Outbox PollerService A (Business Logic + Outbox DB)
Outbox PollerMessage Broker
Message BrokerService B (Event Consumer)
Service B (Event Consumer)Service B Database
Failure Scenario
Component Fails:Message Broker
Impact:Events cannot be delivered to Service B, causing delays in event processing
Mitigation:Outbox Poller keeps events in the outbox table until broker is available; retries publishing events; message broker should be highly available with replication
Architecture Quiz - 3 Questions
Test your understanding
Why does Service A write events to the outbox table in the same transaction as business data updates?
ATo ensure events are not lost if the business update fails
BTo speed up event delivery to Service B
CTo avoid using a message broker
DTo reduce database storage
Design Principle
The Outbox pattern ensures reliable event delivery by combining business data updates and event recording in a single transaction, then asynchronously publishing events. This avoids lost or duplicated events and maintains data consistency across microservices.

Practice

(1/5)
1. What is the main purpose of the Outbox pattern in microservices?
easy
A. To store user session data for microservices
B. To ensure events are stored and published reliably with data changes
C. To replace the need for message queues entirely
D. To speed up database queries by caching events

Solution

  1. Step 1: Understand the problem Outbox pattern solves

    The Outbox pattern ensures that events related to data changes are not lost and are reliably published.
  2. Step 2: Identify the main purpose

    It stores events in the same database transaction as the data change, so both succeed or fail together, ensuring consistency.
  3. Final Answer:

    To ensure events are stored and published reliably with data changes -> Option B
  4. Quick Check:

    Outbox pattern = reliable event storage and publishing [OK]
Hint: Outbox pattern links events with data changes atomically [OK]
Common Mistakes:
  • Thinking it speeds up queries
  • Believing it replaces message queues
  • Confusing it with session storage
2. Which of the following is the correct sequence in the Outbox pattern?
easy
A. Write event to outbox table, commit transaction, then publish event
B. Publish event, then write event to outbox table, then commit transaction
C. Commit transaction, then write event to outbox table, then publish event
D. Publish event and write to outbox table simultaneously outside transaction

Solution

  1. Step 1: Understand transaction order in Outbox pattern

    The event is first written to the outbox table inside the same transaction as the data change.
  2. Step 2: Commit transaction before publishing

    Only after the transaction commits successfully, a separate process reads and publishes the event.
  3. Final Answer:

    Write event to outbox table, commit transaction, then publish event -> Option A
  4. Quick Check:

    Outbox write before commit, publish after commit [OK]
Hint: Events must be saved before commit, published after [OK]
Common Mistakes:
  • Publishing before commit causes lost events
  • Writing outbox after commit breaks atomicity
  • Trying to publish and write outside transaction
3. Given this pseudocode for an Outbox pattern implementation, what will be the output if the transaction fails?
begin transaction
write data change
write event to outbox
commit transaction
publish event from outbox
medium
A. Neither data change nor event is saved or published
B. Event is published but data change is lost
C. Data change saved but event not published
D. Event published twice

Solution

  1. Step 1: Analyze transaction failure impact

    If the transaction fails, none of the writes (data change or outbox event) are committed to the database.
  2. Step 2: Understand event publishing dependency

    Since the event is published only after commit, no event will be published if commit fails.
  3. Final Answer:

    Neither data change nor event is saved or published -> Option A
  4. Quick Check:

    Failed transaction means no data or event saved [OK]
Hint: Failed transaction means no commit, no event published [OK]
Common Mistakes:
  • Assuming event publishes despite transaction failure
  • Thinking data change saves without commit
  • Believing event publishes twice
4. A developer notices some events are missing in the message queue after using the Outbox pattern. What is the most likely cause?
medium
A. Events are duplicated in the outbox table
B. Events are published before the transaction commits
C. The database transaction is too fast
D. The outbox table is not being read and events published after commit

Solution

  1. Step 1: Identify missing events cause

    If events are missing in the queue, it usually means the process that reads the outbox and publishes events is not running or failing.
  2. Step 2: Rule out other causes

    Publishing before commit risks lost events, but missing events usually mean no publishing process or failure to read outbox.
  3. Final Answer:

    The outbox table is not being read and events published after commit -> Option D
  4. Quick Check:

    Missing events = outbox not read or published [OK]
Hint: Missing events? Check outbox reader process [OK]
Common Mistakes:
  • Assuming transaction speed causes missing events
  • Thinking events publish before commit is safe
  • Confusing missing events with duplicates
5. You want to design a microservice using the Outbox pattern to handle user registrations and notify other services. Which approach best ensures no events are lost and services stay consistent?
hard
A. Publish event first, then write user data; rollback event if data write fails
B. Write user data first, then publish event immediately without transaction; retry on failure
C. Write user data and event to outbox in one transaction; use a separate reliable process to publish events asynchronously
D. Write user data and publish event in the same transaction synchronously

Solution

  1. Step 1: Ensure atomicity of data and event writes

    Writing user data and event to the outbox table in the same transaction guarantees both succeed or fail together.
  2. Step 2: Use separate process for event publishing

    Publishing events asynchronously from the outbox ensures reliable delivery without blocking the main transaction.
  3. Final Answer:

    Write user data and event to outbox in one transaction; use a separate reliable process to publish events asynchronously -> Option C
  4. Quick Check:

    Atomic write + async publish = reliable and consistent [OK]
Hint: Atomic write + async publish ensures no lost events [OK]
Common Mistakes:
  • Publishing events outside transaction without retry
  • Publishing before data write risks inconsistency
  • Trying synchronous publish inside transaction