Serialization Format - Binary & JSON Serialization
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 the process of deserialization reads the serialized data. This whole process of serialization/deserialization requires a lot of resources like time, memory, and performance, etc. These factors are to be kept in mind when choosing a format for serialization.
Note
This feature is also available in NCache Professional.
There are two types of serialization formats provided by NCache:
- Binary Serialization
- JSON Serialization
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 deserialized to the Product object, locally. Furthermore, serialization/deserialization takes place on the client-side only, and just once either while fetching or adding the data – deserialized, while fetching and serialized, while adding. This saves the cost of serialization/deserialization, 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 only needs 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 deserialize it. This increases the overall overhead and cost of serialization and deserialization.
Similarly, if an object is being added to the cache using the Binary Format, it will be serialized before it is sent over the network and it will be stored as it is.
JSON Serialization
Note
Ensure that you are using the JSON Serialization format for .NET 8.0, as BinaryFormatter Serialization methods are obsolete and prohibited in .NET 8.0. However, if you still want to use Binary Serialization, you can suppress the BinaryFormatter, as discussed here.
Note
This feature is available in NCache Enterprise and Professional.
JSON is the 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 = "Ensure";
product.Expiry = new DateTime(2008, 12, 28);
This data will be serialized to the following JSON string:
{
"Product": {
"name": "Ensure",
"id": "1164",
"price": "20",
"details": {
"deliveryLocation": "US",
"manufacturedBy": "Mitchels"
}
}
}
The serializer used is an extension of the 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 for the 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 less memory for storing the objects. The entire data set is serialized as JSON and results in reduced consumption of memory due to significantly smaller size, thus, improving 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 the 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. This 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 factors above, 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 JSON serialization since binary serialized data is deserialized more accurately.
See Also
Compact Serialization
Register Classes for Compact Serialization
Bridge For Geo Replication
Data Load Balancing