I am working on a java based usecase ( target is to make an microservice for this ) in which concurrent debits and credits are occurring for a bank account. I want to make this operation thread safe with least possible SLA. In java there are three ways to achieve this by synchronised() blocks, ReadWriteLock and StampedLock. But for all these options Threads have to wait for availability of Lock. So if number of threads increase in future ,SLA of balance update API will increase.
is it any way to reduce this Lock dependency perfectly? I have thought of one design which is based on kafka. Before I proceed further on it,I want to know view of experts here on my approach. Please comment/suggest whether it will be effective solution or not.
Here is my approach:
(1) Payment producer posts Payment (say credit of $100) as Topic to kafka (properly replicated and configured to make sure No data loss)
(2) On successful Topic registration in Kafka, debit money (- $100) from Sender account and send successful transaction confirmation to Payment producer.
(3) Now Payment consumer read Payment Topic (credit of $100) and credit money (+ $100) to Receiver account.
In this approach Producers and consumers are not waiting for any lock so I believe balance update operation will remain efficient even if number of producer threads increase.
Please comment which option is better to use for this usecase?.