Handling Distributed Transactions
Let’s say there is a monolith – it takes help of Order service-> Payment service to create an order and do the payment.
There are several steps involved here
- adding items,
- creation of order,
- payment
- updating of order and payment status.
This can be done easily and as an atomic operation in a monolith using Transactions.
In microservice world, there are ways to solve it :-
- Compensating transaction – Order service creates order passes flow to payment service which fails. The payment service should interact with the order service to get order status updated or deleted. It’s too many interactions and complexity in case it fails.
- Event based architecture – Order service creates order and publishes to Kafka topic. This is being subscribed by all payment service instances which hear and do their work and again send one more event to the Kafka topic. This topic is subscribed by all order services which update the order. This gives the complexity to the message broker.
- Combining all status – Order service and payment service combine all of their status and finally update into the DB. This is again prone to issues in case one or the message fails to come.