1.

What is Caching? What are the various cache update strategies available in caching?

Answer»

Caching refers to the process of storing file copies in a temporary storage location called cache which helps in accessing data more quickly thereby reducing site latency. The cache can only store a limited amount of data. Due to this, it is important to determine cache update strategies that are best suited for the business requirements. Following are the various caching strategies available:

  • Cache-aside: In this strategy, our application is responsible to write and read data from the storage. Cache interaction with the storage is not direct. Here, the application looks for an entry in the cache, if the result is not found, then the entry is fetched from the database and is added to the cache for further use. Memcached is an example of using this update strategy.

Cache-aside strategy is ALSO known as lazy loading because only the requested entry will be cached thereby avoiding unnecessary caching of the data. Some of the disadvantages of this strategy are:

  • In cases of a cache miss, there would be a noticeable delay as it results in fetching data from the database and then caching it.
  • The chances of data being stale are more if it is updated in the database. This can be reduced by defining the time-to-live parameter which forces an update of the cache entry.
  • When a cache node fails, it will be replaced by a new, empty node which results in increased latency.
  • Write-through: In this strategy, the cache will be considered as the main data store by the system and the system reads and writes data into it. The cache then updates the database accordingly as shown in the database.
  • The system adds or updates the entry in the cache.
  • The cache synchronously writes entries to the database. This strategy is overall a slow operation because of the synchronous write operation. However, the SUBSEQUENT reads of the recently written data will be very FAST. This strategy also ensures that the cache is not stale. But, there are chances that the data written in the cache might never be read. This issue can be reduced by providing appropriate TTL.
  • Write-behind (write-back): In this strategy, the application does the following steps:
    • Add or update an entry in the cache
    • Write the entry into the data store asynchronously for improving the write performance. This is demonstrated in the image below:

The main disadvantage of this method is that there are chances of data loss if the cache goes down before the contents of the cache are written into the database.

  • Refresh-ahead: Using this strategy, we can CONFIGURE the cache to refresh the cache entry automatically before its expiration.

This cache strategy results in reduced latency if it can predict accurately what items are NEEDED in future.



Discussion

No Comment Found