What are different cache read strategies?
What is Cache-Aside read strategy?
In Cache-Aside strategy, application is responsible for talking to cache and database. Logic sits in application that if object is not found in cache then get data from DB and update cache also.
Here is how application logic will look:
- Application first checks the cache.
- If data is found in cache, then data is read and returned.
- If the data is not found in cache, the application queries data from database, stores it in cache and then return the call to client.
- If the cache goes down, the application will still be fine as call will go to database. Though you have to consider what will happen if cache goes down when load is high.
- Since application is controlling data insertion in cache, application can have different object structure for storing data into cache and different structure of object in database.
What is Read-Through cache strategy?
In Read-Through cache, cache is intelligent enough to get data from database, save it in cache if it gets calls from application and it doesn’t have that specific object with itself.
Cache-Aside and Read-Through, both strategies load data lazily, that is, only when it is first read.
Difference between Cache-Aside and Read-Through:
- In cache-aside, the application fetches data from the database and populates it in the cache.
- In read-through, application doesn’t have to do anything, cache library itself has logic to get data from database.
- In Cache-Aside data model of objects stored in cache and database can be different but in Read-Though it will be the same.
- Read-through caches work best for read-heavy workloads when the same data is requested many times.
- When the data is requested the first time, it always results in a cache miss. Though application dev has the option of ‘warming’ or ‘pre-heating’ the cache by querying cache manually at start and loading data to cache.
- It is also possible for data to become inconsistent between cache and the database.