5 ms·
When to cache in your Rails app
- andrenotgiant 5y agoI'm surprised about: >4. Database queries can probably be left uncached ...but maybe I am thinking about it wrong. Imagine building Quora as a Rails App. For all the interesting user stats like "your answers have been viewed 28,000 times" I feel like your choice is to either denormalize in the application logic and database or cache the complex queries. Are most people taking the first approach?
- ch4s3 5y agoThe DB should be caching some things, and you'd index which is basically a cache.
- Gigachad 5y agoI don't think you want to be storing individual view events in a relational database. You are looking at billions of events. Of course running a count(*) every time you want to show the view count would be absurd. But I imagine how it would work in practice is you store all events in something like elasticsearch and then every x minutes you recalculate the number of views if there were any new events and store that calculated number somewhere. What I would _not_ be doing is something like the active record cache_key to calculate if any changes have occurred to decide if you grab from redis or run the query again. For something like view count, it doesn't matter if the data is slightly stale.
- taf2 5y agoabc - always be caching. Seriously if it’s in the db use an index - it’s a cache. If you can’t get an index - background worker into a cache
- Gigachad 5y agoCaching is a significant source of bugs and complexity. Often really bad bugs too, like serving one users data to another user. Only be caching when there is a decent performance gain to be had.
- forgingahead 5y agoI think the author had some good points about when to use caching - it's unlikely for the vast majority of readers on this forum who would need a speed up benefit from 10ms to 5ms just because of caching, precisely because of the complexity cache management involves. There's that old joke: What are the two hardest things in computer science? Cache expiry, and naming things.