loading words...

Jan 11, 2019 09:01:04

Rails caching

by @swizecteller | 207 words | 🐣 | 116💌

Swizec Teller

Current day streak: 0🐣
Total posts: 116💌
Total words: 32303 (129 pages 📄)

Today I learned something weird about Rails caching.

Yes, it isn't JavaScript, but I been doing a lot of backend lately. Nobody else wants to deal with that so I gotta ...

Juniorer team members get to have JavaScript fun. Don't want them too grizzled too soon. ✌️✌️



I was looking at a feature I re-designed a while ago. Rewrote one of our core algorithms from scratch so it's more readable, easier to extend, and faster. Or so I thought.



Turns out not only is my algorithm slower, it gets linearly slower the more users it sees online. 🤔

So I looked at profiling info on New Relic ...


... my algorithm runs 285 database queries to match a pool of 10 users with a pool of 5 or so chatrooms.

53 of them are just for the Settings object, which changes maybe 3 times per month. This algorithm runs every 30 seconds.



Doesn't Rails have SQL cache? Isn't it supposed to automagically avoid running the same query multiple times!?

Rails does. BUT


But while Rails does have SQL cache, that cache only lasts for 1 action. Basically it only caches the same query run multiple times in the same function.


For anything more than not-actually-cache, you have to use Rails.cache

Down to 232 queries ✌️

Originally published at twitter.com

contact: email - twitter / Terms / Privacy