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
    Your information will be securely sent to and stored in Google Sheets for the purpose of processing your form submission.
    Related Blogs
    CTE in MS SQL Server - feature
    How to Use CTEs in MS SQL Server for Hierarchical Data and Query Optimization
    Read More: How to Use CTEs in MS SQL Server for Hierarchical Data and Query Optimization
    13 Essential Backup Guidelines and Data Loss Prevention Techniques for Maximum Safety
    Read More: 13 Essential Backup Guidelines and Data Loss Prevention Techniques for Maximum Safety
    Importance of Estimation in Software Development The First Step to Success
    Read More: Importance of Estimation in Software Development The First Step to Success
    Generative AI and Its Impact: Everything You Need to Know About This Creative Technology
    Read More: Generative AI and Its Impact: Everything You Need to Know About This Creative Technology

    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
      Your information will be securely sent to and stored in Google Sheets for the purpose of processing your form submission.
      Related Blogs
      Modernizing with New Identity of Triveni Global Software Solution LLP
      Read More: Modernizing with New Identity of Triveni Global Software Solution LLP
      What is Docker? How to Deploy and Install Redis in Docker
      Read More: What is Docker? How to Deploy and Install Redis in Docker
      What is JavaScript Spread Operator?
      Read More: What is JavaScript Spread Operator?
      Programming C# - blog feature
      Parallel Programming in C#: Different Use Cases
      Read More: Parallel Programming in C#: Different Use Cases

      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.

      Explore More