Conceptualizing caching
Learning objectives
- You know the term caching and can outline benefits and downsides of caching.
Caching refers to storing data in a temporary location that provides faster access to the data than the original data location. It is used to increase the efficiency of retrieving data that has already been retrieved once. As an example, an image on a web page could be stored in the browser (on the client) and read from there on subsequent web page accesses. Similarly, data requested from a database could be stored on the web server, and read from there on subsequent requests.
There are multiple benefits to the use of caching in web applications, including (1) improved performance -- caching can speed up the retrieval of the data, resulting in a faster web application; (2) reduced server load -- caching can reduce the number of requests made to the server, reducing the load on the server; and (3) increased scalability -- through reducing server load, caching can help increase the scalability of a web application. In addition, caching can be used to enable offline access to data, and depending on the cost model of the hosting solution, caching may also lead to reduced hosting costs.
There are, however, also downsides to caching.
"There are only two hard problems in Computer Science: cache invalidation and naming things." -- Phil Karlton (see also TwoHardThings)
Downsides to caching include it being hard to implement properly. If cache invalidation is not properly implemented, the use of caching can lead to stale data, meaning that the data is no longer accurate or up-to-date. Caching also adds (some) complexity to the application, and may require introducing additional services. The use of caching also leads to an increased need of resources, as the cached data needs to be stored. Finally, if not properly secured, caching of data may also lead to security risks.
Caching can be implemented in multiple locations, including the browser, the server, and the application, proxy servers, load balancers, and so on. We'll look into client-side caching and server-side caching next.