Saga Design Pattern

Saga is a method to do Asynchronous Distributed Transactions
With 2 Phase Commit we can perform distributed transaction but, those are Blocking/Synchronous.
Read 2 Phase Commit first

Working of Saga

Again considering Customer ordering Pizza example.

                                                                            |-------------- Distributed DB -------------|
                                                                            |                                           |
                    orderSvc       KAFKA-BROKER           KitchenSvc     KitchenDB           TransactionSvc       TransactionDB
  -POST(Pizza 249)-> /order
                      -Topic:order-events->
                                          -Topic:order-event->
                                                            -Check ingredients->
                                                            <----available---
                                                            ---INSERT INTO---->
                                                            //Committed(no wait)//
                                          <-Topic:Kitchen_event-
                                          -----------------------Topic:Kitchen_event-------------->
                                                                                                  --Validate payment method->
                                                                                                  <----available----
                                                                                                  ----------INSERT INTO---->
                                                                                                    //Committed(no wait)//
                                          <-----------------------Topic:Transaction_event-----------
                        <-Topic:Kitchen_event
                        <-Topic:Transaction_event
Customer <------Order Confirmed
          
Steps
1. Every microservice writes to its DB (local transaction)
2. Publishes an event
3. Other services(consumers) react asynchronously, ie writes to their DBs and publishes event
4. if 1 service is dead, others are not blocked(as in 2 Phase commit)