Redis Locking for a KEY

1,917 views
Skip to first unread message

Madhu Sudhanarao palepu

unread,
Sep 28, 2016, 3:03:03 AM9/28/16
to Redis DB
Hi,

Can I lock a particular KEY in Redis?

import redis
import time

conn = redis.StrictRedis(host='localhost', port=6379, db=0)

print ("previous Connected Key value is :" + conn.get('connected'))
print ("previous Operational Key value is :" + conn.get('operational'))

have_lock = False
my_lock = redis.Redis().lock("my_key")
try:
    have_lock = my_lock.acquire(blocking=False)
    #have_lock = my_lock.acquire(timeout=5)
    if have_lock:
        print("Got lock. Doing some stuff...")
        time.sleep(15)
        conn.set('connected', 'false')
        conn.set('operational', 'false')
    else:
        print("Did not acquire lock.")

finally:
    if have_lock:
        my_lock.release()
print ("After Connected Key value is :" + conn.get('connected'))
print ('After Operational Key value is :' + conn.get('operational'))


I tried with above example but it didn't work, the KEY still accessible from redis-cli and I can set even lock is acquired.

Any help is appreciated.

-Thanks,

Marc Gravell

unread,
Sep 28, 2016, 3:19:05 AM9/28/16
to Redis DB

Redis does not have a locking mechanism in the sense of blocking other connections. Instead, it relies on connections agreeing on common rules and respecting them. For example, it is common to use a key to *indicate* that you have a notional lock. That doesn't block anyone, but it is expected that other connections will try to acquire the same key, and if they can't: *then they don't do their thing* - they might do a pause / retry until they successfully take the lock, for example.

Marc


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

Madhu Sudhanarao palepu

unread,
Sep 28, 2016, 4:57:51 AM9/28/16
to Redis DB
Thanks for your reply!

My requirement is:

I want to execute a script through which I can set some KEYS, at the same time I can execute a java based application where the same KEY can be reset.
This whole process should be synchronized.

I tried the above script where executing multiple instances, it is acquiring the lock properly 
but from java based or using redis-cli still I can access the KEY.

Can I do synchronize KEY from multiple access?

-Madhu

Marc Gravell

unread,
Sep 28, 2016, 8:58:24 AM9/28/16
to redi...@googlegroups.com
Can I do synchronize KEY from multiple access?

Not from inside redis; to repeat - it does not support a blocking-style lock mechanism. You need to co-ordinate this from the calling code by following some convention. Two such conventions are discussed in the documentation; "redlock": http://redis.io/topics/distlock and something simpler using set with nx: http://redis.io/commands/set

To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+unsubscribe@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc

Madhu Sudhanarao palepu

unread,
Sep 30, 2016, 7:25:27 AM9/30/16
to Redis DB
Okay.

Thanks for the reply.
Regards,

Marc
Reply all
Reply to author
Forward
0 new messages