[nanomsg] Load balancing vs round robin

  • From: "Andrew Whyte" <andrew.whyte@xxxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Thu, 15 Jan 2015 18:05:29 -0000 (GMT)

Hi, I am trying to load-balance messages using PIPELINE (push/pull) in a
situation with two workers and one producer. If one of the workers is
processing a very long task, I would like the load balancer to see it as
"busy" (e.g. not "recv"-ing new jobs) and route subsequent messages to the
other worker.

The current implementation does a simple round robin, such that every
subsequent even-numbered message gets backed up on the busy worker until
it is free, with the other worker seeing only the odd-numbered messages.

Here is some C code that demonstrates the issue:

// sender.c
#include <stdio.h>
#include <nanomsg/nn.h>
#include <nanomsg/pipeline.h>

int main ()
{
  int push = nn_socket (AF_SP, NN_PUSH);
  nn_bind (push, "tcp://127.0.0.1:5560");

  nn_send (push, "9", 1, 0);
  sleep (2);
  nn_send (push, "1", 1, 0);
  sleep (2);
  nn_send (push, "1", 1, 0);
  sleep (2);
}

// worker.c
#include <stdio.h>
#include <nanomsg/nn.h>
#include <nanomsg/pipeline.h>

int main ()
{
  int pull = nn_socket (AF_SP, NN_PULL);
  nn_connect (pull, "tcp://127.0.0.1:5560");

  while (1)
  {
    char *buf = NULL;
    int nbytes = nn_recv (pull, &buf, NN_MSG, 0);

    printf ("%s\n", buf);
    nn_freemsg (buf);
    sleep (atoi(buf));
  }
}

The output on worker A is always {"9", "1"}, whereas I am aiming to see
only {"9"} on worker A and {"1", "1"} on worker B.

This behaviour is alluded to in the early blog post
[http://250bpm.com/blog:14] but I appreciate things may have changed since
back then.

Is there a simple way to avoid this problem in the current library?

Thanks very much,

Andrew


Other related posts:

  • » [nanomsg] Load balancing vs round robin - Andrew Whyte