DEV Community

Jess Lee
Jess Lee Subscriber

Posted on

DEV Notes: Don't Forget To Clear Cache!

This post is for anyone who wants to contribute to the DEV codebase, and needs some notes on how caching might affect the features/bugs they're working on.

We don't have much info about caching in our docs (yet), except that fastly is listed as a key service we rely on for edge caching. That little note theoretically gives enough info for an experienced rails dev to know what to consider and how to navigate caching on DEV, but likely leaves juniors totally lost.

If you're a bootcamp grad like me, caching might not have popped up in your curriculum at all -- that's because caching isn't really something you need to think about until your app is serving a lot of traffic. So if you're not familiar with the general concept of caching, please read this awesome article by Kevin Kononenko first:

Great.

On DEV, we rely heavily on caching through our content delivery network, Fastly. This means your dev.to requests will hit the closest proxy server to your location (a server provided by the CDN that stores the files for our most common requests) instead of traveling all the way to the origin. If your request does need to hit the origin, we also rely on out-of-the-box caching features provided by Rails that also stores static, ready-to-go info. This means the browser, CDN, and Rails are all working together to fulfill a request before it goes directly to the underlying code for fresh assets.

Browser Cache > CDN > Rails Cache > Underlying Code

This is all pretty conventional so the Fastly and Rails documentation are great resources for learning more.

If you're working on DEV, it's worthwhile to ask: "should this be cached? and if so, at what level?" Or the reverse, "should the cache be cleared?" We're going to focus on the latter for this article.

Recently, I was building the functionality to merge two users -- sometimes people login via twitter and github separately (instead of linking them), which creates two accounts. A pretty straightforward process:

  • Move all activity from Account B to Account A.
  • Delete Account B.

After doing the above, one can reasonably assume that when I visit Account A's profile, I would see all of the user's consolidated activity. Nope! I had a momentary panic thinking all of Account B's data was deleted. Turns out I didn't signal, in my code, to clear the Fastly cache so the request didn't receive fresh data. Yes, user profiles (/username) are all cached which means we need to clear the cache every time something on that page changes.

We have a cache busting object that makes it easy to purge our Fastly cache, along with a few handy methods for items that need to be purged regularly. This is the file: app/labor/cache_buster.rb that handles all the logic. Here's how you would clear cache on my user profile: CacheBuster.new.bust("/jess").

When I cleared Account A's user profile path on Fastly, I expected to see all the consolidated activity. That was almost the case -- articles, badges, and comments appeared but one of the social icons was missing (Account B's original login method to be specific). Turns out we utilize low level Rails caching for user data. I didn't run any 'save' callbacks that would have cleared the cache when I updated Account A's profile. In order to clear this Rails cache, I needed to formally update the user -- something like user.touch typically does it.

I don't think it's possible to run Fastly caching in development, but it is possible to run Rail's caching with a simple rake task by toggling bin/rails dev:cache.

One last thing: aside from all of this, we also use a gem called CounterCulture. This gem keeps track of association counts like articles_count on a user and it helps us query faster. Sometimes these counts go out of sync (like when you don't run proper callbacks when you update user activity...). If this happens to you, you can update the count directly. For example:



user.articles_count = user.articles.size


Enter fullscreen mode Exit fullscreen mode

This was a bit of a 'reverse' view of our cache policy but I hope it provided some insight into the DEV app.

Top comments (5)

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Interestingly, I have to clear my cache to see most updates on DEV, via Ctrl+F5. Sometimes, my comments and chat posts will even get eaten if I don't hard-refresh first! Honestly, I've just gotten used to it.

Collapse
 
_bigblind profile image
Frederik πŸ‘¨β€πŸ’»βž‘οΈπŸŒ Creemers

Great article!

Very clear explanation. Hope this helps out many (future) contributors.

Collapse
 
jess profile image
Jess Lee

Thanks Frederik :)

Collapse
 
tolambiasapna profile image
Sapna T

Great piece of information.
Thanks Jess Lee

Some comments may only be visible to logged-in visitors. Sign in to view all comments.