Write-Through and Write-Behind Caching Overview
This feature is only available in NCache Enterprise Edition.
NCache supports Write-Through caching which allows write operations directly on the data source through the cache. In this way, you can synchronize your cache and the data source. In Write-Through caching, NCache updates cache store first and then applies that operation to the data source. For example, if a client application updates an entry in cache, then NCache will also update the data source (if Write-Through is enabled).
To use Write-Through Caching in your application, you need to first implement
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
Currently NCache provides two modes for Write-Through caching given below:
- Write-Through (Updates data source synchronously)
- Write-Behind (Updates data source asynchronously)
NCache provides a performance counter
Write-thru/sec for Write-Through operations.
In Write-Through caching, an operation is first applied on 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.
In Write-Through, due to synchronous operations on data source, the rate of operations on data source 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, data source operations are performed asynchronously after NCache performs operations on cache store. After updating cache store, these operations are queued up and later applied to configured data sources asynchronously. Thus using Write-Behind will enhance the response time of cache operations.
NCache provides different configuration settings in Write-Behind to control operation flow on data source. For instance, you can specify the rate at which NCache will apply Write-Behind operations on data source through Throttling.
It indicates the number of operations applied on data source per second. Default value for throttling is 500 ops/sec. You can change this value through backing source settings in NCache Web Manager.
NCache provides a performance counter
Write-behind/sec for Write-Behind operations.
NCache allows you to apply Write-Behind operations individually or in 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.
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.
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.
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 NCache Web 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.
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.
Partitioned-Replicated 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
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.
NCache also provides a performance counter for
Datasource failed operations/sec.
Write operations performed on data source
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
NCache Web Manager and if that limit exceeds, 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
associated with it, which is incremented on each operation retried on the data
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
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 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 cache store synchronously
through Write-Through or asynchronously through Write-Behind caching.
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
Write-Through Provider Configuration and Implementation
Explains the Write-Through Provider interface and provides a sample implementation for the interface.
Using Write-Through with Cache Operations
Provides samples to use Write-Through with basic operations in NCache.
Using 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.