A practical guide to distributed transactions with the saga pattern in Go. Build an orchestrated saga, roll back failed steps with compensating transactions, and retry failed compensations with Kafka retry topics and a dead letter queue.
Prerequisites
What do you need to follow along?
Working knowledge of Golang.
Go & Docker installed on your PC.
Basic knowledge of Kafka. If you need a refresher, my guide on fault-tolerant Kafka pipelines covers everything we use here.
At least 8 hours of sleep (too much to ask for?)
The Monolith
Say you have a small e-commerce web app (for simplicity, and also because I want it to match my code examples). You built it as a monolith, and everything works well together.
In this system, when a user makes a purchase, everything happens in one app with one database. So we can use a database transaction to perform all the operations needed. For simplicity, say our operations are:
Place the order.
Reserve the stock.
Process the payment.
Three simple enough steps.…