Search code examples
c#.net-corearchitecturemicroservicesapi-gateway

Where should I filter an MS level external service response in microservice architecture pattern?


I use an API Gateway (BFF) in front of my microservices to handle the UI needs.

For example I have a microservice which calls an external service.

In the UI I don't need (NOW) all the information which the external service provides. Should my MS pass only the currently needed data to the BFF, or pass the whole response and I should "filter" the MS response in the BFF?

For Example:

basic diagram


Solution

  • The whole point of BFF pattern in the first place is to provide a purpose built interface for Frontends, and they're free to collect the data from the services that contain them. I'd suggest either:

    1. If the Microservice microservice (in your diagram) manipulates the data somehow before returning them, then manipulate them as they are written (potentially by observing some event in the external system if you can) and store the processed result (potentially a local copy).
    2. If you don't need to manipulate the data as they're sent to the BFF, then any processing you're doing can (in theory) be done asynchronously, and outside the context of the requst, and not in-line and have the unnecessary risk of unavailability if it fails (this is called temporal coupling).

    If one of the above two cases will be feasible, and I very strongly suggest you avoid daisy chaining synchronous web requests, because this greatly decreases your avaiability (if each service is 95% available, the two combined are 90% available, not 95%, and this gets bad quickly). Latency is a concern (as @picolino rightly mentions), but in my experience it is much rarer for it to be a bigger concern than availability. In any case, flattening the call stack will greatly reduce the time taken to process the request anyway.

    Both above options are also good from perspective of allocation of responsibilities; one service would own the customer data, and the other data doesn't need to own passing through, it can be notified asynchronously by the BFF to do any cross-cutting concerns it needs to.

    If and only if none of the above are feasible, I'd suggest you consider a bit the contract of the service you're developing and the data that is needed by it before making a decision. Passing along the complete set of data blindly may or may not be a good idea, depending on what is the nature of the responsibilities of Microservice service you have there.