Why 'all or nothing' across several databases or services is so hard to get.
A distributed transaction is one job that must change data in more than one database or service, and must either happen fully or not at all.
In a single database, the ACID promises are kept for you. If you change two tables in one transaction and the power fails halfway, the database uses its log to undo the half-finished work. Either both changes exist or neither does.
Now think about an online order. The Payment service has its own database. The Inventory service has another. The Shipping service has a third. One order must charge the card, reserve the item and create a shipping label. These are three separate commits on three separate machines. If the second one fails after the first has succeeded, the customer has paid for something that was not reserved.
We want atomicity across machines: all three changes happen, or none. That is much harder than it sounds, because of two facts about distributed systems: any message can be lost or delayed, and any machine can crash at any moment. When no answer arrives, the sender cannot tell whether the other side did the work, did not do it, or is just slow.
| Trouble | What happens | Example |
|---|---|---|
| Partial failure | Some steps worked, others did not | Payment charged, inventory failed |
| Unknown outcome | No reply, so you cannot tell if it worked | Timeout after sending 'reserve the item' |
| Crash between steps | The coordinator or a service dies in the gap | App server dies after step 1 of 3 |
| Concurrent users | Others see or change the half-done data | Another order buys the last item while yours is waiting |
| Duplicate requests | A retry repeats work that already happened | The customer is charged twice |
| Approach | Promise | Cost |
|---|---|---|
| Keep the data in one database | Full ACID, the simplest | Hard to do when services must own their data |
| Two-Phase Commit (2PC) | All commit or all abort | Slow, locks held, blocks if the coordinator dies |
| SAGA | Each step is local, failures are undone by new steps | Others can see half-done states, and you must write the undo code |
| Outbox + retries + idempotency | A change and its message are never separated | At-least-once delivery, so receivers must handle duplicates |
A simplified model (no random numbers): each service does about 5 ms of local work. The all-or-nothing mode is two-phase commit, covered in the next topic. Real systems add logs, retries and timeouts on top of this.
| Term | Meaning in easy words |
|---|---|
| Distributed transaction | One unit of work that changes data in more than one database or service |
| Atomicity | All of the changes happen, or none of them do |
| Partial failure | Some parts of the system failed while others kept working |
| Coordinator | The one process that drives a commit protocol and decides the outcome |
| Participant | A database or service that takes part in the transaction |
| Commit / abort | Make the changes permanent, or throw them away |
| Compensation | A new action that reverses the effect of an earlier committed action (a refund) |
| Timeout | A limit on how long to wait for a reply. It does not tell you what happened |
| Idempotency | Doing the same request many times has the same effect as doing it once |
| Blocking | Participants must wait, holding locks, until a crashed process returns |
| Eventual consistency | The data may disagree for a short time but will agree later |
| Choice | We gain | We lose |
|---|---|---|
| Strong consistency (2PC) | Never a half-done state visible | Speed, availability and scale |
| Eventual consistency (SAGA) | Speed and independent services | A short window where data is inconsistent, plus compensation code |
| One shared database | Simple ACID transactions | Services are tied together and cannot scale or deploy alone |
| Long timeouts | Fewer false alarms | Locks held longer, users wait longer |
| Short timeouts | Fast failure | More 'unknown outcome' cases to handle |