Read-through, Write-through, Write-behind in NCache

NCache is an extremely fast and scalable In-Memory Distributed Cache for .NET that caches application data to reduce expensive database trips. You can use NCache to remove performance bottlenecks associated with your data storage and database scalability.

The most common way to use such a cache is to let your application read data from your database/data source and then caches it. After which, when your application updates this data in your database, it also updates the cache to ensure the cache is in sync with the database.

Furthermore, NCache provides another very powerful mechanism for reading and writing data called Data source Providers (Read-through/Write-through/Write-behind) to let NCache read and write data from your database.

There are three ways you can use NCache in this context:

  1. Read-through: you develop a Read-through Handler that resides and runs on all cache servers in the cluster. Then, when your application fetches any data from the cache, NCache calls the Read-through Handler to go and read this data from your data source if that data is not already present in the cache.
  2. Write-through (sync): you develop a Write-through Handler that also resides and runs on all cache servers in the cluster. And, when your application updates data in the cache, NCache first updates the cache and then calls the Write-though Handler to immediately update your database with it too. And, your application waits until data is updated in the database as well.
  3. Write-behind (async): if you don't want your application to wait until the Write-through Handler completes the updating of data in the database, you can choose the Write-behind option where NCache updates your database asynchronously and your application does not have to wait for the database update. This speeds up your application as database updates are usually much slower than cache updates.
Figure 1: Server-Side Code (.NET Edition)
Figure 1: Server-Side Code (.NET Edition)
Figure 1: Server-Side Code (Java Edition)
Figure 2: Server-Side Code (Java Edition)

Benefits of Read-through / Write-through / Write-behind

Here are some benefits of using Read-through, Write-through, and Write-behind in NCache.

  1. Better write performance with Write-behind: Updating your database is usually much slower than updating the cache. When you use Write-behind, your application only updates the cache and moves on while NCache asynchronously updates your database. This improves your application performance quite a bit.
  2. More database scalability with Write-behind Throttling: If your application is updating the database quite frequently, it is likely to choke. But, if you turn on throttling with Write-behind, then NCache keeps your data updated in the cache with replication and updates your database slowly. This relieves a lot of pressure from your database.
  3. Auto-refresh cache on expiration: If you have developed a Read-through Handler, you can enable auto-refresh on expiration with NCache. This way, whenever any cached item expires, instead of removing it from the cache, NCache calls the Read-through Handler to reload a fresh copy from your data source.
  4. Auto-refresh cache on database changes: If you have developed a Read-through Handler, you can enable auto-refresh on SQL Dependency/Oracle Dependency with NCache. This way, whenever the corresponding data in the database changes, instead of removing the cached item from the cache, NCache calls the Read-through Handler to reload a fresh copy from your database.

Feature Highlights

Here are some highlights of Read-through, Write-through, and Write-behind in NCache.

  1. Server-side Code (.NET or Java): You can develop your Read-through/Write-through Handlers with either .NET or Java. There is an interface for each Handler that you develop and deploy your code on all cache servers in the cluster. NCache is able to call them at runtime when needed. Any Handlers developed in .NET are called natively by NCache since it is also developed in .NET. However, Handlers developed in Java are called by NCache through the Java Native Interface (JNI).
  2. Multiple Read-through/Write-through Handlers: You can develop multiple Read-through and Write-through Handlers and register them with NCache. In that case, they’re all named and your application can ask for any of them by specifying their name.
  3. Default Read-through/Write-through Handler: If you have multiple Handlers registered as named Handlers, then NCache allows you to select one of them as the default options. This default Handler is used when your application doesn’t specify a Handler name.
  4. Read-through Highlights
    1. Forced Read-through: NCache provides an option where you can tell it to use Read-through even if the item exists in the cache (usually, Read-through is only called when an item is not found in the cache). This allows you to refresh this item from your database if you feel the data might have changed there.
    2. Bulk Get Read-through: NCache provides a Bulk Get API that allows your application to provide a list of keys against which you can fetch data from the cache.
    3. Data Structures Read-through (Counter, Dictionary, List, Queue, Set): NCache provides your application with the ability to fetch any data structure and NCache calls a Read-through handler to fetch this data from your database.
  5. Write-through Highlights
    1. Remove Item Write-through: NCache also provides your application with the Remove API which lets you use a key to remove an item from the cache and then from the data source.
    2. Bulk Remove Items Write-through: NCache also provides a Bulk Remove API that lets your application provide a list of keys to remove from the cache and data source as a bulk.
    3. Bulk Add/Update Write-through: NCache provides a Bulk Add/Update API that lets your application to provide a list of keys and their values to add/update the cache in bulk. Write-through works in this situation as well and adds/updates your database / data source with these items in bulk.
    4. Data Structures Write-through (Counter, Dictionary, List, Queue, and Set): NCache provides the ability for your application to update any data structure and NCache calls a Write-through handler to go update your database with the changes.
    5. Async Add/Update/Remove Write-through: NCache API provides Async Add/Insert/Remove. Write-through supports this API and gets invoked whenever your application makes that API call. Here. Write-through assumes your application is waiting for the operation to complete, and the fact that it's asynchronous is only recognized at the client API level.
  6. Write-behind Highlights
    1. Async Operations Queue Replicated (high-availability): Write-behind accepts the client’s request to update the cache and immediately updates the cache but queues up the database update for asynchronous execution later. This queue is always replicated to more than one cache server depending on the caching topology to make sure these operations are never lost if any cache server ever goes down suddenly.
    2. Throttling: You can specify a throttling level for Write-behind async operations. This allows you to spread them out. The default throttling level is 500 ops/sec.
    3. Write-behind Modes (Non-Batch or Batch): The Non-Batch mode means that each operation in the Write-behind queue is executed separately, whereas in Batch mode you lump multiple operations together and execute them collectively.
    4. Batch Operation Delay: You can specify a delay in-between two batch mode operations. This allows you to slow down your updates to the database if you want.
    5. Failed Operations Queue: Since Write-behind performs all operations asynchronously when an operation fails, NCache can put it in the Failed Operations Queue so it can be executed again without interfering with fresh operations that have not failed. You can specify a max size and eviction.
  7. Monitor Write-through/Write-behind through Counters: NCache provides a rich set of counters with which you can monitor what is happening in Write-through/Write-behind. These counters are available in the NCache Monitor, Windows PerfMon Tool, SNMP Counters, Prometheus, and Grafana.

Read-through / Write-through Code Interface

Developing code for Read-through and Write-through Handlers is a straightforward process. Below are examples of interfaces for both.

public interface IReadThruProvider
{
	void Init(IDictionary parameters, string cacheId);
	ProviderCacheItem LoadFromSource(string key);
	IDictionary<string, ProviderCacheItem> LoadFromSource(ICollection<string> keys);
	ProviderDataTypeItem<IEnumerable> LoadDataTypeFromSource(string key, DistributedDataType dataType);
	void Dispose();
}
public interface ReadThruProvider extends java.lang.AutoCloseable
{
	void init(java.util.Map<java.lang.String,java.lang.String> parameters, java.lang.String cacheId) throws java.lang.Exception;
	ProviderCacheItem loadFromSource(java.lang.String key) throws java.lang.Exception;
	java.util.Map<java.lang.String,ProviderCacheItem> loadFromSource(java.util.Collection<java.lang.String> keys) throws java.lang.Exception;
	ProviderDataStructureItem loadDataStructureFromSource(java.lang.String key, DistributedDataStructureType distributedDataStructureType) throws java.lang.Exception;
}
public interface IWriteThruProvider
{
	void Init(IDictionary parameters, string cacheId);
	OperationResult WriteToDataSource(WriteOperation operation);
	ICollection<OperationResult> WriteToDataSource(ICollection<WriteOperation> operations);
	ICollection<OperationResult> WriteToDataSource(ICollection<DataTypeWriteOperation> dataTypeWriteOperations);
	void Dispose();
}
public interface WriteThruProvider extends java.lang.AutoCloseable
{
	void init(java.util.Map<java.lang.String,java.lang.String> parameters, java.lang.String cacheId) throws java.lang.Exception;
	OperationResult writeToDataSource(WriteOperation operation) throws java.lang.Exception;
	java.util.Collection<OperationResult> writeToDataSource(java.util.Collection<WriteOperation> operations) throws java.lang.Exception;
	java.util.Collection<OperationResult> writeDataStructureToDataSource(java.util.Collection<DataStructureWriteOperation< dataStructureWriteOperations) throws java.lang.Exception;
}
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.