Cache is used by applications to optimize the repeated data access from data store like DB. Caching is effective for either relatively static data or the data that is read frequently.
In this pattern application is only responsible for reading and writing data from the database.Cache is kept aside and it does not interact directly with the database. The cache is kept as in-memory data enabling faster and better data scaling.
- This Pattern is about loading data into cache from data store on demand
- This pattern helps in performance and also on maintaining the consistency of cache data with the data store.
- It supports read-through, write-through and write-behind operations
- Applications should take care of maintaining the consistency of data and also detect and handle data when it becomes stale.
- The principle of the pattern is
- Check if the requested data is available in the cache first .
- If not then retrieve the data from the data store and also update the cache with the data
- If the data is modified in the cache then that data needs to be persisted in the data-store.
- It is very powerful and allows usage of complex DB queries involving joins and nested queries.
Important point to consider while using this pattern
- Lifetime of cached data – Do not make expiration date too short or too long . Customize based on the data access pattern of the application
- Removing/cleaning the cache
- Cache-aside pattern will not be suitable for applications which requires data to be pre-populated . It may still be valid if those data gets removed and pre-populated during the application run-time.
- In-Memory caching – applicable if the applications has repeated access to the cache. Most appropriate for this scenario would be shared or distributed caching
Suitable scenario for using this pattern
- When the cache does not provide a native read-through or write-through operation
- On Demand cache population