I would like to utilize caching on a couple pages:
- individual blog pages
- index.php
However, after devising my system, I realized that the headers of my pages change according to state very slightly (aka, it says something like “Hello, johnnie@example.com” if you are signed in). I therefore want to make sure that it doesn’t accidentally display the wrong username or email up top.
It’s a shame that such a small thing that I would like to NOT be cached on a page should force me to cache nothing on my site at all.
How can I get around this?
You’ll want to think about WHY you want to do caching. Caching is an optimisation and as such, should only be done if necessary.
If you aren’t sure it’s necessary, go back and do some research to make sure. This will involve performance testing on production-grade hardware on your test lab.
Once you’re sure it is necessary, then you will know how much improvement you need. You can consider caching just PARTS of pages – this will be less efficient on the front-end and caching layer than caching complete pages, but may take more load off the back-end as you will get a higher cache hit rate.
Consider a page which has a personalised element (say, Hello Johnnie) and a part which is expensive to compute but changes relatively infrequently and is the same for all users – say some stock prices.
You can cache the stock prices part of the page for (say) 5 minutes and generate the personalised part of the page each time. That way you don’t hit your back end for stock prices and can still show the right page to the right people.
Most companies find that generating the front-end is quite computationally expensive (sticking HTML together is sadly rather time-consuming) BUT scales very well – this means that you can simply add more tin when it’s not fast enough. On the other hand, back-end servers can do a lot more work, but scale much less well – e.g. databases – you cannot simply add more servers because there are consistency / synchronisation problems to contend with which limit scalability.