Caching is one of the most famous buzz words among developers, engineers and architects when it comes to scalability and performance of applications. It sounds magically and mystically and helps us techies to put MBAs and wannabes into their bounds of IT skills. However, as often the case in information technology (but also in other disciplines such as economics, physics or maths) experts tend to obfuscate pretty simple things in a way that noboby else understands them anymore.
But … here is the plain truth:
Caching is just about locating something as close as possible to its consumer.
Let’s consider the following example: At my home, beer can be stored in different places. Each place is harder for me to reach. Starting with the liquor store around the block (far way), followed by the basement (not very near) to the fridge in my kitchen (near). If I want to grab a beer with least efforts (way to walk and time it takes to do so), I may want to make sure that I always locate some beer in my fridge. I need to cache the beer. As beer is a consumer item I will also need to make sure that the cache is re-filled continuously.
Why is this good? First of all, if I have some beer cached, I can get it very fast if I want to. I do not need to do very much for it. Much more, I am also quickly available to handle some other longings (”Give me pizza!” says the brain) as I am not locked with the task of getting a beer for so long.
That’s it. If you take the tale from above and put it on information technology, it seems to be very obvious, that this method can also help us to speed up (web-) applications. As you may know, it takes longer to read data from a harddisk instead of reading it out of the RAM. Also it is faster to read something from a harddisk instead of reading it from a network host somewhere in the distance (especially if this host itself needs to read the data from its own local harddisk).
Therefore, it might be a good idea to put certain data into a place nearby where it can be accessed fast and easy. Especially in web applications you should always remember, that we just talk about single documents that are rendered (”shown”) in a user agent (”browser”) on request. If you have a document to show to people you should always first identifiy if the page contains any data that is meant only to a specific requestor. If not, try to stop generating that document again and again with each single request and instead pre-generate it on a regular basis (each minute or so) while caching the result (static page) for serving . Sure, caching can also make sense for just a specific user, but if you can pre-generate documents for all users of your site at once, the effect is much more impressive.
Imagine that there is a “virtual requestor” inside of your application requesting a specific URL again and again and making that generated (and now static) document available to public. If you have a lot of traffic, this approach can help you to save thousands of expensive network- and database connections.
As you can see, caching itself is nothing really cool. However, electrons are also not very interesting by default, but when pushing them through a wire, they can turn on the light.