Since caching is one of the two hard things in Computer Science (along with naming things and off-by-one errors), and I’ve been dealing with tons of caching issues of late, I here offer my personal rules of caching:
- Caching should be done as little as possible.
- You should not cache until you know you need to.
- When possible, caching should be done by dedicated applications (Varnish, Akamai, et cetera) outside of the application.
- When possible, HTTP APIs should set sane cache headers to help aforementioned dedicated applications.
- Try to keep cache lifetimes as short as your applications can handle. If your data rarely changes, try an ultra-long-lived cache, keeping in mind the below rules.
- Ultra-long-lived caches which require forced expiration on change should only be used inside the application which generates the changes.
- Never depend on polling to tell you when to expire a cache.
- Always turn off caching in your test suite except when testing your cache.
- When consuming slow data you do not control, you may cache the response in your application. However, you should not cache the processed result, only the source, and as early as possible.
- You should not cache intermediate steps in your application’s program flow.
This will be updated as I bang my head against more issues.