madelson / DistributedLock

A .NET library for distributed synchronization
MIT License
1.74k stars 182 forks source link

Concurrency with Redis Semaphore #172

Closed leorg99 closed 9 months ago

leorg99 commented 9 months ago

Hi - I was wondering how concurrency works within the same process when multiple threads try to acquire a semaphore with redis? It looks like only one thread at a time can use the redis connection unless you setup some kind of connection pool? But then looking at the source, it seems like the connection is only used briefly and if acquiring fails (max count hit on the semaphore) then it just waits a little bit before trying again?

Thanks!

madelson commented 9 months ago

Hi @LeorGreenberger ,

When multiple threads in the same process try to acquire any of the distributed semaphore implementations (including Redis), it behaves just as if two threads from different processes tried to acquire.

You are correct in thinking that many threads in the same process can share the same underlying connection to Redis and that holding on to the semaphore between acquisition and release does not block others from using that connection.

Because of the way Redis locks work, if acquisition fails and there is no timeout (or the timeout isn't met), it will enter a wait-retry loop.

Does that answer your question?

leorg99 commented 9 months ago

It does! Thank you very much!