Asynchronous Majordomo Pattern
The Majordomo implementation in the previous section is simple and stupid. The client is just the original Simple Pirate, wrapped up in a sexy API. When I fire up a client, broker, and worker on a test box, it can process 100,000 requests in about 14 seconds. That is partially due to the code, which cheerfully copies message frames around as if CPU cycles were free. But the real problem is that we’re doing network round-trips. ØMQ disables Nagle’s algorithm, but round-tripping is still slow.
Theory is great in theory, but in practice, practice is better. Let’s measure the actual cost of round-tripping with a simple test program. This sends a bunch of messages, first waiting for a reply to each message, and second as a batch, reading all the replies back as a batch. Both approaches do the same work, but they give very different results. We mock up a client, broker, and worker. The client task is shown in Example 4-46.
Example 4-46. Round-trip demonstrator (tripping.c)
//// Round-trip demonstrator//// While this example runs in a single process, that is just to make// it easier to start and stop the example. The client task signals to// main when it's ready.//#include "czmq.h"staticvoidclient_task(void*args,zctx_t*ctx,void*pipe){void*client=zsocket_new(ctx,ZMQ_DEALER);zsocket_connect(client,"tcp://localhost:5555");printf("Setting up test...\n");zclock_sleep(100);intrequests;int64_tstart;printf("Synchronous round-trip ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access