Saturday, September 19, 2020

Microservice Spring Java Interview Question - Is it the best approach to implement the REST for inter-microservices communication? or any other alternatives

Question :

Are there other alternatives for inter-microservices communication? Is it the best approach to implement the REST for inter-microservices communication?

Ans:

the microservices we have developed are based on REST. We have used REST for both internal (inter-microservice, where one microservice communicates with another microservice in the same system) and external (through the public API) communication. At present, REST fits best for the public API. 

You can build microservices that are purely asynchronous. You can build microservice-based systems that would communicate based on events. There is a tradeoff between REST and event-based microservices. 

REST provides synchronous communication, whereas reactive microservices are based on asynchronous communication (asynchronous message passing). We can use asynchronous communication for inter-microservice communication. Based on the requirement and functionality, we can choose REST or asynchronous message passing.

 Consider the example case of a user placing an order, which makes a very good case for implementing reactive microservices. On successful order placement, the inventory service would recalculate the available items; the account service would maintain the transaction, correspondence service would send the messages (SMS, emails, and so on) to all involved users such as a customer and a supplier. 

In this case, more than one microservice may perform distinct operations (inventory, accounts, messaging, and so on) based on an operation (order placement) performed in one microservice. Now, just think if all these communications were synchronous. Instead, reactive communication, with asynchronous message passing, provides efficient use of hardware resources, non-blocking, low latency, and high throughput operations. 

We can primarily divide the microservice implementations into two groups—REST-based microservices and event-based/message-driven microservices. Reactive microservices are event-based.

So now what is reactive microservice?

A reactive microservice is based on the reactive manifesto and it has four principles.

for reference please go through it

https://www.reactivemanifesto.org/




1)Responsive
2)Resilient
3)Elastic
4)Message Driven

Reactive microservice performs operations in response to events like a single event, a microservice can have multiple producers or consumer events. Also, a microservice can have both producer and consumer events. like in the Booking microservice that creates the new booking (POST /v1/booking). This will be our event source and would make use of Apache Kafka for sending this event. Other microservices can consume this event by listening to the event. On a successful booking call, the Booking microservice will produce the event bookingOrdered.

So that why we should be focusing on the Reactive Microservice Architecture.



 

No comments: