redis-py icon indicating copy to clipboard operation
redis-py copied to clipboard

Suggestion: Read/write lock

Open matthew-mcallister opened this issue 3 years ago • 6 comments

This might be too far out of scope for the project, but a reader/writer lock implementation would be a really nice addition to redis.lock. I haven't come across a proven implementation during my brief research unfortunately. An MVP probably should have the option to allow writers to block readers.

matthew-mcallister avatar Sep 07 '22 18:09 matthew-mcallister

Thanks @matthew-mcallister for the idea. Let's see if we can noodle this one out. Do you have any interest in contributing to this - perhaps with a PR?

chayim avatar Sep 15 '22 10:09 chayim

I wouldn't mind looking into it and opening a PR. I'll have to start with some more research. Eventually someone will likely rely on such a feature in production, and since I'm no expert in this area, it will presumably need extra scrutiny before release.

matthew-mcallister avatar Sep 16 '22 17:09 matthew-mcallister

Is there any update on this?

SuperCipher avatar Mar 22 '23 16:03 SuperCipher

@SuperCipher I just put it on my "projects" list to do down the line when I have some time and I'm not working on something else, since it's not really a priority for me or my job. But if other people are interested in this, I could stop putting it off.

matthew-mcallister avatar Mar 22 '23 23:03 matthew-mcallister

This would be useful for us too. I think one approach could be to use keyspace notifications.

Reader:

  1. Check if lock is acquired (i.e. key exists).
  2. If acquired, subscribe to DEL events for the lock.
  3. Wait for the DEL event, then unsubscribe and continue.

Writer can just acquire the lock as usual.

If it's desired for the writer to wait on currently running readers, then maybe adding a counter to readers, then the writer can just acquire the write lock, then wait for the counter to reach 0 and proceed.

Maybe the keyspace notifications could also optimise the normal acquiring of the lock (instead of frequent polling as it currently does)?

Dreamsorcerer avatar Aug 18 '23 16:08 Dreamsorcerer

Thanks @matthew-mcallister for the idea. Let's see if we can noodle this one out. Do you have any interest in contributing to this - perhaps with a PR?

I do have, what do you recommend before starting?

LeOndaz avatar Jul 04 '24 05:07 LeOndaz