Amit Tomar
Amit Tomar

Reputation: 4858

Mutual Exclusion using Redis

I read about Redis being used for mutual exclusion. Docs say the following :

enter image description here

Can somebody explain what condition can this simple implementation fail for achieving mutual exclusion.

For all the instance trying to do some critical work, do the following (pseudo code) :

while ( set keyForMutex anyValue nx == nil )
{
    sleep ( 200 ms )
}

Finish up the critical task.

del keyForMutex

Upvotes: 0

Views: 450

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49942

This wouldn't fail in achieving mutual exclusion, but it could over-succeed and deliver infinite exclusive exclusion if your code dies before it gets to deleting the key.

A possible solution is to expire the key after a generous timeout to allow the critical task to end.

Upvotes: 1

Related Questions