Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gRPC and/or GraphQL for microservice architecture

At my company we're about to set up a new microservice architecture, but we're still trying to decide which protocol would be best for our use case.

In our case we have some services that are called internally by other services, but are also exposed via a GraphQL API gateway towards our clients.

Option 1: gRPC

gRPC seems to be a popular choice for microservice internal communication because of its performance and efficiency.

However, gRPC makes it more difficult to query relational data and requires more work to hook up to our API gateway.

Option 2: GraphQL

Another option is for each microservice to implement their own GraphQL schema so they can be easily stitched together using Apollo Federation in the API gateway.

This approach will make queries more flexible, but internal requests become less performant because of the lack of protocol buffers.

Option 3: Both?

Perhaps another alternative is to use the best of both worlds by implementing mutations in gRPC and queries in GraphQL. Or just creating two APIs, one facing the gateway clients and one for communication between services.

Questions

  1. How do we decide which approach to use?
  2. Are there any significant (dis)advantages we should consider? E.g. in terms of ease of use, maintainability, scalability, performance, etc?
  3. Are there better alternatives for this use case?
like image 502
Duncan Luk Avatar asked Feb 17 '21 12:02

Duncan Luk


People also ask

Can we use GraphQL with gRPC?

When it comes to architecting an application, developers have a wide range of client-server communication protocols to choose from. It's pretty popular across modern apps to use GraphQL, gRPC, REST, and Webhooks. Depending on the needs of your application, each protocol can offer different benefits.

Is gRPC good for microservices?

A gRPC based RPC framework is a great choice for inter-process communication in microservices applications. Not only the gRPC services are faster compared to RESTful services but also they are strongly typed. The Protocol Buffer, a binary format for exchanging data, is used for defining gRPC APIs.

Is GraphQL good for microservices?

Data graphs are a good solution for enterprise system integrations, enabling multiple applications to work together smoothly, and thus efficient for microservice architecture. This is how GraphQL allows you to model your data.

Is GraphQL the right fit for designing microservices architecture?

GraphQL and microservices are a perfect fit, because GraphQL hides the fact that you have a microservice architecture from the clients. From a backend perspective, you want to split everything into microservices, but from a frontend perspective, you would like all your data to come from a single API.

Should I use gRPC or GraphQL for my API gateway?

However, gRPC makes it more difficult to query relational data and requires more work to hook up to our API gateway. Another option is for each microservice to implement their own GraphQL schema so they can be easily stitched together using Apollo Federation in the API gateway.

Should microservices implement their own GraphQL schema?

Another option is for each microservice to implement their own GraphQL schema so they can be easily stitched together using Apollo Federation in the API gateway. This approach will make queries more flexible, but internal requests become less performant because of the lack of protocol buffers. Option 3: Both?

What is gRPC in ASP NET 5?

gRPC usage of Microservices Microservices are modern distributed systems so with gRPC in ASP.NET 5, we will develop high-performance, cross-platform applications for building distributed systems and APIs. It’s an ideal choice fo r communication between backend microservices, internal network applications, or iot devices and services.

How do I use gRPC between microservices?

When Using gRPC between microservices the front end is connected to a microservice (that is, the gateway to the microservice cluster) via a plain HTTP API consumed over the Web. As mentioned, at the current stage of both gRPC and the browser technology, no gRPC services can be invoked directly from any browser.


2 Answers

It depends on the overall architecture you are implementing.

I would suggest to use both GraphQL and gRPC:

  • Use Apollo Federation as a border node to seamlessly handle the requests from your frontends talking in GraphQL.

  • Use CQRS pattern on your API and microservices, and strictly separate the read model from the write model.

  • Use hexagonal architecture (we use Explicit Architecture in my company) to implement DDD - Domain-driven-design.

  • In order to seamlessly integrate Apollo, you will have to implement GraphQL layer into the architecture of all of your backend services.

  • In read model, implement GraphQL layer. You will benefit from the federation (parallel reads of data from several microservices will be "joined" in federation engine without any participation of your API nodes).

  • For communication among your backend services in write model (mutations), use gRPC.

  • gRPC will make it possible to call your CQRS commands remotly like it was local.

  • So, your remote microservices will look like part of your local backend code.

  • You will also need a message-broker like RabbitMQ or Kafka to manage some more complex state changes via events and message queues.

like image 116
digitalstraw Avatar answered Sep 28 '22 08:09

digitalstraw


I would suggest that you should go with gRPC for internal services communication as it is fast. Now comes what should you use for external communication, you may use REST or Graph QL. Graph QL is good option if different type of Client needs different amount of data otherwise REST will be easier to implement.

In one of my project, we have used the gRPC for internal communication (services are in Go language) & for external communication, we have used REST. Go have some packages which help to develop service expose using REST and internally communicating with other service using gRPC. So, in our scenario, it was working fine.

like image 39
xs2tarunkukreja Avatar answered Sep 28 '22 07:09

xs2tarunkukreja