-
Notifications
You must be signed in to change notification settings - Fork 34
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
Fragment cache somtimes returns null despite cached data loading initially #72
Comments
Hi @RudiBoshoff! That sounds really strange, because gem does reading in a dead simple way: FragmentCache.cache_store.read(cache_key).tap do |cached|
return NIL_IN_CACHE if cached.nil? && FragmentCache.cache_store.exist?(cache_key)
end In this case Reading happens here, looks like it also does nothing more that a |
Closing this for now, feel free to reopen 🙂 |
FWIW: we're seeing similar behavior. Isn't there a race condition.
Thoughts? @DmitryTsepelev |
We've been doing some testing in production with the following
It seems to have addressed the race condition @RudiBoshoff @DmitryTsepelev I think we should reopen this |
@gsdean |
The workaround, isn't perfect. There is still a chance a race condition could cause a misread from the cache (specifically when an eviction occurs after the initial read, and then the cache is repopulated). Basically it reduces the risk of For what it's worth we've tested both with and without this patch in a high load/concurrency production environment and without this patch we see numerous intermittent errors where nil is returned incorrectly. With the patch we have yet to have an error. |
Sorry guys I don't know how but github does not sends me events from some projects. I'll take a look tomorrow morning |
If I understand correctly, the problem is that race condition makes cache to return I guess we could add it to the docs and call it a day, what do you think? |
The cache returns the wrong value not an outdated value. I do not believe documentation alone is sufficient. In a moderately concurrent deployment, this is a serious issue. |
in my situation, the problem seems that race condition makes cache to return nil even though it has never been nil before. In reality, there are only two possibilities: the cache exists and is filled with values, or the cache does not exist. However, it seems to behave as if the cache exists and the value is nil. |
The only way to synchronize threads here is using some kind of lock: we could make it configurable and let users to opt–in. It will fix the issue completely but will slow down things a bit. WDYT? |
Also, sounds like we could change the behavior to keep something different rather than |
My vote would be to either
OR
I would actively discourage locks. As you've said they would be slow, but also very hard to manage in a multi-server deployment such as the one we have in production (load balancer in front of multiple web servers). |
I'm not familiar with the cache API or instrumentation API, but it seems that the https://github.com/rails/rails/blob/v7.0.4.2/activesupport/lib/active_support/cache.rb#L379 |
@tsugitta I agree, this is the most correct fix, but it seemed like a big change 🤷 |
Big change is not an issue, we can make a major release. The problem I'm trying to solve is that Do you want to create the PR for the workaround? |
Yes, I'm also trying to solve the same problem. As I commented above, Aside from that, the workaround seems great. I think it would be nice if I could submit a PR, but I haven't figured out the structure of the library yet, so I don't know where to start now and it may take some time. So at least I don't think I am the right person. |
I had overlooked that the version I was using was outdated. After upgrading this library from |
Interesting 🤔I didn't remember we addressed it here in the gem, maybe something in Rails had changed for better |
As far as I can tell this is still an issue. Any thoughts on the PR? It would be really great to either merge that or leave this open until it’s resolved. |
Hi there,
ENV:
We are currently using your gem on our Rails api which passes data through to an Apollo Angular app. We originally had a pure rails solution but got better performance results with your gem.
Issue:
The problem is that sometimes the cache returns null. This doesn't happen locally but happens when we use the cache on Redis for staging and production. The cached data is returned most of the time but sometimes returns null. We have investigated as much as we could: we first thought it might be the front end but we have narrowed it down to the cache returning null.
How to reproduce:
This only works some of the time
Question:
Do you have any ideas on what might be causing this or what we might be able to do? Thank you in advance.
The text was updated successfully, but these errors were encountered: