CQRS and Replaying state events from a snapshot

Still reading the blog posts from Greg Young and derived works and one of the points which I have just got my head through is the scenario where you may have 100,000 + events for a particular object. Basically, to obtain the state for said object you need to load the aggregate from the persistence layer, and the only information you have to build up the object are the commands which were executed, so you have to replay them. Replaying 100,000+ events every time you need to load up an aggregate would be slow so the Memento pattern is used.  This means that you can query the last Memento of the object and replay only the Commands that were executed after that time.

I really like the idea of this way of thinking. Another point that was made is that, say you get a bug in your system.  Remedy for such bug should come in the form of a Command.  When I read this, i kind of tilted my head slightly whilst reading it, but it makes perfect sense.

In previous blog posts I have mentioned that I use Agatha library on my service layers and with this I was thinking that I already have the entry point for the Command Handler.  Of course I have the write and read requests so I would have to make sure that the write request handlers dealt with the domain model, where as the read request handlers dealt with the traditional DAO Repositories and Finders. 

IDEA : I can imagine that the types of UIs which could evolve from this style of architecture will be sweet.  I am picturing an interface where you could simply find an object from the read store, and when you select this object the UI will then load in the object from the transactional write store and possibly a nice timeline look-a-like with a property grid display of the object parallel to it. You could have a slide on the timeline and each time you changed the value of the slider, you could see the information in the property grid update.

Published Friday, July 09, 2010 6:10 PM by REA_ANDREW
Filed under: , ,

Comments

# re: CQRS and Replaying state events from a snapshot

Friday, July 09, 2010 1:55 PM by Jonathan Oliver

You'll want to be very careful in your use of the word command and event.   Think of a command as something "to be done" whereas an event is "something that has happened".  When you execute a command against a domain model, it invokes behavior and generates events.  When you replay an event against a domain model, it doesn't invoke behavior, instead it modifies the aggregate state.  It's a subtle distinction but very critical.

Also, when you are fixing a bug, oftentimes you'll manually append compensating/correcting *events* into the event log.

# OEM News » Aspiring marijuana growers look to stake a claim in NJs new industry

Pingback from  OEM News » Aspiring marijuana growers look to stake a claim in NJs new industry

# re: CQRS and Replaying state events from a snapshot

Saturday, July 10, 2010 6:47 AM by REA_ANDREW

Cheers for that Jonathan!! :-)

# Twitter Trackbacks for CQRS and Replaying state events from a snapshot - Andrew Rea [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 CQRS and Replaying state events from a snapshot - Andrew Rea         [asp.net]        on Topsy.com

# re: CQRS and Replaying state events from a snapshot

Sunday, July 11, 2010 5:44 AM by seagile

Fixing bugs should be done by issueing commands that will generate compensating events. Introducing new state tracking e.g. for a new feature happens in a similar fashion. I also must agree with Jonathan about the misuse of the words command and event, it may give people the wrong idea. As an aside, generating the compensating events can be done asynchronously to minimize the downtime of a production environment.

Leave a Comment

(required) 
(required) 
(optional)
(required)