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

Avoid RedisCacheStore#increment on Rails 6+ #597

Merged
merged 1 commit into from
Nov 20, 2023

Conversation

jdelStrother
Copy link
Contributor

@jdelStrother jdelStrother commented Nov 21, 2022

As far as I can tell, Rails has handled increment+expires_in since
6.0 (rails/rails@9d5b02e), and more recent versions add pipeline optimizations on top of that.

How about avoiding our own custom RedisCacheStoreProxy#increment method on modern versions of ActiveSupport?

This also avoids an extra GET (from the if options[:expires_in] && !read(name) call).

@jdelStrother
Copy link
Contributor Author

Let me know if there's interest in merging this & I'll rebase.

@mitchellhenke
Copy link
Contributor

mitchellhenke commented Oct 11, 2023

I'd really like to see it land.

In some brief local application-level benchmarking, the patch is quite a bit faster. In simpler applications or situations where Redis latency is worse, I suspect the impact would be even larger. Using derailed_benchmarks:

❤️ ❤️ ❤️  (Statistically Significant) ❤️ ❤️ ❤️

(0.2698 seconds) "rack attack update"
  FASTER 🚀🚀🚀 by:
    1.0643x [older/newer]
    6.0431% [(older - newer) / older * 100]
(0.2872 seconds) "main"

Iterations per sample: 200
Samples: 200

Test type: Kolmogorov Smirnov
Confidence level: 99.0 %
Is significant? (max > critical): true
D critical: 0.15174271293851463
D max: 0.66

@grzuy
Copy link
Collaborator

grzuy commented Oct 12, 2023

Let me know if there's interest in merging this & I'll rebase.

Yes, please.

Thank you.

@jdelStrother
Copy link
Contributor Author

jdelStrother commented Oct 12, 2023

It looks like there's test failures with redis-activesupport against ActiveSupport 7.1.1 - it tries to use Integer#present? and Hash#symbolize_keys without requiring them from ActiveSupport.

I've fixed it by adding the requires to spec/acceptance/stores/active_support_redis_store_spec.rb for now, though could see an argument for adding them to lib/rack/attack/store_proxy/active_support_redis_store_proxy.rb.
(Ideally it would be fixed in the redis-activesupport gem, but since it's in Archive status, that seems unlikely)

Copy link
Collaborator

@grzuy grzuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

Thank you!

Pending conflicts with latest changes in main.

@jdelStrother
Copy link
Contributor Author

I'm away for a few weeks, but can pick this up again in November.

@santib
Copy link
Collaborator

santib commented Oct 19, 2023

Hi, I think this is a nice improvement 👏

I was checking the PR and noticed that there are no tests for ActiveSupport < 6, so went ahead and added:

# gemfiles/active_support_5_redis_cache_store.gemfile

source "https://rubygems.org"

gem "activesupport", "~> 5.2.0"
gem "redis", "~> 5.0"

gemspec path: "../"

When running the tests BUNDLE_GEMFILE=gemfiles/active_support_5_redis_cache_store.gemfile bundle exec rake test I got a couple of failures. After some debugging seems like defined?(ActiveSupport) isn't behaving well, and instead we need to do if defined?(::ActiveSupport).

EDIT:
The gemfile for ActiveSupport 5 was merged. When updating this branch with main the CI should fail because of the above

@mitchellhenke
Copy link
Contributor

I'm very interested in seeing this merged, is there anything I can do to help move it forward?

@santib
Copy link
Collaborator

santib commented Nov 20, 2023

Hi @mitchellhenke, I think we are waiting for @jdelStrother to fix the CI and that should be it 👌

He said he was out til November, so he'll probably be coming back soon.

@jdelStrother
Copy link
Contributor Author

Yep, let me try & take a look at it today

Rails has handled increment+expires_in since
6.0 (rails/rails@9d5b02e),
and more recent versions add pipeline optimizations on top of that.
@jdelStrother
Copy link
Contributor Author

Rebased and fixed the defined?(ActiveSupport) check, though Github Actions seems to be having a bad day - could you re-run the failed ones?

@santib santib merged commit d9fedfa into rack:main Nov 20, 2023
102 checks passed
@santib
Copy link
Collaborator

santib commented Nov 20, 2023

Thanks for your contribution @jdelStrother 🙌

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

Successfully merging this pull request may close these issues.

4 participants