Steven
Steven

Reputation: 879

CQRS: Complex Event Handler with Query or Lookup service

I believe the general guidance on event handlers that populate read models / projections is to keep them simple.

What is the guidance on performing a query from an event handler, or preferably, using a lookup domain service that returns some information needed by the view?

My specific example is an event that contains a country code, that I want to appear as a country name (and other country info) in the read model. ie. highly stable data, although not guaranteed that it could never change at some point in the future. Some thoughts:

Any previous experiences that would lead someone to advise one of these options over the other?

Upvotes: 0

Views: 469

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57289

Option 2 is the usual choice in the literature - we run an asynchronous process that collects values from one or more persistent stores and composes a new representation that is cached for use by your read-only use cases.

In effect, there's very little difference between "our" data, which we read from the book of record, and "their" data, of which we have a stale copy.

Risks: it adds a db read (via the domain service) to the event handler, which is creating an additional potential failure point.

So what? we'll just fail, and retry later. Our read only views are stale copies anyway; anybody expecting nanosecond latency or better is kidding themselves.

Put another way, we don't care about failure, what we care about is meeting our service level targets, and how quickly we burn through our error budget.

Re-running the events to project the view model again could result in different state. ie. that country doesn't exist anymore.

That was always the case anyway - from the moment you decided to do distributed processing; stale data became inevitable. Modeling time can help, as can ensuring that the semantics remain stable (we can continue to understand the semantics of a country code even though that country no longer exists.)

Upvotes: 1

Related Questions