nest icon indicating copy to clipboard operation
nest copied to clipboard

microservices module in cluster/load balancing environment

Open roeehershko opened this issue 8 years ago • 10 comments

I'm submitting a...


[ ] Regression 
[ ] Bug report
[*] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

This is more of a discussion, currently the microservices module works good as long as you have 1 process, but once you add load balancing or clustering (which is a must in microservices applications) it become harder to work with since messages will but fired to all the services even if they are the same services under a loadbalancer, the protocol doesnt know that. some of the messaging protocols have a way around it such as MQTT "group" mode but not sure that redis/nats has one. i can just create mqtt transport that support the groups mode, but you think its better to provide support for this in nest? it is microservice transport protocol and load balancing is a big part of it

roeehershko avatar Jun 02 '18 19:06 roeehershko

+1

zuohuadong avatar Jul 11 '18 07:07 zuohuadong

+1

phantomk avatar Jul 28 '18 03:07 phantomk

I was able to get load balancing working using NATS queuing.

https://nats.io/documentation/concepts/nats-queueing/

triedal avatar Sep 27 '18 20:09 triedal

I was able to get load balancing working using NATS queuing.

https://nats.io/documentation/concepts/nats-queueing/

@triedal How have you managed to pass queue name to the Nest's nats transport?

According to the docs:

To create a queue subscription, subscribers register a queue name. All subscribers with the same queue name form the queue group. As messages on the registered subject are published, one member of the group is chosen randomly to receive the message. Although queue groups have multiple subscribers, each message is consumed by only one.

rychkog avatar Oct 04 '18 15:10 rychkog

@kamilmysliwiec I haven't found this in docs nor in source code, but maybe I just wasn't attentive enough Is there a way to pass this information (nats queue name) to the nats transport in order to make a queue group for messages load balancing? Because right now all of them (instances of same microservice) receive this message. So let's imagine that I have Auth microservice, then all of its instances will receive login request....

rychkog avatar Oct 05 '18 07:10 rychkog

@rychkog my situation is a little unique in that I built my own custom NATS transport. My services are running NestJS v4 which did not include a NATS transport.

  public async listen(callback: () => void) {
    const patterns: [MessagePattern] = Object.keys(this.messageHandlers).map(
      pattern => JSON.parse(pattern)
    ) as any;

    patterns.forEach((pattern: MessagePattern) => {
      const natsOpts = {
        queue: `queue.${pattern.topic}`
      };
      const sid = this.nc.subscribe(pattern.topic, natsOpts, (msg, replyTo) =>
        this.handleMessage(JSON.parse(msg, dateReviver), replyTo)
      );
      this.subscriberIds.push(sid);
    });
    callback && callback();
  }

Here is the code that subscribes my queue group members to the NATS cluster. I make each message topic into a queue group. So a micro listening to { topic: 'math', cmd: 'add' } would be subscribed to the math queue group.

triedal avatar Oct 09 '18 15:10 triedal

@triedal Thanks for sharing this! This helps a lot. I'll try to create my own then.

rychkog avatar Oct 10 '18 08:10 rychkog

Once this https://github.com/nestjs/nest/pull/1175 PR gets merged, it will be possible to load balance messages across multiple instances. However, it still won't enable you to subscribe to different queues on different topics (which might be achieved using custom strategy).

kamilmysliwiec avatar Oct 17 '18 16:10 kamilmysliwiec

In 5.4.0 RabbitMQ (with queues) has been added. Also, it's possible to set a queue in Nats strategy now. In terms of the TCP, MQTT, and Redis, there is no built-in load balancer available out-of-the-box.

kamilmysliwiec avatar Oct 19 '18 12:10 kamilmysliwiec

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 24 '19 11:09 lock[bot]