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

Clear Rails cache when upgrading Consul Democracy #5547

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

javierm
Copy link
Member

@javierm javierm commented May 18, 2024

References

Background

We use caching in our application in two different ways:

  1. Rails.cache.fetch in models/controllers/libraries
  2. Fragment caching in the views

When using Rails.cache.fetch, we usually set an expiration date or provide a precise way to invalidate it. If the code changes and the information stored in the cache is different from what the new code would return, it's usually not a big deal because the cache will expire in an hour or a day. Until commit a4461a1, the statistics were an exception to this rule, but that's no longer the case. The actual exception to this rule are the i18n translations, but the code caching them is very simple and hasn't changed for more than three years (when it was written for the first time).

When using fragment caching, on the other hand, Rails automatically invalidates the cache when the associated view code changes. That is, if a view contains cache, the view renders a partial, and the partial changes, the cache is correctly invalidated. The cache isn't invalidated when the code in helpers, models or controllers change, though, which the Rails team considers a compromise solution.

However, we've been moving view partials (and even views) to components, and the cache isn't invalidated when a component changes (it doesn't matter whether the component Ruby file or the component ERB file changes). That means that the cache will keep rendering the HTML generated by the old code.

Objectives

  • Avoid possible cache-related issues when upgrading to a new version of Consul Democracy

Notes

I was thinking of adding it to a Capistrano task, but that would mean that every time people deploy new code, even if it's a hotfix that doesn't affect the cache at all, the cache would be cleared, which could be inconvenient. Doing it in a release, that usually changes thousands of lines of code, sounds like a good compromise.

@javierm javierm added the Devops label May 18, 2024
@javierm javierm self-assigned this May 18, 2024
@javierm javierm added this to Reviewing in Consul Democracy May 18, 2024
@javierm javierm force-pushed the empty_cache_when_upgrading branch 2 times, most recently from 2514132 to e9be45e Compare May 18, 2024 18:08
We use caching in our application in two different ways:

1. Rails.cache.fetch in models/controllers/libraries
2. Fragment caching in the views

When using Rails.cache.fetch, we usually set an expiration date or
provide a precise way to invalidate it. If the code changes and the
information stored in the cache is different from what the new code
would return, it's usually not a big deal because the cache will expire
in an hour or a day. Until commit <insert_commit_here>, the statistics
were an exception to this rule, but that's no longer the case. The
actual exception to this rule are the i18n translations, but the code
caching them is very simple and hasn't changed for more than three years
(when it was written for the first time).

When using fragment caching, on the other hand, Rails automatically
invalidates the cache when the associated _view code_ changes. That is,
if a view contains cache, the view renders a partial, and the partial
changes, the cache is correctly invalidated. The cache isn't invalidated
when the code in helpers, models or controllers change, though, which
the Rails team considers a compromise solution.

However, we've been moving view partials (and even views) to components,
and the cache isn't invalidated when a component changes (it doesn't
matter whether the component Ruby file or the component ERB file
changes). That means that the cache will keep rendering the HTML
generated by the old code.

So, now, we're clearing the cache when upgrading to a new version of
Consul Democracy, as part of the release tasks. That way, institutions
upgrading to a new version don't have to worry about possible issues
with the cache due to the new code not being executed.

I was thinking of adding it to a Capistrano task, but that would mean
that every time people deploy new code, even if it's a hotfix that
doesn't affect the cache at all, the cache would be cleared, which could
be inconvenient. Doing it in a release, that usually changes thousands
of lines of code, sounds like a good compromise.
@javierm javierm force-pushed the empty_cache_when_upgrading branch from e9be45e to bd56eb9 Compare May 20, 2024 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Consul Democracy
  
Reviewing
Development

Successfully merging this pull request may close these issues.

None yet

1 participant