Allow persistent projections to be built/updated with Active Record and Aggregate Root #1925
Replies: 3 comments 2 replies
-
|
That seems convenient, although it needs some clear conventions how things are tied together. I think it is more suitable to a framework rather than a general toolkit. Or perhaps it needs some escape hatches to rewire the conventions/configuration. You want an ActiveRecord read model, re-built from a projection. Projection runs on a stream. That's first convention — parameterized matching of streams to read models. On the other hand subscriptions trigger on events. Thus reactively updating the read model would require subscribing to all event types to be found in a projection stream. Perhaps that signals that a hypothetical read model DSL could declare event types it is interested in. It would have been easier to be updated on stream changes but it is what it is. Not direct answers but good reads on read models from my former colleagues. |
Beta Was this translation helpful? Give feedback.
-
|
I've now published a first cut of this to https://github.com/brentsnook/active_record_projection |
Beta Was this translation helpful? Give feedback.
-
This would work only in the following conditions:
In other situations, there's a race condition coming from the fact that PostgreSQL and MySQL have multiple writers (MVCC). Global positions coming from auto-incremented primary key are not reliable for ordering due to the way how they're assigned across transactions and concurrent writers. This issue would manifest as skipped events under high concurrency. The problem is very well described in an article from Oskar. There's a couple of possible solutions, all having some kind of drawback in legacy systems with high chance of long transactions. As long as you stick to named streams with OCC (which is a default for Aggregate Root) you'll be fine. OCC will act as a linearizer in scope of a named stream, no event with an earlier position would appear. Positions in named streams coming from OCC are not global positions, thus they're safe. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We'd like to build Active Record model projections from our events to allow things to be queried efficiently from APIs etc without needing to rebuild the projection for each request. Has anyone considered building something like this on top of
AggregateRootto allow that to be mixed into an ActiveRecord model and have that model updated as new events are added to a stream? Or is there another way to do this?This could look something like this (maybe fired from event handlers for the different events):
A projection record could store a polymorphic reference to the model, the stream it is projected from and the last seen event ID:
The aggregate behaviour could then use it to apply only unseen events:
And used in an AR model:
This would also require some changes to the
AggregateRoot::Repository:If there's some interest in this I can put up a spike PR, otherwise we'd probably look to implement something similar just internally.
Beta Was this translation helpful? Give feedback.
All reactions