NestJS tcp microservice load balance

1.3k views Asked by At

I have a nestjs app as follows:

  1. Gateway - HTTP
  2. Microservice1 - TCP (ms1)
  3. Microservice2 - TCP (ms2)

Gateway calling ms1 and ms2. ms1 and ms2 are the same duplicated microservice.

I would like to load the balance between ms1 and ms2. How do accomplish that in nestjs?

Thanks

1

There are 1 answers

1
Mateusz Klimentowicz On

Many cloud platforms have a built-in load balancer and I recommend using them. There is a fundamental problem with this TCP story from my point of view. I think that a load balancer could not distribute the TCP packets correctly because it would not know which packets belong together as a logical unit.

Since the NestJS documentation says: "... a microservice is fundamentally an application that uses a different transport layer than HTTP", you could simply create a new Nest application that runs separately but is still accessible via HTTP, because the difference is only in the transport protocol and the cloud built-in load balancer can handle HTTP in any case.

That mean:

  1. Gateway - HTTP
  2. Microservice-a (n instances) - HTTP

Communication goes via the HttpModule:

@Injectable()
export class SomeService {
  constructor(private httpService: HttpService) {}

  findAll(): Observable<AxiosResponse<Entity[]>> {
    return this.httpService.get('http://host-ms-a:port/my-entities');
  }
}