This is a design question. If let’s say I have a Service A which needs to request Service B to do some processing, and Service A needs to know the result of that processing, what would be the best way to implement this?
Note: My understanding of lagom’s preferred way of communication is via event topics over rest calls. Although both are available to lagom developers, communication via event topics makes the overall architecture more resilient vs communication via REST. If I misunderstood this, kindly correct me as well.
Option 1: Via Event Publication/Subscriptions
ServiceA publishes a ProcessingRequestedEvent to service-a-topic
ServiceB subscribes to service-a-topic
Once ServiceB sees a ProcessingRequestedEvent, it will do the processing, and outputs the result by publishing a ProcessedFinishEvent to service-b-topic.
ServiceA subscribes to service-b-topic, and waits for the ProcessedFinishEvent to find out what happened to its request.
Pro : It’s asynchronous and all communications are via event topics
Con : Introduces a cyclic dependency wherein ServiceA and ServiceB subscribes to each other. Although I think this is a “weak” circular dependency (because it is ServiceA-impl that depends on ServiceB-api, and it is ServiceB-impl that depends on ServiceA-api, it still introduces a conceptual cyclic dependency between two microservices)
Option 2: Via REST Calls
ServiceA invokes ServiceB’s REST API.
Once ServiceB has finished processing, it will respond to ServiceA’s request synchronously.
ServiceA will find out the result of that request via the REST call’s response
Pro : There are no cyclic dependency. Only ServiceA knows about ServiceB (i.e. ServiceB does NOT know about ServiceA)
Con : It’s a synchronous operation
Given that scenario, which is the lagom best practices to approach this? (if there are other options, kindly let me know as well).