Lock Manager in C# and Redis

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.

Mr. Rohit Chodvadiya