I have a nodejs application running (haraka smtp server) with 1 master thread and 7 worker threads . I'm using redis to read some counters and after some operations I'm updating those counters.
I'm facing the issue of race condition where some of the threads are reading the counters before other threads actually updates it, hence there is a mismatch in counters. Ideally it should be like one thread do its read and write operation on that particular key and then others should proceed with there ops.
I have currently a single instance of redis running. Is it a way to avoid this race condition with multiple threads and single redis instance ?
I've read about redlock but redis website says its best to use redlock with atleast 3 redis instances.
For your use case, I dont think there is way to totally exclude read conflicts, but you can reduce the time window where data is inconsistent by doing the read/write on counters before doing the actual process, you could use a redis stream to be sure that every request is processed, processed only once and reduce the time window for inconsistencies.
It could work this way :
INCR
the key, it returns the new value of the counterXACK
to the redis stream to ensure the event has been processedEdit:
Reading again this way of doing it appeared to me that the essential part is to use the
INCR
as a way to get the new value. It ensures that every request gets treated with a different counter. The redis stream is nice and all but unnecessary if you want to do it easily.