As the previouse 2 articles were about optimizing performance and keeping the server cost as low as possible, this article will also cover this area and write about why I’m using MsgPack instead of JSON in ZeroPilot.
In a web-app there is allways data sent between the client and the server – would you believe me if I told you that after otimizing all the server-calls, bundeling them together ect. you can still save up to 40% (and some cases even 60%) of your traffic caused by client-server communication with not more than one line of code?
Well, the magic word is “MsgPack” – it is a ‘binary-based efficient object serialization library‘. The difference to JSON is, that MsgPack is binary-based – this gives the possibility to make the exchanged data a) smaller and use less bytes, I guess we all know the advantages of that, however there is an even bigger advantage: b) It is faster to parse and encode, having a parser parse 40 bytes takes about twice as long as parsing 20 bytes. I know those advantages might seem marginal – however if you scale this up to a couple thousand (or even more) users you will see quite a bit of a difference there. And even if you don’t have a couple thousand users – why would you not want to take advantage of something like this?
To make this difference more visible i put together a small example:
7B 22 6E 61 6D 65 22 3A 22 4A 6F 68 6E 20 44 6F 65 22 2C 22 61 67 65 22 3A 20 31 32 7D
82 A4 6E 61 6D 65 A8 4A 6F 68 6E 20 44 6F 65 A3 61 67 65 0C
As you can see, the MsgPack-encoded data is about 2/3 the size of JSON.
(edit: I previously had some unnecessary bytes in the JSON code, making the example to much in favour of MsgPack, I removed those. thx to charles leifer for noticing)
Additionally, the whole thing look less readable when you look at it – so as a small bonus this will probably repell some script-kiddies, who are trying to intercept your JSON- or XML-calls.
One line of code you said?
//just exchange the encoding and decoding lines
myJSONString = JSON.stringify(myObject);
myObject = JSON.parse(myJSONString);
var myByteArray = msgpack.pack(myObject);
myObject = msgpack.unpack(myByteArray);
MsgPack returns Objects/Hashes or Arrays, Numbers and Strings just like JSON will do, so there is NO need to change anything further than the encoding- and decoding lines.
Why havn’t I heared from MsgPack before?
As with all good things: They need time, MsgPack has first been introduced about 2 1/2 years ago I believe (at least according to their github-repo) – it might be older, but without a github repository it is rather difficult to get noticed nowadays.
I can only encourage you to visit the MsgPack-Site and download the implementation for your programming-language(every major language has one), even if your desired language has no official implementation, there are usually unofficial projects to be found via google or github. If you can’t find it – contribute to the community and write your own implementation and share it.
Edit #2: Since there was quite a bit of critisism here and on Hacker News, Sadayuki “Sada” Furuhashi, the creator of MessagePack posted some thoughts on MessagePack on gist: https://gist.github.com/2908191 clarifying a few things, thank you for that Sadayuki.