Categories
General

What is a Cache?

In computer lingo, the concept of cache refers to storage where data is saved for quick retrieval later when the same request is made again. The idea behind caching is to trade storage space for computing time because computing is slower and more expensive in terms of resources and power.

It is generally possible to rapidly retrieve the cached data in constant time, no matter how complicated and long it took to create the data.

As an example, a Multiplication Table Chart is a form of caching (for humans) and could be faster than having someone compute multiplications in their head. Looking up the result in a table can be done in a predictable time, no matter how complex the multiplication is.

Caches and Caching are pervasive in computer systems, and the types of storage greatly vary depending on the context (RAM, Disk, local, remote, shared). What’s important is that caching should be much faster than re-computing data.

Caching is trading storage space for computing time.

Cached items are meant to stay in the cache only temporarily, and they typically have an expiration date called TTL (time to live). If the storage becomes full, cache items could get evicted (deleted) at any time to free up space for newer items. The app should be able to function without any cache, although slower.

If a cached item is successfully retrieved during a request, we say it is a “cache hit”. If the item was not found, it’s a “cache miss,” and the data will be recomputed and stored in the cache for later use.

A stale (expired) version of the item may be found in the cache. Serving the stale content right away and recomputing new data in the background is best for performance. If the most up-to-date content is required, the requester will have to wait longer.

In the context of websites, and particularly WordPress-based websites, caching can happen at several layers inside or outside the server. Page Caching specifically refers to caching the HTML code of a webpage.

Why caches are great

Performance. That’s really the primary benefit and raison d’etre of caches. They often increase performance by orders of magnitude and make computing extremely efficient, freeing precious computing assets for better tasks.

Failover. When a system fails or is temporarily overwhelmed, a cache might serve content even though the content’s origin is no longer available. In turn, this avoids or mitigates cascade failures across the data flow. A great example is Cloudflare’s Always Online feature. A local server cache might do the same if PHP or MySQL was down, for example.

Server costs reduction: since caching reduces the amount of computing required, you might get away with a less expensive server and save money.

Power efficiency. For web apps and websites, caches save a lot of power by storing content locally and thus avoid battery-intensive wireless communications on laptops and mobiles. In fact, if browsers didn’t cache content, your battery would die 10X faster.

When caches are naughty

Stale content. All caches need a reliable purging mechanism that will clear all relevant entries every time the source data is updated. Unfortunately, there are many cases where caches are not properly cleared, and having multiple caches create conflicts that can be difficult to diagnose.

Users are often baffled at why websites and apps do not display the latest information, and as a last resort, a “clear all caches” is the unelegant but ultimate way to solve the problem. Often, this might cause a spike in computing usage, but that’s the price to pay.

Cache data can also become corrupted if there was a connectivity loss or a problem with the storage medium. Although rare, it can happen and could yield some weird results as well. Typically, checking on another device or clearing the cache would solve the problem.

Cache examples in everyday’s computing

Web caches

Everybody knows of browser caching, and it’s probably one of the busiest forms of caching in the world. Thanks to browser caching, assets are loaded only once and change very infrequently. From there, only dynamic content is checked and updated, making browsing a pleasant and efficient process.

When faced with a website glitch, users often ask how to clear the browser’s data, including the cache, web history, etc.

Outside the browser, content delivery networks like Cloudflare, Bunny, KeyCDN, Stackpath and more, are specialized caches that are incredibly fast and efficient at delivering web assets like images, CSS, Javascript files, etc.

Hardware caches

Inside your CPUs, GPUs, and other microchips, there are several layers of caches in the form of ultra-fast memory blocks. The faster they are and the more expensive they get, but since CPU cores are many times faster than RAM, the caches allow these cores to run as fast as possible without being held back by memory.

App caches

App caches are very similar to web caches, but the type of data varies. For example, the operating system manages a great deal of file caching that is completely transparent and invisible to users. However, that cache plays a critical role in making the OS quicker to respond. This article from G2 shows a good overview of app caches.

People often ask how to clear app caches on iPhone/iPad but on Android too.

For a broader definition and more detailed cache definition, Wikipedia is a good place to learn from. I like this Amazon cache overview as well

Leave a Reply

%d bloggers like this: