Caching Strategy for restful API and website performance of any web page is a significant factor. It can affect the user’s experience and affect the business if not considered and optimized correctly.
Cache Consistency – Cache Writing Policy
When writing we must decide when to write to the cache or the primary data store.
The following are Cache Writing Policy designs:
Write-behind cache design
This writes first to the cache and then to the primary database which can happen almost immediately or after a set amount of time. Changes in the cache that have not been made to the database are referred to as “dirty”.
Write-behind caching is convenient when you expect to have a write-heavy workload. However, if the time limit is long and the system goes down, you risk losing data that has not been updated in the database.
Write-through cache Design
This updates both the cache and the database at the same time. If the cache layer fails, the update will not be lost since it will be persisted in the database. Write-through caches are convenient when updates are done infrequently. If you perform updates too many times, this reduces the benefit of having a cache in the first place since you must hit the database.