23

I want develop a project in microservice structure. I have to use php/laravel and nodejs/nestjs What is the best connection method between my microservices. I read about RabbitMQ and NATS messaging and also GRPC Which option is more suitable for microservice? and why? Thanks in advance

Mohammad Honarvar
  • 231
  • 1
  • 2
  • 6

3 Answers3

34

The technologies address different needs.

gRPC is a mechanism by which a client invokes methods on remote (although they needn't be) server. The client is tightly-coupled (often through load-balancers) with servers that implement the methods.

E.g. I (client) call Starbucks (service) and order (method) a coffee.

gRPC is an alternative to REST, GraphQL, and other mechanisms used to connect clients with servers though some form of API.

Message brokers (e g NATS, Rabbit) provide a higher-level abstraction in which a client sends messages to an intermediate service called a broker (this could be done using gRPC) and the broker may queue messages and either ship them directly to services (push) or wait for a service to check its subscription (pull).

E.g. I (client) post a classified ad on some site (broker). Multiple people may see my ad (subscriber) and offer to buy (method) the items from me. Some software robot may subscribe too and contact me offering to transport or insure the things I'm selling. Someone else may be monitoring sales of widgets on the site in order to determine whether there's a market for opening a store to sell these widgets etc.

With the broker, the client may never know which servers implement the functionality (and vice versa). This is a loosely-coupled mechanism in which services may be added and removed independently of the client.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
25
  • If you need a synchronous response on 1:1 service call use gRPC

  • If you don't care which service will consume messages (asynchronous & no tight coupling between services) use RabbitMQ

  • If you need distributed system to keep events history and reuse later on another service use Kafka

Alireza Sattari
  • 181
  • 2
  • 12
Trung Pham
  • 351
  • 2
  • 2
  • It may be worth mentioning that grpc supports websocket-like asynchronous streams of messages as well – saolof Jul 31 '23 at 05:43
1

Basically, it comes down to whether you want an Async communication between services or not.
That is when you can decide between real-time communication services (Sync) such as gRPC or RPC & Message Queueing ones (Async) such as RabbitMQ, Kafka or Amazon SQS.

Here are also some good answers by other users:

Alireza Sattari
  • 181
  • 2
  • 12