Home > database >  How I can handle the multiple transactions
How I can handle the multiple transactions

Time:07-19

I have a c# rest service which is distributed among across different servers. In the service I have a balance check for the incoming request amount and if balance is there, we proceed and create the transaction.

if(txnamount> balamount){
 // return
}
else{
//create txn
}

The balance amount is calculated using the previous txn amounts. If bulk request comes in quick session, balance check condition fails when previous txn amount is not reduced from the balance. How to solve this

CodePudding user response:

This is a fundamental limit of computer systems.

On a single system it is fairly trivial since you have locks built into the language and operating system.

For multiple systems the simple answer is to use a database and let it deal with locking etc. Databases are built specifically to deal with this kind of problem. Just open a transaction and do your operation inside the transaction. Or keep a version value on each row and fail commits if the version value has been changed between the read and write.

On a more fundamental level you need to ensure exclusive access. This could be done by a central authority to manage access (like a database). Or you could assign values to specific machines, so all requests for that value need to be run on that machine (like a database shard). Or you could use two phase locking. This is a topic you could probably spend your entire career on if you want to go really deep.

CodePudding user response:

It looks like you're dealing with something that requires Event sourcing technic, you need some sort of immutable state especially when dealing with financial problems

  • Related