Many organizations today use different .NET and Java technologies to develop various high-traffic applications. Thus, they need to share data between such technologies at runtime. Such data sharing is possible through databases, but is slow and doesn’t scale well. Alternatively, using in-memory distributed caches as a shared data store between multiple applications is much slower and linearly scalable.
XML Serialization: Necessity and Issues
But even with this approach, you must consider the incompatibility of .NET and Java data types. Therefore, you have to transform 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 offer a built-in data-sharing mechanism, you must define the XML schema and use third-party XML serialization to construct and read all the XML data.
Unfortunately, the XML serialization process is notably slow and resource-hungry. It involves XML validation, parsing, and transformations, hampering application performance and consuming extra memory and CPU resources.
Distributed caching aims to improve your application performance and scalability. It achieves this goal by allowing your applications to cache their data and reducing expensive database trips, causing scalability bottlenecks. XML-based data sharing is the antithesis of these goals. With increased application transaction loads, XML manipulation becomes a performance bottleneck.
Challenges of Using Native Binary Serialization for Data Sharing
Given all this, you could assume that binary-level serialization and data sharing are better options, avoiding any XML transformations. However, the native .NET and Java binary serialization are incompatible as they interpret objects differently, this is due to them having different type systems. Moreover, the serialized byte stream of an object also includes data type details, such as fully qualified names, which are dissimilar in .NET and Java, preventing data type compatibility between .NET and Java necessitating another alternative.
Runtime Data Sharing in NCache
Fortunately, NCache offers interoperable binary serialization that is common for both .NET and Java to handle these incompatibilities. It identifies objects based on type-ids consistent across .NET and Java instead of fully qualified names that are technology-specific. This approach not only provides interoperability but also reduces the size of the generated byte stream. As you can see in the all-attribute-list included under sharing-type as part of the <data-sharing> section in the config.ncconf as demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 |
… <all-attribute-list> <attribute name="_CustomerID" data-type="System.String" serialization-order="1"/> <attribute name="_CompanyName" data-type="System.String" serialization-order="2"/> <attribute name="_ContactName" data-type="System.String" serialization-order="3"/> <attribute name="_Country" data-type="System.String" serialization-order="4"/> <attribute name="CustomerID" data-type="java.lang.String" serialization-order="5"/> <attribute name="CompanyName" data-type="java.lang.String" serialization-order="6"/> <attribute name="ContactName" data-type="java.lang.String" serialization-order="7"/> <attribute name="Country" data-type="java.lang.String" serialization-order="8"/> </all-attribute-list> |
Secondly, NCache’s interoperable binary serialization implements a custom protocol that generates byte streams in a format that .NET and Java implementations can easily interpret. Here is an example of NCache config.ncconf with data interoperable class mapping. As shown below, first you need to declare the .NET classes as part of the sharing-class tag.
1 2 3 4 5 6 7 8 9 10 11 |
… <data-sharing> <sharing-type id="1001" handle="Customer" portable="True"> … <sharing-class name="Alachisoft.Sample.Data.Customer:1.0.0.0" class-handle-id="1" assembly-name="Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" language-platform="net"> <attribute name="CustomerID" data-type="System.String" serialization-order="1"/> <attribute name="CompanyName" data-type="System.String" serialization-order="2"/> <attribute name="ContactName" data-type="System.String" serialization-order="3"/> <attribute name="Country" data-type="System.String" serialization-order="4"/> </sharing-class> … |
Then you declare the java classes as part of the sharing-class tag as well, but as a separate section:
1 2 3 4 5 6 7 8 |
… <sharing-class name="com.alachisoft.sample.data.customer:0.0" class-handle-id="2" assembly-name="" language-platform="java"> <attribute name="customerID" data-type="java.lang.String" serialization-order="5"/> <attribute name="companyName" data-type="java.lang.String" serialization-order="6"/> <attribute name="contactName" data-type="java.lang.String" serialization-order="7"/> <attribute name="country" data-type="java.lang.String" serialization-order="8"/> </sharing-class> … |
Basically, NCache can serialize a .NET object and de-serialize it in Java with no code change as long as a compatible Java class is available. NCache facilitates a runtime code generation mechanism, for in-memory code serialization and de-serialization for your interoperable classes at runtime using the compiled form. Therefore, this binary-level serialization is more compact and faster than any XML transformation.
Conclusion
In summary, using NCache to share data between .NET and Java to scale and boost your application performance is incredibly efficient. It lets you avoid XML serialization, which is extremely slow and resource-hungry. So, what are you waiting for? Sign up for a fully working 30-day trial of NCache Enterprise and try it out for yourself.