Daniel Stefanelli
Daniel Stefanelli

Reputation: 517

Why does nestjs framework use a transport layer different than HTTP in a microservices approach?

I have been developing microservices with Spring Boot for a while, using feign client, rest template and AMPQ brokers to establish communication between each microservice.

Now, I am learning NestJs and its microservice approach. I've noticed that nestjs uses TCP as the default transport layer which is different from the way it is done with Spring Boot.

Why does nestjs prefer those transport layers (TCP, AMPQ) instead of HTTP? isn't HTTP the transport protocol for rest microservices?

From NestJs documentation:

"a microservice is fundamentally an application that uses a different transport layer than HTTP"

Upvotes: 11

Views: 3046

Answers (2)

Yilmaz
Yilmaz

Reputation: 49521

The main reason is it is slow. The problem with HTTP approach is that, with HTTP, JSON can generate an unwanted processing time to send and translate the information.

One problem with http-json is the serialization time of JSON sent. This is an expensive process and imagine serialization for a big data.

In addition to JSON, there are a number of HTTP headers that should be interpreted further which may be discarded. The only concern should be to maintain a single layer for sending and receiving messages. Therefore, the HTTP protocol with JSON to communicate between microservices is very slow. There are some optimization techniques and those are complex and does not add significant performance benefits

Also,HTTP spends more time waiting than it does transfer data.

Upvotes: 8

Noah
Noah

Reputation: 919

If you look a the OSI model, HTTP is part of Layer 7 (Application). TCP is Layer 4 (Transport).

When looking at Layer 4 there is no determining characteristic that makes it HTTP, AMPQ, gRPC, or RTSP. Layer 4 is explicitly how data is transmitted and received with the remote device.


Now, this is where networking and the software development worlds collide. Networking people will use "transport" meaning Layer 4, while Programming people use "transport" meaning the way a packet of data is transmitted to another component.

The meaning of "transport" (or "transporter" as used in the docs) is used as an abstraction from how messages are shared in this architecture.

Looking at the documentation if you are looking for something like AMPQ for your microservice you can use NATS or REDIS (both implementations are built by them).

https://docs.nestjs.com/microservices/basics#getting-started

OSI Layer

Upvotes: 7

Related Questions