resilient-http / resilient.js

Fault tolerant and reactive HTTP client for node.js and browsers
184 stars 13 forks source link

Not balancing #136

Closed danwkennedy closed 8 years ago

danwkennedy commented 8 years ago

Either I'm not configuring it right or the balancer doesn't actually balance the load between the servers.

My setup is the following:

When I start the 3 consumers, they each latch onto one of the two service servers and never let go. In the case below, two consumers latch onto one node in the service while the third latches onto the second (which I guess is a bit better than all three on one but theoretically worse than a simple randomization strategy).

Consumer 1:

Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.69.231:32778/sales-record
Calling out to: http://172.31.69.231:32778/sales-record
Calling out to: http://172.31.69.231:32778/sales-record
Calling out to: http://172.31.69.231:32778/sales-record
Calling out to: http://172.31.69.231:32778/sales-record
Calling out to: http://172.31.69.231:32778/sales-record
Calling out to: http://172.31.69.231:32778/sales-record

Consumer 2:

Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.69.231:32778/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record

Consumer 3:

Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.69.231:32778/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record
Calling out to: http://172.31.65.172:32781/sales-record

The configuration used is as follows:

let client = Resilient({
    service: {
      headers: {
        <custom auth headers>
      },
      json: true
    },
    balancer: {
      roundRobin: true
    }
  });

client.use(resilientConsul({
    service: 'ms-service',
    servers: [
      `<single Consul server>`
    ],
    onlyHealthy: true
  }));

client.on('request:outgoing', opts => {
    console.log(`Calling out to: ${ opts.url }`);
  });

The behavior is the same for the default balancer options as well. Since basic client-side load balancing was the goal of importing this project, this is a deal breaker for me.

h2non commented 8 years ago

This should be fixed in version 0.3.4. Feel free to update your dependency tree.

Moreover, 2 minor features were added to resilient.

You can enable the random balancer simply enabling the option, such as:

var client = Resilient({
  balancer: {
    random: true
  }
})

Additionally, if you want to use a custom balancer strategy, you can use it via balanceStrategy option:

var client = Resilient({
  balancer: {
    balanceStrategy: servers => {
       return servers.sort(myCustomSorter)
    }
  }
})

For more details, take a look the docs: https://github.com/resilient-http/resilient.js#balancer

danwkennedy commented 8 years ago

Works like a charm now. Thanks for the quick turnaround!