Cache strategies
From https://www.storagereview.com/

Cache strategies

What are the different caching strategies you can use to enhance your application performance?

Choosing a good caching strategy can indirectly enhance your application performance, availability, and scalability. You can choose one strategy or a combination of them which is more suitable for your use case.

We can summarize caching strategies into 5 types:

No alt text provided for this image
Caching strategies


Below are the details of each cache strategy with its use case and drawbacks.

Cache-aside

In the cache-aside strategy, the application tries to get the data from the cache, if it exists in the cache, the result is returned immediately. If the data is not in the cache, the application gets the data from the database and then updates the cache so that in subsequent read requests the data is fetched directly from the cache.

Use Cases: read-heavy workloads

Drawbacks: data in the cache may be delayed due to recent writes in the database for a while until the cache is updated

No alt text provided for this image
Cache-aside caching strategy


Read-through

With the read-through strategy, the cache sits between the application and the database for read requests. The application always gets the data from the cache, if the data exists in the cache it is returned immediately and if not, the cache will get the data from the database and update itself then returns the data to the application. The write requests still go to the database directly. The difference between the cache-aside and the read-through strategies is that the application updates the cache in the first strategy whereas the cache updates itself in the second strategy.

Use Cases: read-heavy workloads

Drawbacks: data in the cache may be delayed due to recent writes in the database for a while until the cache is updated

No alt text provided for this image
Read through caching strategy


Write-through

In the write-through strategy, the application writes data to the cache, and then the cache writes data to the database.

Use case: can be combined with the read-through strategy to make use of the benefits of both without having the issue of stale cache data due to recent writes.

Drawbacks: latency in the write as there are 2 writes, one in the cache and another in the database.

No alt text provided for this image
Write-through caching strategy


Write-back

In the write-back strategy, the application writes data to the cache, and then the cache writes data to the database. but, instead of writing to the database with every write request, the cache writes in bulk. so the key difference from the write-through is the frequency in which the cache writes data to the database.

Use case: write-heavy workloads

Drawbacks: possible data loss if the cache fails or goes down before writing the cache write requests to the database.

No alt text provided for this image
Write-back caching strategy


Write-around

In the write-around strategy, the application writes data to the database, and only the data that is read go to the cache. if the data doesn't exist in the cache the application read the data from the database and then updates the cache.

Use case: rarely or no read for the data

Drawbacks: cache miss for recently written data

No alt text provided for this image
Write-around caching strategy

Conclusion:

In this article, we discussed the different caching strategies you can make use of. You can select one of them or a combination of them based on your use case. Selecting the proper caching strategies will have a great effect on your application.

要查看或添加评论,请登录

Hamdy A. AbdulFatah的更多文章

社区洞察

其他会员也浏览了