Marius
Marius

Reputation: 9664

nservicebus: what is the standard approach to ensuring that all events have been processed?

Sorry for the fussy question, I'll try to ellaborate by showing you a use-case (which is not my real world case, so please don't take note of event-/service- names or responsibilities):

Consider a system with the services (among others): "Store" and "Economy". The store service processes item-purchases and publishes three events: "CustomerBoughtItem", "CustomerReturnedItem" and "StoreIsClosed". Now, the economy service picks up those events and creates a financial report on a daily basis. The report should be created immediately after the shop is closed. It is crucial that all events from the store service included in the daily report, which means that the economy service can not create its financial report before all events from the store has been processed. How can I deterministically ensure that all "CustomerBoughtItem" and "CustomerReturnedItem" events have been processed at the end point before I start processing the "StoreIsClosed" event?

Upvotes: 1

Views: 160

Answers (1)

Stephan Schinkel
Stephan Schinkel

Reputation: 5530

you can use the following pattern:

open a saga and receive these three messages. every message has a sequential id (for example as int). save all sequential ids and determine on every of these messages received that:

  • StoreIsClosed Message was received in some point of time
  • All ids (from 0 or 1 to the id of the StoreIsClosed-SequentialId) are sequential and no id is missing

if these two conditions are met send the last message to your saga via .SendLocal (CreateDailyReportMessage).

edit for better formatting and to answer the comment:

I assume by "StoreIsClosed" event published by another service you mean: that the other service does not now, what the last SequentialId is or more general the service that is responsible for the event, that an StoreIsClosed does not know anything about customer transactions.

so store service must implicitly or explicitly send you more context information so that the economy service

examples:

  • (implicit) subscribe to the StoreIsClosed event in your storeservice and publish a CustomersHaveBoughtOrReturnedToday Message that is subscribed to in your saga. here you can include a list of transaction Ids that you can cross check in your saga or some other kind of checksum like total count of transaction or the first sequential id and the last sequential id for that store.
  • (explicit) right before your sage is ready for completion explicitly ask your store service if you have all transactions processed. (send/reply in your saga)

Upvotes: 1

Related Questions