Locking Mechanism in Distributed Architecture

Typically many developers may face issues of race condition or deadlock, When they work with multi threading processing and shared resources. So they need some kind of synchronization mechanism so that multiple threads can not access shared resources simultaneously. Microsoft .Net framework provides many built-in classes like Mutex, Semaphore, EventWaitHandle, AutoResetEvent, ManualResetEvent etc. to synchronize the threads. But this class provides inter-process communication between threads or even in multiple processes in a single node. But what about in distributed systems? Distributed system is much more complex than the stand alone system, the main question that arises is how to coordinate several processes running on different systems? We need some central approach. Redis is a good approach for its special feature of locking based on expiration timeout. We can also use it to control rate limit (request) control in the system. Please go through a demo that will give you more ideas regarding this.

For E.g.

Suppose there are two processes called Process-A & Process-B. Both Processes are running in different nodes. Both Processes want to withdraw the amount in the same account number. What if there is a NoSql database like mongodb which does not support any lock mechanism and we require to create our own mechanism in the distributed system. We can use Redis approach here.

Suppose Current Balance : 2000;
Process – A : db.collection.update({Balance: Balance – 2000}, {AccountNo:12345});
Process – B : db.collection.update({Balance: Balance – 2000}, {AccountNo:12345});

What happens here when both processes try to update simultaneously ? Process A & B both read 2000 balances. Both try to withdraw the amount at same time and Balance becomes 2000(Actual Balance) – 2000(Process A) – 2000(Process B) = -4000 How to overcome this situation ? We need a central lock mechanism. Please go through the Demo.

Demo Code & Explanation:

Please check the below image. One process wants to withdraw the amount. But it needs to first check if the resource is accessible or not using CanIAccessResource method. I have used redis stack exchange. _redisDB is a IDatabase object. It contains the one method named StringSet which is used for storing Key/Value pairs in redis. The StringSet method will play a main role. It contains four parameters. 1) Key 2) Value 3) Expiration 4) Option (which is very important ). So the presence of the 4th option would depict that if the key does not exist then store it and return true otherwise return false. Redis is thread safe so no other thread would overwrite the same key. We can assure that the StringSet method returns true and thread allows access. if another thread tries to access the same key, it would return false so it won’t get access. After work is finished, thread would release the resource.

Team Triveni (Technical)
About Triveni

Triveni is such strong and trustworthy name in the field of customised IT solution provider which is earnest name in Indian IT sector.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Settings
We use cookies to enhance your experience while using our website. If you are using our Services via a browser you can restrict, block or remove cookies through your web browser settings. We also use content and scripts from third parties that may use tracking technologies. You can selectively provide your consent below to allow such third party embeds. For complete information about the cookies we use, data we collect and how we process them, please check our Privacy Policy
Consent to display content from Youtube
Consent to display content from Vimeo
Google Maps
Consent to display content from Google
Consent to display content from Spotify
Sound Cloud
Consent to display content from Sound