Video in progress
`order-service` needs the customer's name: do you call `customer-service` each time, or keep a copy of the data yourself?
It depends on how fresh that data has to be. For data that rarely changes and does no harm if it's slightly stale, I keep a local copy updated through events; when I need the exact current value (a balance, for example), I call synchronously.
📖 Answer
Calling synchronously every time looks simple, but it has a price: if customer-service goes down or slows down, the orders list won't open either. With 50 orders in the list, it turns into N+1 network calls.
The local copy option:
@KafkaListener(topics = "customer-events")
public void on(CustomerUpdated e) {
customerViewRepo.save(new CustomerView(e.customerId(), e.fullName(), e.version()));
}
order-service stores only the fields it needs in its own database. Even if customer-service is down, orders keep showing.
The drawback is that the data may be stale for a few seconds, and you have to be careful not to lose events and to handle ordering (that's what version is for: so an old event doesn't overwrite a newer value).
One more option is a snapshot: when the order is created, write the customer's name and address at that moment into the order itself. In many domains this is exactly right: the receipt has to show "how it was at the time of the order", even if the name changes later.
I keep the synchronous call for things like limits, balances and permissions, where making a decision on a stale value is dangerous.
Sign up to mark studied questions and save your progress.