NCache 4.6 - Online Documentation

Client Cache

A client cache is a local cache synchronized with a remote clustered cache. It always synchronizes with remote cache through notifications that are triggered whenever changes are made into the state of data. Because of synchronized nature of client cache, it provides maximum boost in performance and scalability of application for read operations without compromising data integrity.  
Since the client cache resides on the same box where client application is running, it saves the network roundtrip every time data is served from the client cache. Therefore, data retrieval in this case is much faster as compared to fetching the same data from the remote server.
Client  cache can either be InProc (resides within the application process) or OutProc (resides locally or on remote box). By default, cache is initialized as OutProc, however, from NCache Manager, it can be configured to start as InProc. Client applications can initialize the cache as InProc by specifying the initialization mode as ‘InProc’ while calling NCache.InitializeCache(). Client cache cannot be directly initiated using Client API Initialize method. It's always used with some remote clustered cache such as Mirrored, Partitioned, and Partition-Replica Cache.
Synchronization Modes
There can be multiple client cache instances running on different client nodes that simultaneously interact with remote clustered caches. Also, client cache has a subset of data residing in remote clustered cache. It means that there is a small window of time for client caches to sync their state. To solve this problem, client cache provides data synchronization with the clustered cache. Data synchronization is performed between client cache and remote clustered cache and is inevitable. Client cache provides two synchronization modes.
  • Optimistic Synchronization
In optimistic mode, application fetches data from client cache despite the fact that data is updated or not. It assumes that data is newer in client cache however, there are possibilities that data may be older than that on the clustered cache because event notifications might not have yet been received during get operations. In optimistic synchronization, updates are propagated to the client cache using event notifications. Synchronization mechanism is explained later in this chapter.
  • Pessimistic Synchronization
Pessimistic mode is used when applications are handling sensitive data. On data fetch request, if data is found in client cache, its version is matched with item on remote cache. If both versions are same, then item is returned otherwise updated item is fetched from remote cache and is updated back to client cache. This is done to make sure that client application always gets the latest data. However, get operations are slower in pessimistic mode than in optimistic, but it ensures data consistency and takes extra care that the retrieved data is always updated. Notification based updates work similar to optimistic mode.
How Client Cache Works
Whenever the client application using client cache adds/updates data into the cache, it is first updated to second level (remote clustered cache) then on the successfully completed operation, data is updated to client cache. Whereas in the case of data retrieval, it is first looked into client cache; if found, the data is returned to client, otherwise it is fetched from the remote cache and inserted into client cache to make it available for next access. By doing this, client cache reduces unwanted trips to clustered cache, thus increasing performance while fetching data. The figure below explains the internal working of client cache for update and fetch operations on data.
Figure 1. Client Cache Workflow
Data Synchronization in Client Cache
While an an item is added to client cache, client cache registers an item level notification with the remote server for any updates or removal of that very item. Whenever this item is updated on the server node, client cache receives a notification which then fetches the updated data from remote cache and updates it locally too. This way, it is made sure that client cache is always synchronized with cache servers even when items are being updated from clients running on other machines. The remote servers propagate these changes only to the clients who have shown interest in the specific items instead of propagating these changes to all client caches. This synchronization mechanism is used for both optimistic and pessimistic client cache. The following figure demonstrates data synchronized  with clustered cache and client cache.  
Figure 1. Client Cache
InProc client cache stores items in object form, therefore if object is modified by the application, it should be updated back to the cache so that other remote clustered cache and other client caches have no data integrity issues.
See Also