4 ms·
Here's how you can do this. You have 3 accounts: A, B and in-flight. 1. Debit A, Credit in-flight. 2. Credit B, Debit in-flight. If 1. fails, nothing happene
by hcarvalhoalves 2y ago
Here's how you can do this. You have 3 accounts: A, B and in-flight.
1. Debit A, Credit in-flight.
2. Credit B, Debit in-flight.
If 1. fails, nothing happened and everything is consistent.
If 2. fails, you know (because you have money left on in-flight), and you can retry later, or refund A.
This way at no point your total balance decreases, so everything is always consistent.
- withinboredom 2y agoIt can fail at your commas in 1&2, then you are just as broke as everyone else. This isn't an easy-to-solve problem when it comes to distributed computing.
- hcarvalhoalves 2y agoIt should be an atomic transaction, double-entry style, so it can’t fail between commas. The important thing is not having money go missing.
- withinboredom 2y agoThe only way that is possible is if the tables exist on the same database server. Otherwise, you are right back at distributed transaction problems.
- hcarvalhoalves 2y agoThat's the whole point of double-entry. You don't split the entries.
- withinboredom 2y agoThen this is off-topic af. We are talking about distributed systems here.