Professional Documents
Culture Documents
MessagePack
Sadayuki Furuhashi
What s MessagePack?
Efcient serialization library
Rich data structures - compatible with JSON Dynamic typing Synchronous, Asynchronous and Callback style Concurrent calls with multiple servers Event-driven I/O Interface Denition Language (IDL) - compatible with Thrift
Ecient Serialization
1. Compact
Binary-based format Embed type information
2. Fast
Zero-copy (C++) Stream deserialization
Format of MessagePack
JSON null Integer Array String Map
null 10 [20] 30 {40:null}
MessagePack
c0 0a 91 14 a2 3 0 81 a1 4 0 c0
Format of MessagePack
JSON null Integer Array String Map
null 10 [20] 30
MessagePack
c0 0a 91 14
a2 3 0 bytes 3 81 a1 4 0 c0 5 bytes
{40:null}bytes 11
Format of MessagePack
Fixed length types
Integer Floating point Boolean Nil
type value
Type information
Type information
Type information Types
0x00 0xc2 0xc3 0xca 0xcb 0xcc 0xcd
0xc0 0xe0
nil false true float double uint8 uint16 uint32 uint64 int8 ...
Embed value
Type information
0x00
Types
0x00 0xc2 nil false true float double uint8 uint16 uint32 uint64 int8 ...
Positive FixNum
0x80 0x90 0xa0 0xc0 0xe0
FixMap FixRaw
FixArray
Negative FixNum
Zero-copy serialization
Zero-copy deserialization
Performance
It measured the elapsed time of serializing and deserializing 200,000 target objects. The target object consists of the three integers and 512 bytes string.
Synchronous call
require 'msgpack/rpc' client = MessagePack::RPC::Client.new(host, port) result = client.call(:method, arg1, arg2)
Asynchronous call
require 'msgpack/rpc' client = MessagePack::RPC::Client.new(host, port) future1 = client.call_async(:methodA, arg1, arg2) future2 = client.call_async(:methodB, arg1, arg2) result1 = future1.get result2 = future2.get
Callback
require 'msgpack/rpc' client = MessagePack::RPC::Client.new(host, port) client.callback(:method, arg, arg2) do |future| result = future.get end client.join
Connection Pooling
require 'msgpack/rpc' sp = MessagePack::RPC::SessionPool.new session1 = sp.get_session(host1, port1) session2 = sp.get_session(host2, port2) future1 = session1.call_async(:methodA, arg1, arg2) future2 = session2.call_async(:methodB, arg1, arg2) result1 = future1.get result2 = future2.get
Server
Client Session
Server
Connection Pooling
Session
Session
Server architecture
Client
Client
Event-driven I/O
Performance of the Loop is important. Java version uses Netty (JBoss I/O framework) Multithreaded Utilizes Javas NIO C++ version uses mpio (Kumofs I/O architecture) Multithreaded Utilizes epoll or kqueue Ruby version uses Rev (libev for Ruby) Utilizes epoll or kqueue