Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>CouchDB isn't suitable for transactional systems because it doesn't support locking and atomic operations.</p> <p>In order to complete a bank transfer you must do a few things:</p> <ol> <li>Validate the transaction, ensuring there are sufficient funds in the source account, that both accounts are open, not locked, and in good standing, and so on</li> <li>Decrease the balance of the source account</li> <li>Increase the balance of the destination account</li> </ol> <p>If changes are made in between any of these steps the balance or status of the accounts, the transaction could become invalid after it is submitted which is a big problem in a system of this kind.</p> <p>Even if you use the approach suggested above where you insert a "transfer" record and use a map/reduce view to calculate the final account balance, you have no way of ensuring that you don't overdraw the source account because there is still a race condition between checking the source account balance and inserting the transaction where two transactions could simultaneous be added after checking the balance.</p> <p>So ... it's the wrong tool for the job. CouchDB is probably good at a lot of things, but this is something that it really can not do.</p> <p>EDIT: It's probably worth noting that actual banks in the real world use eventual consistency. If you overdraw your bank account for long enough you get an overdraft fee. If you were very good you might even be able to withdraw money from two different ATMs at almost the same time and overdraw your account because there's a race condition to check the balance, issue the money, and record the transaction. When you deposit a check into your account they bump the balance but actually hold those funds for a period of time "just in case" the source account doesn't really have enough money.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload