What is distributed Lock Manager in C# and Redis

What is distributed Lock Manager in C# and Redis

Mr. Rohit Chodvadiya

2–3 minutes
Share on Social Media

    Get The Expert Advice To Grow Your Business Digitally
    Related Blogs
    How to Start with Enterprise Mobile and IOT Strategy
    Read More: How to Start with Enterprise Mobile and IOT Strategy
    A Guide to Built-in Scripting for Advanced Postman Automation API Testing
    Read More: A Guide to Built-in Scripting for Advanced Postman Automation API Testing
    What is the life cycle of AngularJS?
    Read More: What is the life cycle of AngularJS?
    BLoC Flutter State Management Explained:A Step-by-Step Guide
    Read More: BLoC Flutter State Management Explained:A Step-by-Step Guide

    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.

    static bool CanIAccessResource(string Key)
    {
        var redisKey = new RedisKey(Key);
        var redisValue = new RedisValue(DateTime.UtcNow.ToString());
        return _redisDB.StringSet(redisKey, redisValue, null, When.NotExists);
    }
    static bool ReleaseResource(string Key)
    {
        var redisKey = new RedisKey(Key);
        return _redisDB.KeyDelete(redisKey);
    }
    static void WithdrawAmount(string AccountNo, double Amount)
    {
        do
        {
            var isAccessible = CanIAccessResource(AccountNo);
            if(isAccessible)a
            {
                UpdateBalance(AccountNo, Amount);
                ReleaseResource(AccountNo);
                break;
            }
            else
            {
                Thread.Sleep(_waitTime);
            }
        }
        while (true);
    }

    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.

    Share on Social Media

      Get The Expert Advice To Grow Your Business Digitally
      Related Blogs
      Streamlining EMS:Neo4j’s Power to solve Supply Chain Challenges
      Read More: Streamlining EMS:Neo4j’s Power to solve Supply Chain Challenges
      Modernizing with New Identity of Triveni Global Software Solution LLP
      Read More: Modernizing with New Identity of Triveni Global Software Solution LLP
      What is the identity server in ASP.NE core ?
      Read More: What is the identity server in ASP.NE core ?
      What is API Request and Response Logger
      Read More: What is API Request and Response Logger

      Stay ahead of the curve

      Get the latest insights, tutorials, and industry news delivered straight to your
      inbox. Join 10,000+ developers and tech leaders.

      Get In Touch