How banks can switch to more profitable operating models<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
Download now <\/span>\n\t\t\t<\/div>\n\t\t<\/a><\/div>\nNow there\u2019s a problem:<\/b> we have a transaction that\u2019s, at the same time, processed and cancelled. Here you go \u2013 that\u2019s a prime example of payment transaction inconsistency.<\/p>\n
But now we need to figure out which one is correct. And that\u2019s somewhat challenging as those two actions were processed in parallel.<\/p>\n
When we don\u2019t have a guarantee of consistency in our system, we can have acid in databases but still not have strong consistency guarantee in distributed systems. As a result, we have a three-way inconsistency between our system, customer and acquiring bank.<\/b> \n <\/p>\nThe technical fix: transaction locking<\/h2>\n
To solve this conundrum, we can do one great thing: apply a lock on transaction entity when we perform any operation with it. Locking gives us the guarantee that only one action can be processed at any given time.<\/p>\n
If that’s the case, here’s how the previous scenario would unroll: after the user hits cancel, we place a lock on transaction to cancel it. At the same time, the bank gets in line to process that transaction. After it acquired lock to proceed, our system already knows that the transaction is cancelled. So we send that info to the bank and it rolls back the transaction.<\/p>\n
Everyone\u2019s happy. \n <\/p>\n
Now let’s move on to the technical part of this post: a quick case study showing exactly how distributed locks can be applied towards payment transactions to ensure consistency.<\/p>\n
How to use distributed locks to achieve data consistency in payment processing<\/h2>\n Let\u2019s define how to implement distributed locks with .NET Core and Apache ZooKeeper and how to ensure payment data consistency. But first, here are some key terms first, we\u2019ll be using in this case study.<\/p>\n
What\u2019s a distributed lock?<\/h3>\n A distributed lock is just a primitive that allows different processes operate with shared resources in mutually exclusive way inside a distributed<\/b> environment. So, basically, it\u2019s the same locks we use in scope of operating systems but designed to work in a distributed environment.<\/p>\nWhy do we need distributed locks?<\/h3>\n
For two good reasons: \nEfficiency:<\/b> adding a lock means that no piece of work is done twice, which may result in minor errors in the system \u2013 executing scheduled jobs, processing some events like user registration and activation, and we\u2019ll not use more computing resources that we need. Hence, our system is effective and fast.<\/p>\n
Consistency:<\/b> If processing the same piece of work simultaneously can result in corrupted data, locking can help prevent its execution (as illustrated in the example above).<\/p>\nWhat does a CAP theorem have to do with all of that?<\/h3>\n
A CAP theorem<\/a> is the fundamental part to distributed systems. It says: \n\n\t\t\t\n\t\t\t\t\n\t\t\t\t
<\/linearGradient><\/defs><\/svg>\n\t\t\t\t\tIn the presence of a network partition, one has to choose between consistency and availability.<\/p>\n\t\t\t\t<\/div>\n\t\t\t\t