Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate replacing mutex with a redis transaction #2

Open
ryakh opened this issue Nov 28, 2019 · 0 comments
Open

Investigate replacing mutex with a redis transaction #2

ryakh opened this issue Nov 28, 2019 · 0 comments

Comments

@ryakh
Copy link
Collaborator

ryakh commented Nov 28, 2019

Per @ankopainting suggestions in #1:

The second thing i just wanted to comment on (sorry for the nitpick), is the mutex is unnecessary. Instead you could use a redis transaction. Here is what I'm doing;

    ret = redis.pipelined do
      redis.zremrangebyscore full_key, 0, clear_before_timestamp # remove requests past period
      redis.zcard full_key # count of how many requests we've received
      redis.zadd full_key, current_timestamp, SecureRandom.uuid
      redis.expire full_key, period
    end

The redis docs say;
All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single isolated operation.

https://redis.io/topics/transactions

Need to investigate how viable this option is and if mutex can be replaced with a transaction it could clean up code a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant