Query a Distributed Cache with SQL-Like Aggregate Functions

Today, distributed cache is widely used to achieve scalability and performance in high traffic applications. Distributed cache offloads your database servers by serving cached data from in-memory store. In addition, few distributed caches also provide you SQL-like query capability, which you can easily query your distributed cache the way you query your database i.e. “SELECT employee WHERE employee.city=‘New York’”

First of all, most distributed caches don’t even provide SQL-like querying capabilities. Even a few that do provide this have a very limited support for it. They only provide searching of distributed cache based on simple criteria. Whereas, there are several scenarios where you have to find the result based on aggregate functions i.e. “SELECT COUNT(employee) WHERE salary > 1000” or “SELECT SUM(salary) WHERE employee.city = ‘New York’”. In order, to achieve this you have to first query the distributed cache and then calculate the aggregate function on fetched cache data.

This approach has two major drawbacks. First is that you have to execute query on distributed cache, which involves fetching of all the data from distributed cache to cache client. This data may vary from MBs to GBs, and this operation becomes more expensive when you are also paying for the consumed network bandwidth. Moreover, mostly you don’t need this data after you are done with aggregate function calculations.

Second drawback is that it involves custom programming for aggregate function calculation. This adds extra man hours and still most of the complex scenarios cannot be covered. It would be much nicer if you could continue to develop application for the purpose that it is being built and not worry about designing and implementing these extra features yourself.

These are the reasons why NCache provides you the flexibility to query distributed cache using aggregate functions like COUNT, SUM, MIN, MAX and AVG as part of its Object Query Language (OQL). Using NCache OQL aggregate functions, you can easily perform the required aggregate calculations inside the distributed cache domain. This approach not only avoids the network traffic, but also provides you much better performance in term of aggregate functions calculation. This is because all the selections and calculations are done inside the distributed cache domain and no network trips are involved.

Here is the code sample to search NCache using OQL aggregate queries:

[code lang=”csharp”]
public void Main(string[] args)


String query = "SELECT COUNT(Business.Product) WHERE
this.ProductID > ? AND this.Price < ?";

Hashtable param = new Hashtable();
param.Add("ProductID", 100);
param.Add("Price", 50);

// Fetch the cache items matching this search criteria
IDictionary searchResults = _cache.SearchEntries(query, values);



For more reduced query execution time, NCache runs the SQL-like query in parallel by distributing it to all the cache servers  just like the map-reduce mechanism. In addition, you can use NCache OQL aggregate queries in both .NET and Java applications.

In summary, NCache provides you not only the scalability and performance, but also the flexibility of searching distributed cache using SQL like aggregate function.

Download NCache Trial | NCache Details

Posted in Aggregate Functions, Distributed Cache, Object Query | Tagged , , , | Leave a comment

Class Versioning in Runtime Data Sharing with Distributed Cache

Today many organizations use .NET and Java technologies to develop different high traffic applications. At the same time, these applications not only have a need to share data with each other, but also want to support runtime sharing of different version of the same class for backward compatibility and cost reduction.

The most common way mostly used to support runtime sharing of different class versions between .NET and Java application is through XML serialization. But, as you know XML serialization is an extremely slow and resource hungry process. It involves XML validation, parsing, transformations, which really hampers your application performance and uses extra resources in term of memory and CPU.

The other approach widely used to support sharing of different class versions between .NET and Java is through database. However, the problem with this approach is that it’s slow and also doesn’t scale very well with the growing transactional load. Therefore, your database quickly becomes a scalability bottleneck because you can linearly scale your application tier by adding more application servers, but you cannot do the same at the database tier.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

This is where a distributed cache like NCache comes in really handy. NCache provides you a binary-level object transformation between different versions not only of the same technology but also between .NET and Java. You can map different versions through an XML configuration file, and NCache understands how to transform from one version to another.

NCache class version sharing framework implements interoperable binary serialization custom protocol that generates byte stream based on specified mapping in such a format that any new and old versions of the same class can easily de-serialize it, regardless of its development language, which can be .NET or Java.

Here is an example of NCache config.ncconf with class version mapping:

[code lang=”XML”]
<cache-config name="InteropCache" inproc="False" config-id="0" last-modified="" type="local-cache" auto-start="False">

<type id="1001" handle="Employee" portable="True">
<attribute name="Name" type="Java.lang.String" order="1"/>
<attribute name="SSN" type="Java.lang.String" order="2"/>
<attribute name="Age" type="int" order="3"/>
<attribute name="Address" type="Java.lang.String" order="4"/>
<attribute name="Name" type="System.String" order="5"/>
<attribute name="Age" type="System.Int32" order="6"/>
<attribute name="Address" type="System.String" order="7"/>
<class name="com.samples.objectmodel.v1.Employee:1.0" handle-id="1"
assembly="com.jar" type="Java">
<attribute name="Name" type="Java.lang.String" order="5"/>
<attribute name="SSN" type="Java.lang.String" order="2"/>
<class name="com.samples.objectmodel.v2.Employee:2.0" handle-id="2"
assembly="com.jar" type="Java">
<attribute name="Name" type="Java.lang.String" order="5"/>
<attribute name="Age" type="int" order="6"/>
<attribute name="Address" type="Java.lang.String" order="7"/>
<class name="Samples.ObjectModel.v2.Employee:" handle-id="3"
assembly="ObjectModelv2, Version=, Culture=neutral,
PublicKeyToken=null" type="net">
<attribute name="Name" type="System.String" order="5"/>
<attribute name="Age" type="System.Int32" order="6"/>
<attribute name="Address" type="System.String" order="7"/>
<class name="Samples.ObjectModel.v1.Employee:" handle-id="4"
assembly="ObjectModelv1, Version=, Culture=neutral,
PublicKeyToken=null" type="net">
<attribute name="Name" type="System.String" order="5"/>
<attribute name="Age" type="System.Int32" order="6"/>


How does NCache do Class Versioning in Runtime Data Sharing?

In the ncache.config file that you see above, you’ll notice that the Employee class has a set of attributes defined first. These are version independent attributes and appear in all versions of .NET and Java classes. This is actually a superset of all attributes that appear in different versions. Below that, you specify version-specific attributes and map them to version-independent attributes above.

Now, let’s say that you saved .NET Employee version Now, when another application tries to fetch the same Employee, but it wants to see it as Java version 1.0 or 2.0. NCache knows which attributes of .NET version to fill with data and which ones to leave blank and vice versa.

There are many other scenarios that NCache handles seamlessly for you. Please read the online product documentation for how NCache runtime data sharing works.

Finally, the best part is that you don’t have to write any serialization and deserialization code or make any code changes to your application in order to use this NCache feature. NCache has implemented a runtime code generation mechanism, which generates the in-memory serialization and deserialization code of your interoperable classes at runtime, and uses the compiled form so it is super-fast.

In summary, using NCache you can now share different class versions between your .NET and Java applications without even modifying your application code.

Download NCache Trial | NCache Details

Posted in Data Sharing with Distributed Cache, Distributed Cache | Tagged , , | Leave a comment

Distributed Cache Continuous Query for Developing Real Time Applications

High traffic real time applications are widely used in the enterprise environment. In real-time applications, information is made available to you in moments it’s produced and any delay in doing so can cause a serious financial loss. The main challenge faced by these high traffic real time applications is to get notified about any changes in data set so that the corresponding views can be updated.

But, these high traffic real time applications cannot rely on the traditional database because they only support queries on the residing data and in order to get the updated data set you have to again execute the query after specific interval which is not instantaneous. And, this periodic polling also causes performance and scalability issues because you are making expensive database trips mostly even when there are no changes in data set.

SqlDependency is provided by Microsoft in SQL Server, and Oracle on Windows also supports it. SqlDependency allows you to specify a SQL statement, and SQL Server monitors this data set in the database for any additions, updates, or deletions and notifies you when this happens. But the problem with SqlDependency is that once it is fired, it gets unregistered from the database. Therefore, all future changes in your data set are missed and you don’t get notified.

Moreover, SqlDependency does not provide details of the record where change occurred. So in order to find the change in data set, you have to fetch the complete data set again instead of directly fetching only the specific record, which is added, updated or removed from data set. And, of course, this is not efficient.

In addition to the limitations of SqlDependency, your database is also not able to cope with the transactional demands of these high traffic real time applications where tens of thousands of queries are executed every second and database quickly becomes a scalability bottleneck. This is because although you can linearly scale your application-tier by adding more application servers, you cannot do the same with your database server.

This is where a distributed cache like NCache comes in because it allows you to cache data and reduce those expensive database trips which are causing scalability bottleneck.

NCache has a powerful Continuous Query capability that enables you to register an SQL-like query with the cache cluster. This Continuous Query remains active in cache cluster and if there is any change in data set of this query, then NCache notifies your real time application. This Continuous Query approach saves you from periodically executing the same expensive query against database to poll.

Here is sample code for NCache Continuous Query:

[code lang=”csharp”]
public void Main(string[] args)

String query = "SELECT NCacheQuerySample.Business.Product WHERE
this.ProductID > 100";

Hashtable values = new Hashtable();
values.Add("ProductID", 100);

onItemAdded = new ContinuousQueryItemAddedCallback(OnQueryItemAdded);
onItemUpdated = new ContinuousQueryItemUpdatedCallback(OnQueryItemUpdated);
onItemRemoved = new ContinuousQueryItemRemovedCallback(OnQueryItemRemoved);

ContinuousQuery query = new ContinuousQuery(queryString, values);

//data set item is removed
void OnQueryItemRemoved(string key){

Console.WriteLine("Removed key: {0}", key);

//data set item is updated
void OnQueryItemUpdated(string key){

Console.WriteLine("Updated key: {0}", key);

//data set item is removed
void OnQueryItemAdded(string key){

Console.WriteLine("Added key: {0}", key);


And, unlike SqlDependency, NCache Continuous Query remains active and doesn’t get unregistered on each change notification. So, your high traffic real time applications continue to be notified across multiple changes.

NCache Continuous Query also provides you the flexibility to be notified separately on ADD, UPDATE, and DELETE. And, you can specify these events at runtime even after creating a Continuous Query, something SqlDependency doesn’t allow you to do. This also reduces events traffic from the cache cluster to your real time application.

In summary, NCache provides you a very powerful event driven Continuous Query that no other database has. And, NCache is also linearly scalable for your high traffic real time applications.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Posted in Continuous Query, Distributed Cache, Distributed events | Tagged , , | Leave a comment

.NET and Java Data Sharing with Binary Serialization

Many organizations today use a variety of .NET and Java technologies to develop different high traffic applications. At the same time, these organizations have a need to share data at runtime between .NET and Java applications.

One way to share data is through the database, but that is slow and also doesn’t scale very well. A much better approach is to use an in-memory distributed cache as a common data store between multiple applications. It’s fast and also scales linearly.

As you know, Java and .NET types are not compatible. Therefore, you end up transforming the data into XML for sharing. Additionally, most distributed caches either don’t provide any built-in mechanism for sharing data between .NET and Java applications or only provide XML based data sharing. If a cache doesn’t provide a built-in data sharing mechanism, then you have to define the XML schema and use a third party XML serialization to construct and read all the XML data.

But, XML serialization is an extremely slow and resource hungry process. XML serialization involves XML validation, parsing, transformations which really hamper the application performance and uses extra resources in term of memory and CPU.

Distributed cache by design is used to improve your application performance and scalability. It allows your applications to cache your application data and reduce those expensive database trips that are causing a scalability bottleneck. And, XML based data sharing goes against these performance and scalability goals for your application. If you increase transaction load on your application, you’ll see that XML manipulation ends up becoming a performance bottleneck.

A much better way is to do data sharing between .NET and Java applications at binary level where you would not have to do any XML transformations. NCache is a distributed cache that provides you runtime data sharing .NET and Java application with binary serialization.

How does NCache provide runtime data sharing between .NET and Java?

Well, before that you need to understand why your native .NET and Java binary serialization are not compatible. Java and .NET have their own binary serializations that interpret objects in their own ways and which are totally different from each other and also have different type systems. Moreover, the serialized byte stream of an object also includes the data type details as fully qualified name which are again different in .NET and Java. This, of course, also hinders the data type compatibility between .NET and Java.

To handle this incompatibility, NCache has implemented its own interoperable binary serialization that is common for both .NET and Java. NCache interoperable binary serialization identifies objects based on type-ids that are consistent across .NET and Java instead of fully qualified name that are technology specific. This approach not only provides interoperability but also reduces the size of the generated byte stream. Secondly, NCache interoperable binary serialization implements a custom protocol that generates byte stream in such a format that its .NET and Java implementations can easily interpret.

Here is an example of NCache config.ncconf with data interoperable class mapping:

[code lang=”csharp”]
<cache-config name="InteropCache" inproc="False" config-id="0" last-modified=""
type="clustered-cache" auto-start="False">

<type id="1001" handle="Employee" portable="True">
<attribute name="Age" type="int" order="1"/>
<attribute name="Name" type="java.lang.String" order="2"/>
<attribute name="Salary" type="long" order="3"/>
<attribute name="Age" type="System.Int32" order="4"/>
<attribute name="Name" type="System.String" order="5"/>
<attribute name="Salary" type="System.Int64" order="6"/>
<class name="jdatainteroperability.Employee:0.0" handle-id="1"
assembly="jdatainteroperability.jar" type="java">
<attribute name="Age" type="int" order="1"/>
<attribute name="Name" type="java.lang.String" order="2"/>
<attribute name="Salary" type="long" order="3"/>
<class name="DataInteroperability.Employee:" handle-id="2"
assembly="DataInteroperability, Version=, Culture=neutral,
PublicKeyToken=null" type="net">
<attribute name="Age" type="System.Int32" order="1"/>
<attribute name="Name" type="System.String" order="2"/>
<attribute name="Salary" type="System.Int64" order="3"/>


As a result, NCache is able to serialize a .NET object and de-serialize it in Java as long as there is a compatible Java class available. This binary level serialization is more compact and much faster than any XML transformations.

Finally, the best part in all of this is that you don’t have to write any serialization code or make any code changes to your application in order to use this feature in NCache. NCache has implemented a runtime code generation mechanism, which generates the in-memory serialization and de-serialization code of your interoperable classes at runtime, and uses the compiled form so it is super fast.

In summary, using NCache you can scale and boost your application performance by avoiding the extremely slow and resource hungry XML serialization.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Posted in data sharing, Distributed Cache, Serialization | Tagged , , | Leave a comment

JSP Session Persistence and Replication with Distributed Cache

As you know, JSP applications have the concept of a Session object in order to handle multiple HTTP requests. This is because HTTP protocol is stateless and Session is used in maintaining user’s state across multiple HTTP requests.

In a single web server configuration, there is no issue but as soon as you have a multi-server load balanced web farm running a JSP application, you immediately run into the issue of where to keep the Session. This is because a load balancer ideally likes to route each HTTP request to the most appropriate web server. But, if the Session is kept only on one web server then you’re forced to use the sticky session feature of the load balancer where it sends requests related to a given Session only to the web server where the Session resides.

This means one web server might get overwhelmed even when you have other web server sitting idle because the Session for that user resides on that web server. This of course hampers scalability. Additionally, keeping the Session object only on one web server also means loss of Session data if that web server goes down for any reason.

To avoid this data loss problem, you must have a mechanism where Session is replicated to more than one web server. Here, the leading Servlet Engines (Tomcat, WebLogic, WebSphere, and JBoss) all provide some form of Session persistence. They even include some form of Session replication but all of them have issues. For example, file based and JDBC persistence are slow and cause performance and scalability issues. Session replication is also very weak because it replicates all sessions to all web servers thereby creating unnecessary copies of the Session when you have more than two web servers even though fault tolerance can be achieved with only two copies.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

In such situations, a Java distributed cache like NCache is your best bet to ensure the session persistence across multiple servers in web cluster is done very intelligently and without hampering your scalability.  NCache has a caching topology called “Partition-Replica” that not only provides you high availability and failover through replication but also provides you large in-memory session storage through data partitioning. Data partitioning enables you to cache large amounts of data by breaking up the cache into partitions and storing each partition on different cache servers in the cache cluster.

NCache Partition-Replica topology replicates the session data of each node to only one other node in cluster this approach eradicates the performance implications of replicating session data on all of the server nodes without compromising the reliability.

In addition, session persistence provided by the web servers (Apache, Tomcat, Weblogic and WebSphere) uses the resource and memory of web cluster. This approach hinders your application performance because the web cluster nodes which are responsible to process the client request are also handling the extra work of session replication and its in-memory storage.  Whereas, you can run NCache on separate boxes other the one part of your web cluster. This way you can free the web cluster resources and web cluster can use those resources to handle more and more client requests.

For JSP session persistence, NCache has implemented a session module NCacheSessionProvider as Java filter. NCache JSP Servlet Filter dynamically intercepts requests and responses and handles session persistence behind the scenes. And, you don’t have to change any of your JSP application code.

Here is a sample NCache JSP Servlet Filter configuration you need to define in your application deployment descriptor to use NCache for JSP Session persistence:

[code lang=”java”]

<filter-class> com.alachisoft.ncache.web.sessionstate.NSessionStoreProvider



Hence, NCache provide you a much better mechanism to achieve session persistence in web cluster along with performance boost and scalability.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Posted in Distributed Cache, Java, JSP Session Persistence | Tagged , , | Leave a comment

Scaling your Java Spring Applications with Distributed Cache

Spring is a popular lightweight dependency injection and aspect oriented development container and framework for Java. It reduces the overall complexity of J2EE development and provides high cohesion and loose coupling. Because of the benefits Spring provides, it is used by a lot of developers for creating high traffic small to enterprise level applications.

Here is an example of a Spring Java application:

[code lang=”java”]
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MySampleApp {

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//In spring.xml
Department dept = (Department) ctx.getBean"department");

List employees = dept.getEmployees();
for(int i=0; i< employees.size();i++)
Employee emp = employees.get(i);
System.out.println("Student Name :"student.getName());



But, these high traffic Spring applications face a major scalability problem. Although these applications can scale by adding more servers to the application server farm, their database server cannot scale in the same fashion to handle the growing transactional load.

In such situations, a Java distributed cache is your best bet to handle the database scalability problem. Java distributed cache offloads your database by reducing those expensive database trips that are causing scalability bottlenecks. And, it also improves your application performance by serving data from in-memory cache store instead of the database.

NCache is a Java distributed cache and has implemented the Spring cache provider. NCache Spring provider introduces a generic Java cache mechanism with which you can easily cache the output of your CPU intensive, time consuming, and database bound methods of Spring application. This approach not only reduces the database load but also reduces the number of method executions and improves application performance.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

NCache Spring provider has a set of annotations including @Cacheable to handle cache related tasks. Using these annotations you can easily mark the method required to be cached along with cache expiration, key generation and other strategies.

When a Spring method marked as @Cacheable is invoked, NCache checks the cache storage to verify whether the method has already been executed with the given set of parameters or not. If it has, then the results are returned from the cache. Otherwise, method is executed and its results are also cached. That is how, expensive CPU, I/O and database bound methods are executed only once and their results are reused without actually executing the method.

Java distributed cache is essentially a key-value store, therefore each method innovation should translate to a suitable unique key for the access. To generate these cache keys NCache Spring provider uses the combination of class name, method and arguments. However, you can also implement your custom NCache Spring key generator by using com.alachisoft.ncache.annotations.key.CacheKeyGenerator interface.

Here are the steps to integrate NCache cache provider in your application: 

  1. Add Cache Annotations: Add NCache Spring annotation to methods which require caching. Here is a sample of Spring method using NCache annotation:
  2. [code lang=”java”]
    slidingExpirator = @SlidingExpirator (expireAfter=15000)
    public Collection<Message>; findAllMessages()

    Collection<Message> values = messages.values();
    Set<Message> messages = new HashSet<Message>();
    synchronized (messages) {
    Iterator<Message> iterator = values.iterator();
    while (iterator.hasNext()) {

    return Collections.unmodifiableCollection(messages);


  3. Register NCache Spring Provider:  Update your Spring application spring.xml to register NCache Spring provider as follows:

[code lang=”java”]
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.alachisoft.com/ncache/annotations/spring http://www.alachisoft.com/ncache/annotations/spring/ncache-spring-1.0.xsd">

<ncache:cache id="r1-LocalCache" name="myCache"/>
<ncache:cache id="r2-DistributedCache" name="myDistributedCache"/>


Hence, by using NCache Spring cache provider you can scale your Spring applications linearly and can boost performance.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Posted in Distributed Cache, Spring | Tagged , | Leave a comment

Using NCache as Hibernate Second Level Java Cache

Hibernate is an Object-Relational Mapping library for Java language. It provides you mapping from Java classes to database table and reduces the overall development cycle. Because of benefits Hibernate provides, more and more high transactional applications are developed using Hibernate. Here is an example of Hibernate in a Java application.

[code lang=”csharp”]

import org.hibernate.*;

public class HibernateSample

Session session = factory.openSession();
session = factory.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer c");
Iterator it = query.list().iterator();
while (it.hasNext ()){
Customer customer = (Customer) it.next();


But, these high traffic Hibernate applications are encountering a major scalability issue. Although they are able to scale at application-tier level, their database or data storage is not able to scale with the growing number of transactional load.

Java distributed caching is the best technique to solve this problem because it reduces the expensive trips to database that is causing scalability bottlenecks. For this reason, Hibernate provides a caching infrastructure that includes first-level and second-level cache.

Hibernate first-level cache provides you a basic standalone (in-proc) cache which is associated with the Session object, and is limited to the current session only. But, the problem with Hibernate first-level cache is that it does not allow the sharing of object between different sessions. If the same object is required by different sessions all of them make the database trip to load it which eventually increases database traffic and causes scalability problem. Moreover, when the session is closed all the cache data is also lost and next time you have to fetch it again from database.

These high traffic Hibernate applications with only first-level when deployed in the web farm also faces across server cache synchronization problem. In web farm, each node runs a web server – Apache, Oracle WebLogic etc. – with multiple instances of httpd process to server the requests. And, Hibernate first-level cache in each httpd worker process has a different version of the same data cached directly from the database.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

That is why Hibernate provides you a second-level cache with provider model. Hibernate second-level cache allows you to plug-in 3rd party distributed (out-proc) caching provider to cache object across sessions and servers. Hibernate second-level cache is associated with SessionFactory object and is available to entire application, instead of single session.

When you enable Hibernate second-level cache, you end up with two caches; one is first-level cache and the other is second level cache. Hibernate always tries to retrieve the objects from first-level cache if fails tries to retrieve them from second-level cache. If that also fails then objects are directly loaded from the database and cached as well. This configuration significantly reduces the database traffic because most of the data is served by the second-level distributed cache.

NCache Java has implemented a Hibernate second-level caching provider by extending org.hibernate.cache.CacheProvider. You can easily plug in NCache Java Hibernate distributed caching provider with your Hibernate application without any code changes. NCache allows you to scale your Hibernate application to multi-server configurations without database becoming the bottleneck. NCache also provides you all the enterprise level distributed caching features like data size management, data synchronization across server and database etc.

You can plug in NCache Java Hibernate caching provider by simply modifying your hibernate.cfg.xml and ncache.xml as follows

[code lang=”csharp”]
<property name = "cache.provider_class">

<region name = "default">
<add key = "cacheName" value = "myClusterCache"/>
<add key = "enableCacheException" value = "false"/>
<class name = "hibernator.BLL.Customer">
<add key = "priority" value = "1"/>
<add key = "useAsync" value = "false"/>
<add key = "relativeExpiration" value = "180"/>


Hence, by using NCache Java Hibernate distributed cache provider you can linearly scale your Hibernate applications without any code changes.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Posted in Hibernate Second Level Cache, Java Distributed Cache | Tagged , | Leave a comment

Synchronize Distributed Cache with Database using CLR Stored Procedures

Distributed caching has become a very important part of any high transaction application in order to ensure that the database does not become a scalability bottleneck. But, since a distributed cache keeps a copy of your application data, you must always ensure that it is kept synchronized with your database. Without this, the distributed cache has older stale data that causes data integrity problems. SQL Server provides an event notification mechanism where the distributed cache like NCache can register itself for change notification through SqlCacheDependency and then receive notifications from SQL Server when underlying data changes in the database. This allows NCache to immediately invalidate or reload the corresponding cached item and this keeps the cache always synchronized with the database. However, SqlCacheDependency can become a very resource intensive way of synchronizing the cache with the database. First of all, you have to create a separate SqlCacheDependency for each cached item and this could easily go into tens of thousands if not hundreds of thousands. And, SQL Server uses data structures to maintain each SqlCachDependency separately so it can monitor any data changes related to it. And, this consumes a lot of extra resources and can easily choke the database server.

Secondly, SQL Server fires separate .NET events for each data change and NCache catches these events. And, these .NET events can be quite heavy and could easily overwhelm the network traffic and overall performance of NCache and your application. There is a better alternative. This involves you writing a CLR stored procedure that connects with NCache from within SQL Server and directly updates or invalidates the corresponding cached item. And, then you can call this CLR stored procedure from an update or delete trigger of your table. You can do this either with SQL Server 2005 or 2008 and also from Oracle 10g or later but only if it is running on Windows. A CLR stored procedure is more resource efficient because it is not creating data structures related to SqlCacheDependency. And, it also does not fire .NET events to NCache. Instead, it open up an NCache client connection and directly tells NCache whether to invalidate a cached item or reload it. And, this connection with NCache is highly optimized and much faster and lighter than .NET events.

Below is an example of how to use a CLR stored procedure.

  1. Copy log4net and protobuf-net from Windows GAC to NCache/bin/assembly/2.0 folder (choose 4.0 if the target platform is .NET 4.0).

2.   Register NCache and following assemblies in SQL server. Example is given below. In this example we are using Northwind as a sample database.

[code lang=”sql” toolbar=”false”]
use Northwind

alter database Northwind
set trustworthy on;

drop assembly SMdiagnostics
drop assembly [System.Web]
drop assembly [System.Messaging]
drop assembly [System.ServiceModel]
drop assembly [System.Management]

FROM N’C:WindowsMicrosoft.NETFrameworkv3.0Windows Communication FoundationSMdiagnostics.dll’
WITH permission_set = unsafe

FROM N’C:WindowsMicrosoft.NETFramework64v2.0.50727System.Web.dll’
WITH permission_set = unsafe

FROM N’C:WindowsMicrosoft.NETFramework64v2.0.50727System.management.dll’
WITH permission_set = unsafe

FROM N’C:WindowsMicrosoft.NETFrameworkv2.0.50727System.Messaging.dll’
WITH permission_set = unsafe

FROM N’C:Program Files (x86)Reference AssembliesMicrosoftFrameworkv3.0System.ServiceModel.dll’
WITH permission_set = unsafe

FROM N’C:Program FilesNCachebinassembly2.0Alachisoft.NCache.Web.dll’
WITH permission_set = unsafe

3. Open Visual Studio to write a stored procedure against NCache And create a SQL CLR Database project as mentioned below. Add a reference to the NCache assembly that you created in the last step. The assembly that you need to refer is highlighted above. It will appear under SQL Server with the same name as “NCache”.


4.  Write your stored procedure. Here is a sample code given:

[code lang=”csharp” toolbar=”false”]
public partial class StoredProcedures
public static void TestSProc(string cacheName)
//— Put your code here
SqlPipe sp = SqlContext.Pipe;

sp.Send(“Starting …..”);

if (string.IsNullOrEmpty(cacheName))
cacheName = “mycache”;

Cache _cache = NCache.InitializeCache(cacheName);
_cache.Insert(“key”, DateTime.Now.ToString());
sp.Send(“Test is completed …”);

5.  Enable CLR integration on database as given below:

[code lang=”csharp” toolbar=”false”]
sp_configure ‘clr enabled’, 1

6. Deploy the stored procedure from Visual Studio and test it.
7. After deploying the stored procedure, you need to place your stored procedure assembly in (C:Program FilesNCachebinassembly2.0) folder as it does not resolve assembly references directly from windows GAC folder and needs them locally.

CLR based stored procedures or triggers can greatly improve the application performance as compared to the SqlCacheDependency that is relatively slower and can be overwhelming for large datasets.

Download NCache Trial | NCache Details

Posted in CLR procedures, Database synchronize, Distributed caching | Tagged , , | 3 Comments

How Compact Object Serialization Speeds up Distributed Cache?

Serialization transforms an object into a byte-stream so it can be moved out of a process either for persistence or to be sent to another process. And de-serialization is the reverse process that transforms a byte-stream back into an object.

And, unlike a stand-alone cache, a distributed cache must serialize objects so it can send them to different computers in the cache cluster. But, the serialization mechanism provided by .NET framework has two major problems:

1.  Very slow: .NET Serialization uses Reflection to inspect type information at runtime. Reflection is an extremely slow process as compared to precompiled code.

2.  Very bulky: .NET Serialization stores complete class name, culture, assembly details, and references to other instances in member variables and all this makes the serialized byte-stream many times the original object in size.

Since a distributed cache is used to improve your application performance and scalability, anything hampering this becomes very critical. And, the regular .NET Serialization is a major performance overhead in a distributed cache because thousands of objects need to be serialized every second before being sent to distributed cache for in-memory storage. And, any slowdown here becomes a slowdown for the distributed cache.

The other issue is that a bulky serialized byte-stream consumes 2-3 times extra space and reduces the overall storage capacity of a distributed cache. An in memory storage can never be as big as a disk storage which makes this an even more sensitive issue for a distributed cache.

To overcome .NET serialization problems, NCache has implemented a Compact Serialization Framework. In this framework, NCache stores two-byte type-ids instead of fully qualified assembly/class names. It further reduces the serialized byte-stream by only serializing the field values and excluding their type details. Finally, NCache Compact Serialization Framework avoids the use of .NET Reflection because of its overhead by directly accessing fields and properties of the instance object.

There are two ways to use NCache Compact Serialization in your application.

  1. Let NCache generate Compact Serialization code at runtime
  2. Implement an ICompactSerializable interface yourself

In this blog, I will stick to first approach only. I’ll discuss the second approach in a separate blog.

Let NCache generate Compact Serialization code at runtime

Identify the types of objects you are caching, and register them with NCache as Compact Serialization types as shown in Figure. That is all you have to do, and NCache takes care of the rest.

Compact Serialization

Compact Serialization

 Figure 1: Register Types for Compact Serialization with NCache

NCache sends the registered types to NCache client at the time of initialization. Based on received types, NCache client generates the runtime code to serialize and deserialize each type. The runtime code is generated only once by the NCache client at the time of initialization and used over and over again. It runs much faster than Reflection based serialization.

Hence, using NCache Compact Serialization you can efficiently utilize your distributed cache memory and can boost your application performance.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Posted in Distributed Cache, Serialization | Tagged , | 1 Comment

Entity Framework Applications Using Distributed Cache

Entity Framework is an object-relational mapping engine that provides abstraction from underlying relational database and therefore greatly simplifies development. Because of these benefits, more and more data-centric and high transactional applications and services are developed with Entity Framework.

But, these high traffic applications are facing scalability problems. Although the application-tier level is scalable, their database or data storage cannot keep up with growing number of transactions being thrown at them.

This is where a distributed cache comes in because it allows you to cache data and reduce those expensive database trips that are causing scalability bottlenecks.  But, Entity Framework does not provide an out of the box solution that allows you to use distributed cache in your application. There are however two ways in which you can incorporate distributed cache into your Entity Framework application. One is to modify your Entity Framework application code and make direct API calls to the distributed cache. Second, is to use a distributed cache that has implemented a custom ADO.NET provider that incorporates caching behind-the-scenes.

Entity Framework has public provider model for ADO.NET providers where you can write providers for 3rd party databases. NCache has implemented a custom Entity Framework ADO.NET provider of its own through which it is able to make distributed cache calls to NCache API. This custom Entity Framework ADO.NET provider intercepts all the database query calls and puts the result-sets of these queries in a distributed cache. Then, NCache custom Entity Framework provider intercepts all subsequent query calls and simply returns the results from its distributed cache rather than making that expensive database trip. If result-set for a query does not exist in the distributed cache then query is executed against database and it’s result-set is then put in the distributed cache.

And, NCache custom Entity Framework provider also needs to ensure that data in the distributed cache is always consistent and synchronized with the database. And, for that NCache uses SqlCacheDependency provided in .NET. SqlCacheDependeny registers a SQL query with SQL Server so if any row in the dataset represented by this query is changed in the database, SQL Server throws a .NET event notification to NCache. NCache catches this .NET event and removes the corresponding result-set from the distributed cache.

Figure 1 shows how NCache Entity Framework Provider plugs into an Entity Framework application.

EF Caching

EF Caching

Figure 1 NCache Entity Framework Provider being used

 You can integrate NCache custom Entity Framework ADO.NET provider in your application in just four simple steps:

  1. Replace default provider: Replace your applications default provider with NCache Entity framework provider in app.config/web.config and .edmx file.
  2. Register NCache provider: Register your application in NCache Entity Framework config (efcaching.conf). In efcaching.config, you can easily specify log level and expiration policies etc. for your Entity Framework application.
  3. Run app in analysis mode: Run your application in analysis mode. In Analysis mode, NCache Entity Framework provider, logs Entity Framework queries executed by your application along with their frequency. Based on the logs you can scrutinize Entity Framework queries you want to cache.
  4. Run app normally: Switch to caching mode and run your application.

Hence, by using NCache Entity Framework caching provider you can easily achieve linear scalability without changing your Entity Framework application code.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Posted in 3rd party integrations, Distributed caching, Entity Framework | Tagged , | 2 Comments