Synchronise redis read and write with multiple threads in my node application

873 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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 :

  1. An email request comes
  2. The worker INCR the key, it returns the new value of the counter
  3. If the counter is too large, do your stuff
  4. Else the worker pushes the event and its counter on a redis stream
  5. The redis stream distributes (fan out) events to workers via a consumer group
  6. A worker (maybe the same maybe another) is handed the event and has to process it
  7. Once it is processed its tell XACK to the redis stream to ensure the event has been processed

Edit:

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.