Caching is used in hardware and software to shorten operational response times for data with a high usage frequency. CPU’s implement caching through the use of smaller and faster memory on or close to the processing unit where data can be retrieved and processed more quickly than in main memory. For persistent storage like hard drives or solid state drives caching can be implemented for storage IO but there will be differences in the caching method depending on if the storage IO is a read or a write. The caching method is known as a policy.
The types of write caching policies for storage are :
- Write-Through – Writes are done simultaneously to both the cache and the backing storage. This method allows for the fast retrieval of recently written data on demand and ensures that nothing is lost in the event of a system disruption.
- Write-Back – Writes are only initially directed to the cache. Writes to the backing storage medium are delayed until the modified content is about to be replaced by new writes. This method focuses on increasing the overall performance of the write IO as the cache is typically faster than the backing storage.
The types are read caching policies for storage are :
- Read-Ahead – The storage controller will read an entire stripe (groups of data blocks written to storage to take advantage of multiple drives) and then retain it in the cache. Each read operation is more intensive on the backing storage medium but if the read requests are sequential then the amount of storage IO can be reduced and performance will increase. This method is highly advantageous for rotational media based systems.Read ahead can be implemented if one of three ways: no read ahead(only the requested block is read), always read ahead (always read the full stripe) and adaptive read ahead (the storage system automatically adjusts the read policy based on the access pattern).
- Read-Through – All read operations are directed to the cache. If the cache does not contain the requested data it must first be read from the persistent storage into the cache before being used. If the data is already in the cache then the backing storage is not required and performance will increase.
- Cache-Aside – Requests for data are sent to the cache first. If the data is found in the cache it is considered a cache hit and responds, if it is not found in the cache then the request is redirected to the backing storage and is then considered a cache miss. Oftentimes a cache miss will cause the data to be populated into the cache.
As a cache is typically smaller than the total data a system will hold it needs to implement a way in which additional space is made available for new data to be cached, this is known as a cache replacement policy.There are a number of cache replacement policies :
- First-In-First-Out – The cache evicts data in the order it is added.
- Last-In-First-Out/First-In-Last-Out – The cache evicts data which was most recently added.
- Least-Recently-Used – The cache evicts data which has been used the most infrequently. There are many variants of this policy.
- Most-Recently-Used – The cache first evicts data which has been used most recently.