Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
System Design, Redis & Distributed Systems 10+ Years (Principal / Architect) Expert Production Scenario

How do you design an Idempotent Payment Processing Architecture with the Saga Pattern across Microservices?

Real-World Scenario / Incident Description

An e-commerce purchase requires 3 microservices: 1) Deduct Inventory, 2) Charge Credit Card, 3) Issue Invoice. How do you ensure consistency if Step 2 succeeds but Step 3 fails?

534 views • Updated 1 month ago

Choreography vs Orchestration Saga

Because 2-Phase Commit (2PC) blocks distributed databases, distributed systems use Saga Pattern:

  • A sequence of local transactions where each step publishes an event.
  • If any step fails, the Saga executes Compensating Transactions in reverse order (e.g. RefundPayment, RestoreInventory).
[Order Created] -> (Deduct Inventory) -> (Charge Stripe) -> [Invoice Failed]
                                                |
                                                v
                              [Trigger Compensating Action]
                                       Refund Stripe
                                     Restore Inventory
                                    Mark Order Cancelled

Idempotency Keys:

Each step checks if the order_id has already been processed to safely allow retries without duplicate charges.

Related Concepts: #Saga Pattern #Distributed Transactions #Idempotency #Compensating Actions