Home Ask Login Register

Developers Planet

Your answer is one click away!

Vladimir Nani February 2016

Should I update read model on each event

I have Customer read model that needs to be updated after NewOrderEvent.

One thing i want to understand, should i update my read model on every event. Or i need to replay all events and replace read model.

What im doing now is:

  1. Saving NewOrderEvent
  2. Getting or creating Customer read model
  3. Invoking Customer.ApplyEvent(NewOrderEvent) that changes Customer state.
  4. Saving Customer read model

Am i missing something?


ILICH February 2016

Looks good to me. You may decide to replay read model only if you introducing something new to it, and this new information could be obtained only from events.

Sometimes people rebuilding read models whenever schema changes, but in many cases you can use migrations for that. Really depends on your application.

MikeSW February 2016

Usually yes, you want to update the read model every time you have an event. But, it's just a simple CRUD operation, a db update. The replaying of events is done when you want to (re)generate a new read model, because you could have millions of events and could be a very long running operation.

Btw, the apply stuff should be reserved for command model only, in order to avoid confusion. You apply events to a domain aggregate root (entity), but you use an event as the source of data for read model updates.

Post Status

Asked in February 2016
Viewed 2,984 times
Voted 9
Answered 2 times


Leave an answer

Quote of the day: live life