Awesome Software Engineer

Share this post

Cache delete strategy of the cache-aside pattern

blog.awesomesoftwareengineer.com

Cache delete strategy of the cache-aside pattern

Ray Chong
Aug 31, 2022
2
Share this post

Cache delete strategy of the cache-aside pattern

blog.awesomesoftwareengineer.com

I created a post to talk about the cache strategies before. If you haven’t read it, go check it out.

Awesome Software Engineer
4 Popular Cache Patterns
In this post, we’ll cover 4 popular cache patterns including Cache-aside, Read-through, Write-through, and Write-back patterns. The application performance will be improved a lot if you choose the right cache strategy. Let’s jump into the topic. Cache-aside…
Read more
7 months ago · Ray Chong

In the cache strategies post, I asked a question:

Do you know why we don’t remove the cache key first and write to the database afterward?

Here is the reason why we don’t do this.

Delete the cache before updating the database❌:

According to the diagram above, it is obviously a dirty read that happened in steps 4 to 6. As you can see, we perform step 3 to delete the cache and now a Read request comes and found that the cache is missing. So, it performs an action to read the data from the database and set the data back to the cache. Since the Write request is not finished yet and the Read request runs faster than the Write request so a data inconsistency problem happens in the cache. The subsequent requests will be reading the staled data from the cache since there is no action to remove the cache until the next Write.

To mitigate the data inconsistency problem, we can update the workflow to Delete the cache after updating the database✅:

Now, the workflow is updated. As you can see in the diagram above, we perform a database update first and clear the cache afterward. Under this flow, the Write request can delete the cache even if the Read request set the stale data into the cache. The subsequent requests will be reading the correct data from the cache. But, there is still a problem in steps 2 to 5 which is related to the execution sequence. If step 5 runs faster than step 4 (set cache), it will cause the data inconsistency problem again. But, it is less chance to have this problem in most of the use cases. Also, when adopting a cache strategy, we always need to have a reasonable expiry time to deal with the data inconsistency problem. We are not designing a strongly consistent system, so a little bit of data inconsistency is acceptable in most of the use cases.

Thanks for reading Awesome Software Engineer! Subscribe for free to receive new posts and support my work.

Share Awesome Software Engineer

Share this post

Cache delete strategy of the cache-aside pattern

blog.awesomesoftwareengineer.com
Previous
Next
Comments
TopNewCommunity

No posts

Ready for more?

© 2023 Ray Chong
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing