Saga Pattern Implementation using Spring Boot and Kafka
Flow
Note: userId and productId values can be 0-9, because, in the services, we are generating some dummy data.
- Once request has been placed then it will produce an event to
order-topic
. payment-service
andinventory-service
listen thatorder-topic
and based on theorder-status
(also for other conditions, like checking stock and checking user money), they try to process that event and produce the results with new status toorder-result-topic
.order-service
listens theorder-result-topic
and it decides the final result based on the status from those events(if everything is okay then it producesCOMPLETED
otherwiseROLLBACK
).
Note: You may think that since
order-service
listens theorder-result-topic
and what if one of the other services sendsACCEPT
and other withREJECT
status. So, theorder-service
consumes that topic one by one and other services produces the event to that topic withorderId
and this will guarantee that they will be in same partition always. You can check the implementation details.
- Then
payment-service
andinventory-service
will complete the process based on the new event with new status from theorder-service
.
You can easily run the project using run.sh
file.
Be sure that you have mvn
, java
and docker
available locally.