WeiTang Lau
WeiTang Lau

Reputation: 123

How to query another microservice in saga pattern

I am developing a spring boot microservice application adopting the Saga Orchestration pattern and Database-per-service design. I understand that the pattern should be async. However, I have a use case where I have a microservice that need to query for data from another service.

Below is the context:

I have 2 services, Order and Customer. Order has a customerId field. When the client (via HTTP) requests for all orders, Order will need to query Customer for the customerName with that particular customerId. This will return all orders along side customer name, instead of customer id, for that order.

Solution considered:

  1. Using synchronous communication, either REST or gRPC for that query from Order to Customer. Easy to maintain but risk of data loss.

  2. A duplicated Customer table in Order. This eliminates the need for communication outside of Order, but need to maintain the data sync.

Upvotes: 0

Views: 602

Answers (1)

Paul Parkinson
Paul Parkinson

Reputation: 63

A good pattern to address this is event sourcing. The Customer service sends an event/message when it does data operations (customerId and customerName events for example) and the Order service listens for these events. The Order service may also request replays of events/messages if necessary. This decouples the services, prevents static interdependencies, etc. Hope this helps. I can elaborate if need be.

Upvotes: 2

Related Questions