kenn / redis-mutex

Distributed mutex using Redis
https://rubygems.org/gems/redis-mutex
MIT License
282 stars 44 forks source link

with_lock(user) #17

Closed acegilz closed 9 years ago

acegilz commented 9 years ago

If i have 2 separated lock block instances with same user as locking param, will it be able to run simultaneously without problem or will it cause "couldn't lock record" error? I know it will cause lock error if it tries simultaneously on the same block with same params, but does it also applies if run on 2 different places ( example: one from controller x and other on controller z)?

def work_1(user, params)
    RedisMutex.with_lock(user) do
      Market.create(params)
   end
end
def work_2(user, params)
    RedisMutex.with_lock(user) do
      Balance.create(params)
   end
end

On this example if work_1 is already running, what will happen if work_2 is called with lock on same user? Will return lock error right? Or because it is another block that still has no lock on that user it will proceed in parallel?

kenn commented 9 years ago

The tagline of redis-mutex is "distributed mutex using Redis" - a lock is a singularity no matter what, between competing controllers / processes / servers, as long as the lock is kept as a Redis key. So, yes.

acegilz commented 9 years ago

yes, it will lock on paralel processing; or yes, parallel will be possible without lock error?

kenn commented 9 years ago

If you don't want to mutex work_1 and work_2 and want to allow them to run in parallel, then you should give them different keys. Like, RedisMutex.with_lock("work_1:#{user.id}") and RedisMutex.with_lock("work_2:#{user.id}").

acegilz commented 9 years ago

I want them not to work in parallel. So I assune its fine as it is :)

acegilz commented 9 years ago

thanks for response!