MessagePack-RPC for C++

What’s MessagePack-RPC?

MessagePack-RPC is a inter-process messaging system that is similar to Facebook’s thrift, Apache avro or Google’s Protocol Buffers. It uses MessagePack format for object serialization and passes serialized object across network connection.

MessagePack is cross-language serialization format and MessagePack-RPC enables you to exchange messages between programs implemented in heterogeneous languages.

Today, I want to introduce the C++ implementation of MessagePack-RPC. Its most important characteristics are (1) High-performance, (2) concept of “Future” and (3) Thread-safe connection pool.

(1) High-Performance

One of the most important factors of the performance is parallel processing because even cheap machines have multiple cores today. MessagePack-RPC for C++ has an intelligent parallel processing mechanism that other libraries (Thrift, Avro, Protocol Buffers, etc.) don’t have.

It processes all messages in parallel even if the messages are reached on single connection.

This characteristic is particularly effective in the cluster application that servers communicate mutually.

The following graph shows you the effectiveness of the parallel processing.

(2) Future and Asynchronous call

MessagePack-RPC for C++ provides flexible asynchronous processing model supported by concept of Future. Future describes an object that acts as a proxy for a result that is initially not known (Wikipedia).

The following code shows the example.

#include <msgpack/rpc/client.h>
#include <string>
#include <iostream>

using namespace std;
using namespace msgpack::rpc;

int main(void)
    // create an client instance
    client c("", 9090);
    std::string text = ...;

    // send some heavy request and receives its future result.
    future f1 ="analyze", text);
    // the server starts to process this request.

    // send other requests.
    future f2 ="count", text);
    // the server starts to process this request on another CPU core.

    // receive actual result of request that will be finished first.
    int result2 = f2.get<int>();

    // you can do some calcuation while server processing first heavy request ...

    // then receive result of the heavy request.
    string result1 = f1.get<string>();

    std::cout << result1 << std::endl;
    std::cout << result2 << std::endl;

(3) Thread-safe connection pool

MessagePack-RPC for C++ has connection pooling feature. Its peculiar advantage is thread safety. You can share one pooled connection with multiple threads.

The following multithreaded client program is valid and it establishes only one connection.

#include <msgpack/rpc/client.h>
#include <msgpack/rpc/session_pool.h>
#include <mp/pthread.h>

void thread_1(session_pool* sp) {
    // do RPC on pooled connection
    msgpack::rpc::session s = sp->get_session("", 9090);
    int result ="add", 1, 2).get<int>();

void thread_2(msgpack::rpc::session_pool* sp) {
    // do RPC on pooled connection
    msgpack::rpc::session s = sp->get_session("", 9090);
    int result ="add", 2, 3).get<int>();

int main(void) {
    // create thread-safe connection pool
    msgpack::rpc::session_pool sp;
    sp.start(4);  // run 4 worker threads

    // run 2 client threads for example
    mp::pthread_thread t1;
    mp::pthread_thread t2; mp::bind(thread_1, &sp) ); mp::bind(thread_2, &sp) );


Starting with MessagePack-RPC

The Ruby implementation is good for first. It’s very easy to use and suitable for prototyping, while it is full-featured that has concept of Future and connection pool.

There are simple implementation of distributed key-value storage on my simple-kvs repository at github. Though it has replication and load-balancing features, it is implemented in only 54 lines.


If you have any question, please contact with @frsyuki_ha or @kzk_mover with #msgpack hash tag, or #msgpack on, or project mailing list. See also the project web site.


About frsyuki

Twitter account is @frsyuki_ha.
This entry was posted in C++, MessagePack-RPC. Bookmark the permalink.

8 Responses to MessagePack-RPC for C++

  1. Pingback: Tweets that mention I just posted new entry on MessagePack Blog. #msgpack --

  2. Anders Dalvander says:

    Why was mpio/pthread and raw sockets chosen instead of Boost.Threads and Boost.Asio which are more portable? Boost is the defacto standard for C++ development. Pthreads doesn’t work natively on Windows and WinSock isn’t exactly the same as raw sockets. Not working on Windows is a major drawback, especially for a cross platform library.

    • IMHO,
      Boost is often used for development, but less often for library development.
      It is a heavy set of libraries and not everyone would like to install more than 9000 c++ header files just to run a light-weight RPC. The code is tightly interdependent, a simple feature (I’ve used an iterator_facade just yesterday) might pull a lot of headers, slowing down the compilation and stressing the compiler with hard-to-fix compatibility issues. ASIO is overly complex as well, I’ve tried it but resorted to writing libevent wrappers.
      Also note that there are other general-purpose libraries, like Poco C++. If one uses Poco C++ one would almost never want to pull another large dependency such as Boost.
      In a way, giants such as Boost or Poco are the artefacts from the slow language evolution and lack of decent package management (such as Perl’s CPAN, Haskell’s Hackage or Erlang’s and Google Go’s ability to automatically fetch dependencies from github).
      A library not dependent on Boost, such as libcppa or MessagePack, is a welcome thing, even though I use Boost myself.

  3. cxwangyi says:

    Is there an example on writing servers (in addition to those example clients)?

  4. Pingback: Learning Go: RPC between Go and C/C++/Ruby/etc « Yi Wang's Tech Notes

  5. Familia says:


    I think this library is great and really easy to work with, i have one question, is there any possibility to compile it in windows as is? And if not, do you think that the new c++11 standard could help in the transition to windows (of course i am not talking about the sockets part), even if not, do you think theses changes, mainly in the atomic variables could even make msgpack-rpc faster and lighter?


  6. FogAn says:

    Great work.
    Any possibility to compile this on windows?

  7. І aⅼways used tto study articlе in news pappers but now
    as I am a ᥙsser of inteгnet thus from noᴡ I am using net foor articles, tɦanks to web.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s