In the past, experts have tried removing performance bottlenecks by using system caches but, with the rise in user traffic, the need for high-speed applications has become pertinent. Therefore, in-memory, linearly scalable, distributed caching solutions like NCache is becoming popular, enhancing system performance multifold.
Generally, with an added caching layer in your system architecture, applications are responsible for maintaining two-hop data integrity – on every write request, the data needs to be updated both in the cache and in the backend data source. So, if you have multiple applications, accessing the same cache and the database, you will have to duplicate your persistence code across all of them. This adds code complexity at the application level.
NCache Details NCache Write-Through Docs NCache Write-Through Admin Guide
What is Write-Through?
Write-Through is a caching technique, where when applications write data to the cache, and a backing source is configured, it updates the records in the database as well.
NCache provides an IWriteThru interface that you can implement, adding all the persistence code logic which otherwise would have existed in your client application. You need to deploy the code on all the cache servers via the NCache Web manager.
This way, whenever a write operation is performed on the cache, the deployed provider will be called and the database will be updated according to your logic.
To learn how to configure a Write-Through provider, please refer to the official NCache Write-Through Provider documentation.
Sometimes, your Write-Through operations might fail. This can happen due to several reasons like network connection problems or because of an error in your interface implementation logic. Since Write-Through is synchronous, NCache throws an exception to the application. You will find the error recorded in the cache logs as well.
Moreover, since NCache provides the operation status as well – it gives its users the flexibility to handle various predetermined scenarios. For example, in case the operation fails and you want to give it a retry, you can set the relevant option, and NCache will perform the retires for you.
For more details regarding these operations, please refer to NCache Docs.
The Write-Through Provider Interface
As mentioned earlier, NCache exposes an IWriteThru interface that the user is expected to implement and then deploy at the server-end. The code snippet below shows IWriteThuProvider methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public interface IWriteThruProvider { // Perform tasks associated with freeing, releasing, or resetting resources. void Dispose(); // Perform tasks like allocating resources or acquiring connections void Init(IDictionary parameters, string cacheId); //Responsible for write operations on data source. OperationResult WriteToDataSource(WriteOperation operation); ICollection WriteToDataSource(ICollection operations); ICollection WriteToDataSource(ICollection dataTypeWriteOperations); } |
There are three overloads of the WriteToDataSource method. One caters to atomic database update requests while the other handles bulk database write requests. NCache also exposes a method where you can add code logic to update NCache-supported data structures to the backing source.
NCache Details Write-Through Caching Docs Configure Write-Through Provider
Write-Through in Application
The front-end application only has to access the cache for any write operation with the option of WriteThru specified so that the cache applies that operation on the database via the Write-Through provider.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Pre-Condition: Cache is already connected // Fetch product with the given ProductID Product product = FetchProductByProductID(1001); // Specify the key of the item string key = $"product.ProductID"; product.UnitPrice = 200; // Create a new cacheItem with the product var cacheItem = new CacheItem(product); // Enable write through for the cacheItem created var writeThruOptions = new WriteThruOptions(); writeThruOptions.Mode = WriteMode.WriteThru; // Add the item in the cache with WriteThru enabled CacheItemVersion itemVersion = cache.Insert(key, cacheItem, writeThruOptions); |
What is Write-Behind Cache?
Write-Behind in a distributed cache is the same as Write-Through caching, except that it asynchronously updates the database. This means that the application never has to wait for the data source to be updated, increasing application performance since updates to the backend data sources are the slowest application operations.
Please keep in mind that while it is safe to assume that the data will be updated to the database, it is recommended that you opt for Write-Through if you are dealing with highly sensitive data. Moreover, since it is an asynchronous mechanism, failures in write-behind are logged as exceptions and errors in the cache logs but not thrown to the application. NCache provides options to handle failures in the same way as with Write-Through because it also returns operation statuses.
Refer to the NCache docs for details regarding these operations.
Write-Behind in Applications
Write-behind is a faster way to perform Write-Through operations as it uses the same Write-Through provider but asynchronously. The only change application-wise is the difference in specifying the Write-Through option as WriteBehind instead of WriteThru.
Take a look at the sample code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Pre-Condition: Cache is already connected // Fetch product with the given ProductID Product product = FetchProductByProductID(1001); // Specify the key of the item string key = $"product.ProductID"; product.UnitPrice = 200; // Create a new cacheItem with the product var cacheItem = new CacheItem(product); // Enable write through for the cacheItem created var writeThruOptions = new WriteThruOptions(); writeThruOptions.Mode = WriteMode.WriteBehind; // Add the item in the cache with WriteThru enabled CacheItemVersion itemVersion = cache.Insert(key, cacheItem, writeThruOptions); |
NCache Details Write-Through Caching Docs Using Write-Behind in Cache
Conclusion
With distributed caching solutions like NCache now providing features like Write-Through and Write-Behind, updates to the backend database are simpler and more manageable. It encapsulates all the persistence code logic within a single provider. This lets you to maintain the database layer at the provider level and caching layer at the application level. So, with NCache, you get the best of both worlds. You get caching performance for read operations and data maintenance for write operations. Thus, enhance your system’s performance with the help of enterprise solutions like NCache. Contact us and let our experts help you out!