Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between microservices - request data

I am dealing with communication between microservices.

For example (fictive example, just for the illustration):

  • Microservice A - Store Users (getUser, etc.)
  • Microservice B - Store Orders (createOrder, etc.)

Now if I want to add new Order from the Client app, I need to know user address. So the request would be like this:

Client -> Microservice B (createOrder for userId 5) -> Microservice A (getUser with id 5)

The microservice B will create order with details (address) from the User Microservice.

PROBLEM TO SOLVE: How effectively deal with communication between microservice A and microservice B, as we have to wait until the response come back?

OPTIONS:

  • Use RestAPI,
  • Use AMQP, like RabbitMQ and deal with this issue via RPC. (https://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html)

I don't know what will be better for the performance. Is call faster via RabbitMQ, or RestAPI? What is the best solution for microservice architecture?

like image 883
David Pavelka Avatar asked May 21 '18 17:05

David Pavelka


People also ask

How do I share data between two microservices?

Another way to share data is to share a data store. Services can share a relational database, NoSQL store, or another data storage service. One or more services publish the data to the database, and other services consume it when required. Most databases and data stores provide data via request/response mechanisms.

How do microservices communicate with databases?

Microservices database challenges 101 A monolithic application interacts with a single database. The data is shared between all application's components. By contrast, in a microservices app, data ownership is decentralized. Every service is autonomous and has its own private data store relevant to its functionality.

Can two microservices talk to each other?

Microservices are tiny (sometimes not so tiny) modules which can work independently of each other. They could have dependencies on other microservices or even a data persistence layer like a database. But the key is to use loose-coupling. Microservices coordinate by means of “communication.”


2 Answers

It all depends on your service's communication behaviour to choose between REST APIs and Event-Based design Or Both.

What you do is based on your requirement you can choose REST APIs where you see synchronous behaviour between services and go with Event based design where you find services needs asynchronous behaviour, there is no harm combining both also.

Ideally for inter-process communication protocol it is better to go with messaging and for client-service REST APIs are best fitted. Check the Communication style in microservices.io

REST based Architecture

  • Advantage

    1. Request/Response is easy and best fitted when you need synchronous environments.

    2. Simpler system since there in no intermediate broker

    3. Promotes orchestration i.e Service can take action based on response of other service.

  • Drawback

    1. Services needs to discover locations of service instances.

    2. One to one Mapping between services.

    3. Rest used HTTP which is general purpose protocol built on top of TCP/IP which adds enormous amount of overhead when using it to pass messages.

Event Driven Architecture

  • Advantage

    1. Event-driven architectures are appealing to API developers because they function very well in asynchronous environments.

    2. Loose coupling since it decouples services as on a event of once service multiple services can take action based on application requirement. it is easy to plug-in any new consumer to producer.

    3. Improved availability since the message broker buffers messages until the consumer is able to process them.

  • Drawback

    1. Additional complexity of message broker, which must be highly available
    2. Debugging an event request is not that easy.
like image 154
Yogendra Mishra Avatar answered Oct 02 '22 21:10

Yogendra Mishra


In your case using direct REST calls should be fine.

Option 1 Use Rest API :

When you need synchronous communication. For example, your case. This option is suitable.

Option 2 Use AMQP :

When you need asynchronous communication. For example when your order service creates order you may want to notify product service to reduce the product quantity. Or you may want to nofity user service that order for user is successfully placed.

I highly recommend having a look at http://microservices.io/patterns/index.html

like image 20
Foolish Avatar answered Oct 02 '22 20:10

Foolish