[ ] 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.
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
+1
+1
I was able to get load balancing working using NATS queuing.
I was able to get load balancing working using NATS queuing.
@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.
@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 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 Thanks for sharing this! This helps a lot. I'll try to create my own then.
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).
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.
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.
Most helpful comment
+1