Serialization Format
Cache data when transmitted over the network needs to be serialized. The serialization process traverses the existing objects, extracts the data and converts it into the serialized data. Data is then transmitted over the network and then the process of deserialization reads the serialized. This whole process of serialization/deserialization requires a lot of factors like time, memory and performance etc. These factors are to be kept in mind when choosing a format for serialization.
There are two types of serialization formats provided by NCache:
- Binary Serialization
JSON Serialization
Note
It is recommended that you use JSON serialization if you are using ASP.NET 5.0 and above. For more details click here.
Binary Serialization
In the binary format, the user objects (e.g. Product object) are serialized from the client end and stored on the server side in the same form. Every time an item is requested from the server, the client receives the binary form of the item which is then de-serialized to Product object locally. Furthermore, serialization/deserialization takes place on the client side only, and just once either while fetching or adding the data – de-serialized while fetching and serialized while adding. This saves cost of serialization/de-serialization, which is noticeable especially in cases where sizeable data is added or fetched from the cache.
Binary Format is beneficial if most of your processing is on the client side, and the operations performed are like add, update, fetch, and remove from the cache. For example, a serialized Product object is fetched from the cache to display its contents. Using binary format, the item will only need to be de-serialized once it reaches the client, thus, keeping the processing cost to a minimum. In case Object format is being used, the server will serialize the Product object and send it to the client which will then de-serialize it. This increases the overall overhead and cost of serialization and de-serialization.
Similarly, if an object is being added to the cache using Binary format, it will be serialized before it is sent over the network and it will be stored as it is.
JSON Serialization
Note
This feature is available in NCache Enterprise and Professional editions.
JSON is text based syntax for storing and exchanging data. It is fairly an easier format for the machines to parse and for the users to understand. Using JSON serialization, the objects are converted into their JSON equivalent when serialized and then converted back into their custom objects at the time of deserialization.
Consider a .NET Product class containing the data of products. It contains various attributes e.g ProductName, ProductID and ProductExpiry etc.
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
This data will be serialized to the following JSON string:
{
"Name": "Apple",
"Expiry": "2008-12-28T00:00:00",
"Sizes":
[
"Small"
]
}
NCache provides you with an ease of serializing your data in JSON (irrespective of the topology) according to JSON serialization. The serializer used is an extension of NewtonSoft 3rd party serializer. It can be a preferable approach for compact and convenient serialization of data. JSON serialization can be preferred over Binary Serialization due to following reasons:
Memory Efficient: Memory is one of the major parameters kept in mind when using large data sets. Consumption of larger memory affects other performance measures. JSON serialization affects lesser memory for storing the objects. The entire data set is serialized as JSON and results in lesser consumption of memory due to significantly smaller size thus improving the network communication.
Portability: JSON serialization provides higher portability since it provides you with the ease of serializing the data of various technologies into the same standard. JSON serializer serializes data of all types. For example, if the user is using Java class, JSON serializer will serialize the data in the form of a string and deserialize it in the type defined by the user. This level of portability is not provided by binary serialization.
No code change: Since the serialization according to the JSON format is fully automated, there is no code change required by the user unless you require code change according to your own logic.
Non-Serializable Attributes: You are also provided with the choice of serializing the selective attributes and keeping the attributes of your choice non-serialized by using a method called “JsonIgnore” in your class. Which means that the serializer will ignore these attributes while serializing the data and serialize the rest. This can be done only with the attributes of a custom class. Please refer to the NewtonSoft documentation for details. For example if the user wants to keep the attribute Password as non-serialized,
JsonIgnore
will mark it as non-serializable.
using Newtonsoft.Json;
[JsonIgnore]
public String Password()
{
get;
set;
}
Given below is an example of a custom class with a non-serializable marked attribute Password. After serialization the serialized JSON string will not contain the value of the non-serialized attribute
Account account = new Account
{
FullName = "Joe User",
EmailAddress = "joe@example.com",
Password = "VHdlZXQgJ1F1aWNrc2lsdmVyJyB0byBASmFtZXNOSw=="
};
The above data will be serialized to the following JSON string.
{
"FullName":"Joe User",
"EmailAddress":"joe@example.com"
}
Keeping in mind the above factors JSON serialization can result in achieving a higher level of performance. However, if you want to obtain accuracy at a higher level, use binary serialization instead of using JSON serialization since Binary serialized data is deserialized more accurately.
See Also
Dynamic Compact Serialization
Register Classes for Compact Serialization
Bridge For WAN Replication
Data Load Balancing