Introduction
Recently, I have been involved in a project which requires better ser./des. capabilities in consideration of message size packing. In general, we need to transfer data over wire (basically it's HTTP) in representation of bulk portion of messages, which is not so big in its size.
JSON is a fantastic format, anywhere people in your organization want to reach for XML. So as a result, the first thought that comes to my mind was usage of Json.NET (Newtonsoft.net), but after some investigation, I have discovered that from size packing consideration JSON.NET is not so compact.
MessagePack Comes to the Rescue
MessagePack
is an interoperable binary encoding format. You can reduce the size of stream dramatically when you have a data of which contains many binary (for example, numeric) data.
MessagePack
is faster to serialize binary data such as thumbnail images. MessagePack
is better to reduce overheads to exchange small objects between servers. JSON is better to use with browsers.
If you ever wished to use JSON for convenience (storing an image with metadata) but could not for technical reasons (encoding, size, speed...), MessagePack
is a perfect replacement.
MessagePack
is effectively JSON, but with efficient binary encoding. Like JSON, there is no type checking or schemas, which depending on your application can be either a pro or a con. But, if you are already streaming JSON via an API or using it for storage, then MessagePack
can be a drop-in replacement.
So, let's summarize and provide some pros:
- JSON Compatible: Anything that works with JSON will work with
MessagePack
. - More space efficient:
MessagePack
uses an extremely efficient binary serialization format, for things like numbers and binary data MessagePack
can be hugely more efficient. For use in persisting datastructures in something like Redis, where you want to be careful with memory usage, this is quite useful. - Faster:
MessagePack
is usually much faster to encode / decode than JSON. - Supports Your Language: Ruby, Python, Perl, Javascript, PHP, Java, C++, C#,Go, Erlang, Haskell, OCaml, Scala, Clojure, and more all support
MessagePack
. - RPC: There's a separate
MessagePack
RPC project that maintains a high performance MessagePack
based RPC server and client available in most of the languages above.
Using the Code
NOTE: To use the code below, just add a reference to MsgPack.dll.
So let's pretend that we have some class called Foo
, in such case for serialization and deserialization code will be next:
using(var stream =new MemoryStream()){
var serializer = MessagePackSerializer.Create<Foo>();
serializer.Pack(foo, stream);
stream.Position = 0;
var value = serializer.Unpack(stream);
}
Serialization Rules in WCF
For transferring data over the wire and successfully deserializing it on a client side, use the next approach (relevant for WCF, and another type of RPC).
[DataContract]
public class Person
{
[MessagePackMember]
public int ID{get;set;}
[MessagePackMember]
public string Name{get;set;}
[NonSerialized]
public int Age {get;set;}
}
Performance
According to the vendors information, from performance consideration MessagePack
is 4 times faster than protobuff. To be honest, I do not believe in it. But what I know exactly is that it's much faster, performs significantly better with the fields in class, - over 10% faster in ser/des in comparison with protobuf.
Points of Interest
In future, I am planning to write some more tips about MessagePack
and especially usage of MessagePack
in conjunction with ASP.NET Web API.
Original link http://msgpack.org/.