nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

Microservices: TCP vs Redis #358

Closed triedal closed 6 years ago

triedal commented 6 years ago

In what circumstances would it be preferable to use Redis over TCP for the transport layer? Does Redis allow you to spin up multiple instances of the same microservice and react to events posted in the broker?

Thanks!

kamilmysliwiec commented 6 years ago

Hi @triedal, They're just different, pros & cons depend on the use-case. It's your decision.

Does Redis allow you to spin up multiple instances of the same microservice and react to events posted in the broker?

Redis is based on pub-sub, you should be able to subscribe to messages across multiple instances.

Also, these built-in transports are sometimes not enough, in this case, you can just provide your custom strategy.

triedal commented 6 years ago

Thank you @kamilmysliwiec

Renader commented 6 years ago

@triedal did u managed to get it to work properly?

I'm new to microservice architecture and so on, tried some example code having 2 separate apps. One acts as a gateway receiving the REST calls. The work should be done by microservices to enable horizontal scaling. In a technical breakthrough, everything works fine. But as I launch 2 instances of my microservice, both get the command.

Is this intended behavior @kamilmysliwiec? What am I missing? I'd expect that each task gets only published to one microservice instance and not to all instances at the same time.

Do I have to implement a load balancer mechanism myself?

kamilmysliwiec commented 6 years ago

Redis doesn't have any built-in groups/topics mechanism as far as I know. In your case, you have to build load balancer by yourself OR reuse any existing, available solution (if fits your requirements).

Renader commented 6 years ago

Just if anyone else runs into the same issue: I solved it by using RabbtiMQ which soon will be available for NestJS https://github.com/nestjs/nest/pull/1170

Until the PR is not merged you can use https://github.com/AlariCode/nestjs-rmq

RabbitMQ defaults to distributing only to 1 listener (what could be changed by using fanout). But don't trust my knowledge too much, I'm still a learner. Anyway I got it to work this way!

triedal commented 6 years ago

@Renader I was able to do the load balancing with NATS queue groups. You can read more about it at https://github.com/nestjs/nest/issues/758#issuecomment-428235340. Seems like your solution is basically the same thing, just with RabbitMQ.

Renader commented 6 years ago

@triedal thanks!

cojack commented 6 years ago

Redis is based on pub-sub, you should be able to subscribe to messages across multiple instances.

There is one problem, you won't receive many responses, but only one. No clue why, @kamilmysliwiec can you explain it, this behaviour? Im just curious.

kamilmysliwiec commented 6 years ago

Redis doesn't have a built-in load balancing mechanism.

lock[bot] commented 5 years ago

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.