Cache strategies
Hamdy A. AbdulFatah
Solutions and Integration Architect @ MOJ. Certified Apigee Engineer, Elasticsearch Engineer/Analyst, Google Cloud Professional Architect, AWS Solutions Architect Associate, Azure Solutions Architect and Developer
What are the different caching strategies you can use to enhance your application performance ?
Choosing a good caching strategy
We can summarize caching strategies into 5 types:
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
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
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
领英推荐
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.
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.
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
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.