javamultithreadingredis

Persistent Atomic Counter in multi-threaded environment


I want to write a data in the file with first field at incremental counter which should start from 1 after reaching 25k. Multiple threads are writing in the file concurrently.

I need to persist this counter because if process get restarted then it counter should start from last count.

My thinking : I am thinking to use INCR in Redis. It will increment the value and give incremental number. In multi-threaded env also since redis is single threaded so this will be thread safe. I hope there wont be a case when any number shall be skipped or give duplicate value to thread. If we use this approach then how to reset this key when value reaches to 25k in a thread safe way. Please suggest.

If there is any other approach, please suggest that as well.


Solution

  • You can use Lua scripting to achieve the goal:

    -- counter.lua
    
    local cnt = redis.call('incr', 'counter')
    if cnt == 25000 then
        redis.call('set', 'counter', '0')
    end
    return cnt
    

    Run the script with Redis cli: redis-cli --eval counter.lua ,