In cache-aside as well as Read-through patterns, in both the patterns we need to write code to write to the database. So whats the real advantage of read-through,write-behind approach?Please clarify my doubt.
Read-through/Write-through (RT/WT): This is where the application treats cache as the main data store and reads data from it and writes data to it. The cache is responsible for reading and writing this data to the database, thereby relieving the application of this responsibility.
A cache-aside cache is the most common caching strategy available. The fundamental data retrieval logic can be summarized as follows: When your application needs to read data from the database, it checks the cache first to determine whether the data is available.
This is used when there are no frequent writes to the cache(The number of write operations is less). It helps in data recovery (In case of a power outage or system failure). A data write will experience latency (delay) as we have to write to two locations (both Memory and Cache). It Solves the inconsistency problem.
What is a “write-behind” cache? In a write-behind cache, data reads and updates are all serviced by the cache, but unlike a write-through cache, updates are not immediately propagated to the data store.
Yes you need to write code in both these patterns but there are a number of benefits for using read-through/write-behind approach.
E.g. in cache-aside pattern, your application is responsible for reading and writing from the database and also to keep cache synchronize with the database. This will make your application's code complex and also may cause code duplication if multiple applications are dealing with the same data. Read-through/write-behind on the other hand simplifies the application's logic.
Furthermore read-through may reduce database calls by blocking parallel calls for same object. As explained in this article by NCache
There are many situations where a cache-item expires and multiple parallel user threads end up hitting the database. Multiplying this with millions of cached-items and thousands of parallel user requests, the load on the database becomes noticeably higher.
Similarly write-behind(asynchronous) can improve your application's performance by speeding up the write operation,
In cache-aside, application updates the database directly synchronously. Whereas, a Write- behind lets your application quickly update the cache and return. Then it lets the cache update the database in the background.
See this article for further details on advantages of using read-through/write-behind over cache-aside. I hope this will help :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With