Write-Through and Write-Behind Caching Overview
NCache supports Write Through caching, which allows the write operations directly on the data source through the cache. This way, you can synchronize your cache and the data source. In Write-Through caching, NCache updates the cache store first and then applies that operation to the data source. For example, if a client application updates an entry in the cache, then NCache will also update the data source (if Write-Through is enabled).
Note
This feature is only available in NCache Enterprise Edition.
To use Write-Through Caching in your application, you need to first implement the IWriteThruProvider
interface. NCache will internally use this custom provider to perform write operations on the data source. NCache will call your
provider behind write operations (Add, Insert, Remove/Delete) API calls with Write-Through.
Currently, NCache provides two modes for Write-Through caching given below:
- Write-Through (Updates data source synchronously)
- Write-Behind (Updates data source asynchronously)
Note
NCache provides a performance counter Write-thru/sec
for Write-Through operations.
Write-Through
In Write-Through caching, an operation is first applied on the cache store and then synchronously updated to the configured data source. Using Write-Through, operations will be completed after NCache applies that operation to the data source. You can use Write-Through caching if immediate data source updates are critical and you need to update the data source as soon as the cache is updated.
Write-Behind
In Write-Through, due to synchronous operations on the data source, the rate of operations will be the same as the rate of user operations on the cache. For applications with high user traffic, the rate of user operations on the cache can be very high which can overwhelm your data source. Also, synchronous data source operations may affect the response time of a user operation.
To overcome these problems, Write-Behind can be used instead of Write-Through. In Write-Behind, the data source operations are performed asynchronously after NCache performs operations on the cache-store. After updating the cache store, these operations are queued up and later applied to configure the data sources asynchronously. Thus using Write-Behind will enhance the response time of the cache operations.
NCache provides different configuration settings in Write-Behind to control the operation flow on the data source. For instance, you can specify the rate at which NCache will apply Write-Behind operations on the data source through Throttling.
Throttling
Indicates the number of operations applied on the data source per second. The default value for throttling is 500 ops/sec. You can change this value through the backing source settings in the NCache Management Center.
Note
NCache provides a performance counter Write-behind/sec
for Write-Behind operations.
Write-Behind Modes
NCache allows you to apply Write-Behind operations individually or as a batch. A Write-Behind queue is maintained for Write-Behind operations. All Write-Behind operations will be queued in this queue and later applied to the data source according to the configured batch or non-batch mode explained below.
Non-Batch Mode
By default, non-batch mode will be configured for Write-Behind operations. In this mode, operations in the Write-Behind queue will be applied one by one on the data source according to the configured throttling rate. For example, if the throttling rate is 500 operations per second, NCache will apply Write-Behind operations one at a time to the data source and it will not exceed 500 operations per second.
Batch Mode
In Batch-mode, a batch/bulk of operations is selected according to their operation delay
. You can configure operation delay
for Write-Behind operations, which indicates the time in milliseconds that each operation must wait in the Write-Behind queue before being applied to the data source. By default, its value is zero.
The batch interval is the configurable interval according to which NCache periodically checks for operation delay timed out operations in the Write-Behind queue. For example, if operation delay is configured as 1000ms and batch interval as 5s, NCache checks the operations in the Write-Behind queue every 5s and selects all operations that have expired operation delays (all operations that are in queue for the last 1000 milliseconds).
After the selection of operations in bulk, they are applied to the data source according to the configured throttling rate. Let's say a bulk of 1000 operations are selected from the Write-Behind queue, which is then applied to the data source in a batch of 500 operations (if the throttling rate is 500 ops/sec) as the maximum operation applied to the data source per second can't exceed the configured throttling value.
You can specify an operation delay time ranging from seconds to days and months. In this way, you can pause your operations on the data source in a configurable amount of time. NCache also provides performance counters for the write-behind queue, operations count, and current batch operation count. For Write-Behind, if batching is enabled, operations that are ready to be executed on the data source are dequeued from the Write-Behind queue.
Note
The number of operations dequeued in the current batch interval will be displayed by the current batch operation count counter.
Hot Apply Support for Write-Behind Configuration
NCache supports hot applicable Write-Behind settings which allows you to change Write-Behind configurations at runtime, without stopping the cache. You can change Write-Behind configurable attributes through the NCache Management Center and NCache will incorporate those changes dynamically.
In hot apply support, you can change the Write-Behind mode from batch to non-batch and vice versa. For instance, if you have changed the batch mode to non-batch, then NCache will ignore the operation delay value and start executing operations individually. Also, you can change the throttling rate at runtime according to your need similarly, operation delay, batch-interval, failed operations queue limit, and failed operations eviction ratio can also change at runtime.
Warning
You can only increase the value of the failed operations queue limit; otherwise, NCache will use its default value for further operations.
Write-Behind in Clustered Environment
As a Write-Behind queue is maintained for Write-Behind operations, a separate dedicated thread monitor executes its operation. The topology level details for Write-Behind are mentioned below:
Replicated Topology: Write-Behind queue is maintained on all nodes, but Write-Behind async processor is present on coordinator node only. It means that all Write-Behind operations are performed through this node and replicated to other node queues cluster wide. In this way, if a node is down, then the next coordinator node performs all of the remaining write behind operations.
Partition-Replica Topology: Write-Behind queue is maintained on each active node and also replicated to its corresponding replicas. Each node is responsible for its Write-Behind operation on data source.
Mirrored Topology: Write-Behind queue is maintained on both active and passive nodes, but only the active node is responsible to perform Write-Behind operations. Similarly, if the active node is down, then the passive node becomes active and performs the remaining Write-Behind operations.
Partitioned Topology: Write-Behind queue is maintained on each partition and every node is responsible for its Write-Behind operations on data source.
Caching Operation Result
NCache provides you with the flexibility to synchronize Write-Through operations in cache based on their operation result. After applying an operation (Add/Insert) on data source, you can specify operation status based on which NCache will synchronize the cache store. For example, in case of data source operation failure, you can decide to remove that data from cache or to keep it. You can also retry that operation on the data source. For this, you
have to specify Success
/Failure
/FailureRetry
/FailureDontRemove
as OperationResult.Status
of OperationResult
. This is provided in both Write-Through and Write-Behind caching.
Data source operation status and their corresponding actions by NCache are described below:
Success: This means that the data source operation is successful and the item was added to the data source so NCache will keep it in the cache as well.
Failure: This means that data source operation failed and the item could not be added to data source, so NCache will remove it from the cache as well.
FailureDontRemove: This means that the data source operation failed and the item could not be added to the data source, but NCache will keep it in the cache.
FailureRetry: This means that data source operation failed and the item could not be added to the data source, so NCache will keep the item in cache and retry. Retries will be done as Write-Behind operations.
Retrying Failed Operations
NCache allows you to retry operations in Write-Through/Write-Behind in case they are failed on data source. For this purpose, if you enable operation retrying then NCache will retry that operation on data source. In case of Write-Through or Write-Behind, all retry operations will be requeued to Write-Behind queue, which means a Write-Through retry operation will be retried asynchronously as a Write-Behind operation.
Note
NCache also provides a performance counter for Datasource failed operations/sec
. Write operations performed on data source returning Failure
/FailureRetry
/FailureDontRemove
as OperationResult.Status
of OperationResult
are counted per second by this counter.
NCache allows you to limit the number of failed operations to be retried. In such a situation, you will mention the Failed operation queue limit through the NCache Management Center and if that limit is exceeded, you can evict failed operations through a configurable eviction ratio. Here, NCache will evict most retried operations when retried queue is full. Each operation has a RetryCount
property associated with it, which is incremented on each operation retried on the data source.
For this, NCache provides a performance counter for Write-behind failure retry count
and Write-behind evictions/sec
. The Write-behind failure retry count
shows the number of operations requeued for retry. Data source write operations returning FailureRetry
as Status
in OperationResult
will be requeued for retry. Whereas, Write-behind evictions/sec
counter displays the number of retry operations evicted per second.
Updating Cache after Data Source Operation
As stated earlier, in Write-Through caching, the operation is first performed on cache store and then to data source. There may exist scenarios in which after performing operations on the data source, data becomes modified, e.g., in case of identity columns, its value may get modified by data source operation. In such a situation, data may become inconsistent in cache and data source. To handle this, NCache allows you to specify whether to update data in cache after
data source operations or not. You can enable UpdateInNCache
flag to operate (Add/Insert) again on cache store to make it synchronized with the data source, hence, updating operations in the cache store synchronously through Write-Through or asynchronously through Write-Behind caching.
Note
NCache also provides a performance counter Datasource updates/sec
that displays the number of update operations per second in cache after data source write operations.
In This Section
Configure Write-Thru Provider
Explains the Write-Through Provider interface and provides a sample implementation for the interface.
Write-Thru with Cache Operations
Provides samples to use Write-Through with basic operations in NCache.
Write-Behind with Cache Operations
Provides samples to use Write-Behind with basic operations in NCache.
Monitor Write-Thru Counters
Describes the performance counters provided by NCache to monitor Write-Through Caching.