Bird
Raised Fist0
Microservicessystem_design~10 mins

Outbox pattern for reliable events in Microservices - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert an event into the outbox table within the same transaction.

Microservices
BEGIN TRANSACTION;
INSERT INTO orders (id, status) VALUES (123, 'created');
INSERT INTO outbox (event_type, payload) VALUES ('OrderCreated', [1]);
COMMIT;
Drag options to blanks, or click blank then click option'
A'{"orderId":123}'
B123
CNULL
Dorder_data
Attempts:
3 left
💡 Hint
Common Mistakes
Inserting NULL instead of event data.
Using a raw number instead of a JSON string.
Forgetting to insert the event in the same transaction.
2fill in blank
medium

Complete the code to select unprocessed events from the outbox for publishing.

Microservices
SELECT id, event_type, payload FROM outbox WHERE processed = [1];
Drag options to blanks, or click blank then click option'
ATRUE
BNULL
C1
DFALSE
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting events where processed is TRUE (already processed).
Using NULL which does not filter correctly.
3fill in blank
hard

Fix the error in the code that marks an event as processed after publishing.

Microservices
UPDATE outbox SET processed = [1] WHERE id = event_id;
Drag options to blanks, or click blank then click option'
AFALSE
BTRUE
C0
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Setting processed to FALSE or 0 after publishing.
Setting processed to NULL which is ambiguous.
4fill in blank
hard

Fill both blanks to implement a retry mechanism that selects events with less than 5 attempts and marks them as processing.

Microservices
SELECT id FROM outbox WHERE processed = FALSE AND attempts [1] 5;
UPDATE outbox SET processing = TRUE WHERE id = [2];
Drag options to blanks, or click blank then click option'
A<
B>
C=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' to filter attempts.
Using '!=' instead of '=' in the update statement.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps event IDs to payloads for unprocessed events with attempts less than 3.

Microservices
events = {event['id']: event[[1]] for event in events_list if event[[2]] [3] 3 and event['processed'] == False}
Drag options to blanks, or click blank then click option'
Apayload
Battempts
C<
Dprocessed
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'processed' as the value instead of 'payload'.
Using '>' instead of '<' for attempts comparison.

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