Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ + Protocol Buffers

ZeroMQ FAQ page suggest use of Google's protobuf as a way to serialise message content.

Has anyone see a good usage example?

I also need to get the answer to "What is the biggest advantage of serialising messages?" - whether it may be something I can live without and take the advantage of slimmer pipeline.

I quite like the idea of .proto files and the protoc compiler.

Also, it seem that another great tool to throw at the playground would be libev, any comments are welcome :)

like image 645
errordeveloper Avatar asked Sep 12 '11 15:09

errordeveloper


People also ask

Can you convert Protobuf to JSON?

A Printer converts protobuf message to JSON format. A TypeRegistry is used to resolve Any messages in the JSON conversion.

Is ZeroMQ an RPC?

RPCZ is built on top of ZeroMQ for handling the low-level I/O details in a lock-free manner. The Python module is a Cython wrapper around the C++ API.

What is the difference between proto2 and Proto3?

Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.

What is Zmq message?

ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is an asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker; the zero in the name is for zero broker.


2 Answers

If you are 100% certain that the programs that are going to communicate over ZMQ will at all times be capable of understanding each other's binary format (eg because they are always distributed together and were all compiled with the same compiler options anyways) I see no benefit to the overhead that's added by serialization.

As soon as the above condition cannot be satisfied (like partner programs running on different host types, programs written in different languages or even partner programs that can evolve independently in time - which may cause incompatibilities in their raw binary structures) serialization becomes quite probably a must.

It seems that nowadays everybody and their brother is creating serialization solutions, which may be an indication that there's no one size fits all solution. This page contains a pretty thorough benchmarking of serialization time, deserialization time and sizes for 27 (!!) different serialization systems. Don't skip the first paragraph of that page, it says "Warning, benchmarks can be misleading". Your application, your data are what counts for you, but the data presented there may help you narrow down the choices you want to study in detail.

like image 85
fvu Avatar answered Sep 18 '22 17:09

fvu


Here is a sample which send and receive messages through java and in C++:

Serializing in java:

Person person = Person.newBuilder().setName("chand")     .setEmail("[email protected]").setId(55555).build(); socket.send(person.toByteArray(), 0); 

De-serialize in java:

byte[] reply = socket.recv(0); Person person2 = Person.parseFrom(reply); 

Serializing in C++:

Person p = Person(); std::string str; p.SerializeToString(&str); int sz = str.length(); zmq::message_t *query = new message_t(sz); memcpy(query->data (), str.c_str(), sz); socket->send (*query); 

De-serializign in C++

zmq::message_t resultset(100); socket->recv (&resultset);  Person p = Person(); p.ParseFromArray(resultset.data(), resultset.size()); printf("\n Server : %s", p.name().c_str()); 
like image 24
Chand Priyankara Avatar answered Sep 18 '22 17:09

Chand Priyankara