Bird
Raised Fist0
Microservicessystem_design~10 mins

Saga pattern for distributed transactions 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 define a saga step that triggers a local transaction.

Microservices
def saga_step():
    # Start local transaction
    [1]()
    # Proceed to next step
Drag options to blanks, or click blank then click option'
Abegin_transaction
Bcommit_transaction
Crollback_transaction
Dsend_compensate
Attempts:
3 left
💡 Hint
Common Mistakes
Using commit_transaction instead of begin_transaction.
2fill in blank
medium

Complete the code to send a compensation event when a saga step fails.

Microservices
def handle_failure():
    # Send compensation event
    [1]('compensate')
Drag options to blanks, or click blank then click option'
Astart_transaction
Brollback_transaction
Ccommit_transaction
Dsend_event
Attempts:
3 left
💡 Hint
Common Mistakes
Using commit_transaction instead of sending an event.
3fill in blank
hard

Fix the error in the saga coordinator code to correctly handle step completion.

Microservices
def saga_coordinator(event):
    if event.type == 'step_completed':
        [1]()  # Proceed to next saga step
Drag options to blanks, or click blank then click option'
Astart_saga
Btrigger_next_step
Csend_compensation
Drollback_saga
Attempts:
3 left
💡 Hint
Common Mistakes
Calling send_compensation instead of triggering next step.
4fill in blank
hard

Fill both blanks to define a saga step that executes a local transaction and sends a success event.

Microservices
def execute_step():
    [1]()  # Commit transaction
    [2]('step_completed')  # Notify coordinator
Drag options to blanks, or click blank then click option'
Acommit_transaction
Bsend_event
Cbegin_transaction
Drollback_transaction
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the order or using rollback instead of commit.
5fill in blank
hard

Fill all three blanks to implement a saga step that begins a transaction, handles failure by sending compensation, and commits on success.

Microservices
def saga_step_handler():
    [1]()  # Begin transaction
    try:
        process_local_transaction()
        [2]()  # Commit transaction
    except Exception:
        [3]('compensate')  # Send compensation event
Drag options to blanks, or click blank then click option'
Abegin_transaction
Bcommit_transaction
Csend_event
Drollback_transaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using rollback_transaction instead of sending compensation event.

Practice

(1/5)
1. What is the main purpose of the Saga pattern in microservices?
easy
A. To replicate data across multiple databases synchronously
B. To manage distributed transactions by breaking them into smaller steps with compensations
C. To speed up database queries by caching results
D. To lock all resources until the transaction completes

Solution

  1. Step 1: Understand distributed transactions challenges

    Distributed transactions across microservices are hard because locking resources is inefficient and can cause delays.
  2. Step 2: Identify Saga pattern role

    The Saga pattern breaks a big transaction into smaller steps, each with a compensating action to undo if needed, avoiding locks.
  3. Final Answer:

    To manage distributed transactions by breaking them into smaller steps with compensations -> Option B
  4. Quick Check:

    Saga pattern = distributed transaction management [OK]
Hint: Saga means small steps with undo actions for transactions [OK]
Common Mistakes:
  • Thinking Saga locks resources like traditional transactions
  • Confusing Saga with caching or replication
  • Assuming Saga runs all steps in parallel
2. Which of the following is the correct sequence in a Saga pattern transaction?
easy
A. Execute steps and compensations simultaneously
B. Run compensations first, then execute all steps
C. Execute only compensations without any steps
D. Execute steps sequentially, then run compensations if any step fails

Solution

  1. Step 1: Understand Saga execution flow

    Saga executes each step in order. If a step fails, compensations undo previous steps.
  2. Step 2: Confirm correct sequence

    Compensations run only after a failure, never before or simultaneously with steps.
  3. Final Answer:

    Execute steps sequentially, then run compensations if any step fails -> Option D
  4. Quick Check:

    Steps then compensations = correct Saga flow [OK]
Hint: Steps run first; compensations only if failure occurs [OK]
Common Mistakes:
  • Running compensations before any step
  • Running steps and compensations at the same time
  • Skipping compensations on failure
3. Consider a Saga with three steps: A, B, and C. Step B fails after A succeeds. What happens next?
medium
A. Saga retries step B indefinitely without compensation
B. Step C runs regardless of failure
C. Compensation for step A runs, then Saga aborts
D. No compensation runs; Saga commits partial results

Solution

  1. Step 1: Analyze failure impact in Saga

    When step B fails, Saga must undo previous successful steps to keep data consistent.
  2. Step 2: Identify compensation actions

    Compensation for step A runs to rollback its changes, then Saga aborts without running step C.
  3. Final Answer:

    Compensation for step A runs, then Saga aborts -> Option C
  4. Quick Check:

    Failure triggers compensation rollback [OK]
Hint: Failure in middle triggers compensations backward [OK]
Common Mistakes:
  • Assuming later steps run after failure
  • Thinking Saga retries endlessly without rollback
  • Ignoring compensation steps
4. A developer implemented a Saga but noticed data inconsistencies after failures. What is the most likely cause?
medium
A. Compensation actions are missing or incomplete
B. All steps are executed synchronously
C. Steps are too small and independent
D. Saga pattern locks all resources during execution

Solution

  1. Step 1: Identify cause of inconsistencies

    Data inconsistencies after failure usually mean rollback (compensation) did not happen properly.
  2. Step 2: Check compensation implementation

    If compensation actions are missing or incomplete, previous steps cannot be undone, causing inconsistency.
  3. Final Answer:

    Compensation actions are missing or incomplete -> Option A
  4. Quick Check:

    Missing compensation = inconsistency [OK]
Hint: Always implement full compensations for each step [OK]
Common Mistakes:
  • Assuming synchronous execution causes inconsistency
  • Believing small steps cause inconsistency
  • Thinking Saga locks resources like traditional transactions
5. You design a payment system using Saga pattern with steps: debit account, reserve inventory, and confirm order. If inventory reservation fails, what should happen?
hard
A. Run compensation to credit back the debited amount and abort order confirmation
B. Ignore failure and proceed to confirm order
C. Retry inventory reservation indefinitely without compensation
D. Lock all services until inventory is reserved

Solution

  1. Step 1: Understand Saga compensation in payment flow

    If inventory reservation fails, previous successful steps (debit account) must be undone to avoid inconsistent state.
  2. Step 2: Apply compensation and abort

    Compensation credits back the debited amount, and order confirmation is aborted to maintain consistency.
  3. Final Answer:

    Run compensation to credit back the debited amount and abort order confirmation -> Option A
  4. Quick Check:

    Failure triggers compensation rollback and abort [OK]
Hint: Failure in middle step triggers rollback of prior steps [OK]
Common Mistakes:
  • Proceeding despite failure causing inconsistent state
  • Retrying endlessly without rollback
  • Locking services defeats Saga benefits