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 on the data source. You can use Write-through caching if immediate data source updates are critical and you need to update data source as soon as cache is updated.
Write-behind
In Write-through, due to synchronous operations on the data source, the rate of operations will be same as the rate of user operations on the cache. For applications with high user traffic, the rate of user operations on cache can be very high which can overwhelm your data source. Also synchronous data source operations may affect 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 cache store. After updating the cache store, these operations are queued up and later applied to configured 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 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 Write-behind queue will be applied one by one on 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 data source and it will not exceed from 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 on the data source. By
default, its value is zero.
Batch interval is the configurable interval according to which NCache periodically checks for operation delay timed out operations in Write-behind queue. For example, if operation delay is configured as 1000ms and batch interval as 5s, NCache checks the operations in Write-behind queue after every 5s and selects all operations which have expired operation delays (all operations which are in queue for last 1000 milliseconds).
After 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 are then applied to the data source in a batch of 500 operations (if throttling rate is 500 ops/sec) as the maximum operation applied to 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 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 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 Manager 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 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 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 on the basis of their operation result. After applying an operation
(Add/Insert) on data source, you can specify operation status on the basis of
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 Manager 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 perform
operation (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-through Provider
Explains the Write-through Provider interface and provides a sample implementation for the interface.
Write-through 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-through Counters
Describes the performance counters provided by NCache to monitor Write-through Caching.