Does all the Redis operations are atomic?

2,414 views
Skip to first unread message

Anshul Sharma

unread,
May 11, 2020, 7:18:59 AM5/11/20
to Redis DB
Does all the Redis operations are atomic? if one of the method is performing just simple Redis increment operation do I need to synchronize it to make it thread-safe or Redis will do it for us(if operations are atomic in nature)?

Tornike T

unread,
May 11, 2020, 7:59:57 AM5/11/20
to redi...@googlegroups.com
If you have race condition you need to change it within WATCH, MULTI, EXEC , but it won't work into cluster configuration;
redis automatically  won't syncronize if you need to update one value from different threads.

On Mon, May 11, 2020 at 3:18 PM Anshul Sharma <anshulsha...@gmail.com> wrote:
Does all the Redis operations are atomic? if one of the method is performing just simple Redis increment operation do I need to synchronize it to make it thread-safe or Redis will do it for us(if operations are atomic in nature)?

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/redis-db/3a826dfe-b050-41c2-8002-c2f034d0244d%40googlegroups.com.

Anshul Sharma

unread,
May 11, 2020, 8:09:04 AM5/11/20
to redi...@googlegroups.com
Hi Tornike, 

Do you think it is necessary even if the INCR is an atomic operation, Could you please help with a simple example.


Tornike T

unread,
May 11, 2020, 8:20:14 AM5/11/20
to redi...@googlegroups.com

if you read the Little redis book at some point this sentence comes. 

"You might not know it, but Redis is actually single-threaded, which is how every command is guaranteed to be atomic. While one command is executing, no other command will run."

INCR command won't need any additional actions to make it thread safe(single cluster), but if you do it manually, like:

 get value;

value ++;

set value;


you will need to make it thread safe manually;


Nitin Kumar Mangalam

unread,
May 24, 2022, 9:17:59 AM5/24/22
to Redis DB
Sorry for reviving this thread. I have been trying to debug an issue and I wasn't able to find a concrete answer online.
Is INCR command thread safe in a Redis cluster with cluster mode enabled?

We have a python code like this:

connection = cluster_cache.connection
pipeline = connection.pipeline(False)

for j in range(max(start_i, 0), min(end_i, 48)):
    subkey = str(j)
    pipeline.hincrby(key, subkey, increment)

pipeline.expire(key, TTL)
pipeline.execute()

This is called by concurrently by multiple threads. The updated values don't seem to be what we would expect. But as we are incrementing values for fields in the same key, this should not see any race condition?
Reply all
Reply to author
Forward
0 new messages