0

Let's say we're talking about a webshop.

  1. Microservice 1: Manages products and their prices
  2. Microservice 2: Responsible for payment and checkout.

If a user wants to pay for a product, the payment service is invoked. The service has to return success or failure synchronously. Also the service has to guarantee to use the current prices.

So the current workflow passes the productIds to the payment service. The payment services queries the current price (from service 1 SYNCHRON). The payment service runs the payment process.

Workflow:

Caller (App) -> Payment service -> Product service -> Payment service -> Caller (App) 

How could such a scenario be optimized to asynchronous communication? Or is synchronous communication through rest services the best way?

2
  • I think you might be looking for the terms request-reply or request-response vs. one way messaging. Messages are only synchronous or async with respect to the internal threading architecture of the sender/receiver, as to whether they are blocking of main/other work vs main/other work can continue while awaiting (e.g. servicing other clients). On the wire, outside of an implementation, a message is neither sync nor async.
    – Erik Eidt
    Mar 30, 2018 at 14:18
  • Well there is a difference between asynchrous communication and async i/o.
    – Florian
    Mar 30, 2018 at 14:28

1 Answer 1

4

There is no best way in programming. All depends on the situation. Thought your explanation is not very clear. I am assuming that you want to say that your Workflow -> Service 2 -> Service 1. Now you want to invoke service 1 Async.

Solution: If this is correct then I don't think you can use Async because you need to wait for Service 1 to return Current Price in order to proceed. Or you can change your process:

You can invoke your Service 1 from Workflow (Pass the productId and get current price). Once you have all the information, Invoke Service 2 and pass all data in parameter. This way you don't have to worry about sync and async.

    private void YourProcessFlow() //inside your workflow
    {
        var currentPrice = InvokeService1(12345);
        InvokeService2(12345, currentPrice);
    }

Alternative: Here is code for Async, Just in case, if you want to use it somewhere else:

    using System.Threading.Tasks;

    new Task(() => { InvokeService1(12345);}).Start();

    private int InvokeService1(int productId)
    {
        // Invoke your Service 2 here
        return price;
    }

Here Line 1 will invoke your method and move on to next line of code. This will not wait for method to return value. Like I said, you need currentPrice to proceed and this cannot be done Async.

Idea: I don't know your complete situation but if if there are other things your service/workflow is doing and they don't depend on each other's result then you can use Parallel Programming concept. You can google for more information

1
  • "There is no best way in programming. All depends on the situation." This needs to be highlighted! I am very surprised that even some senior and experienced software engineers become victims of hypes, and declare one technique, one strategy, one approach or one pattern as THE solution. Aug 9, 2018 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.