In computing, cache is a widely used method for storing information so that it can be later accessed much more quickly. According to Cambridge Dictionary, the cache definition is,
An area or type of computer memory in which information that is often in use can be stored temporarily and got to especially quickly.
This definition, although technically correctly, is slightly lacking given that caching can be applied to a variety of scenarios and it is not just related to computer memory. There are many forms of caching employed in various use-cases. All caching however, aims to achieve the same goal of improving the accessibility time of the particular asset or piece of data you are requesting. This guide should help give you a better understanding of the cache definition and how exactly caching works.
What is Cache and How Does It Work?
The section above gave a broad definition of what is cache. However, to fully understand the cache definition, we need to understand how it works. When a caching mechanism is in place, it helps improve delivery speed by storing a copy of the asset you requested and later accessing the cached copy instead of the original. To better outline the full process, the following example can be used to better explain how caching works:
- A web page request is made for an asset (e.g. style.css) from the origin server.
- The cache is checked first to see if the asset already exists (in the case of a webpage, the cache could pertain to the browser cache, CDN, web proxy, etc).
- If there is not a copy of the requested file (style.css) stored in cache, this will result in a cache miss and the file will be retrieved from its original source.
- Otherwise, if the file is stored in cache, this results in a cache hit response and the asset is delivered from cache.
- Once the file is cached, it will continue to be delivered from cache until it expires or the cache is cleared / purged.
That is the basic process of how cache works. The above example is in reference to caching a web resource, however, the same process is more or less used for all types of caching mechanisms.
Types of Caching
There are various types of caching mechanisms that exist in computing to help speed up different processes. The following section will outline a few popular types of cache and what they are used for.
A cache server is a dedicated server used for caching web resources. This type of cache mechanism is used in content delivery networks or web proxies. These servers (called edge servers in CDN terms) can be located in many geographic regions and used to store and deliver data so that the user’s request / response does not need to travel as far.
Web browsers store files in their local cache so that they can be accessed faster as they don’t need to be downloaded from a server. Leveraging browser cache is an important optimization tactic to use that’s also easy to implement on your origin server.
Memory cache is a little different than the two previously mentioned types of cache. This caching mechanism is used in computers to help speed up the delivery of data within an application. The memory cache stores certain parts of data in Static RAM (SRAM) as it is faster to access files using this method rather than accessing them via the hard drive. An example of this could be a music recording program loading certain audio files from the hard drive into SRAM so that they can be quickly accessed.
Disk cache is very similar to memory cache in that is also stores data so that it can be accessed faster when using an application. However, instead of using SRAM, disk cache makes use of conventional RAM. Disk cache stores data that has been recently read as well as adjacent blocks of data that are likely to be accessed soon.
Difference Between Cache Definition and Buffer Definition
Although both cache and buffers do share certain similarities, there are a couple key differences that set them apart. According to Wikipedia, the definition of a buffer is
A temporary memory location that is traditionally used because CPU instructions cannot directly address data stored in peripheral devices.
Buffers are meant to store file metadata (e.g. what’s in certain directions, user permissions, etc) while a cache only contains the content of the file itself. As mentioned in the definition, buffers are able to serve as an intermediary for devices to communicate between one another that are incapable of direct transfer. Therefore both buffers and cache are meant to store particular data, however, it is the type of data stored as well as the purpose of storing such data that differs.
Cache Definition and Explanation – In Summary
Now that we’ve shed some light on the cache definition as well as explained what is cache exactly and how it is used, hopefully you now have a better idea of just how useful caching is. In today’s technological environment speed is a very important factor. Being able to access data quickly is vital. Although different forms of caching exist, they all reach for the same goal – to store a copy of data somewhere that is more readily available upon subsequent requests.