Event Sourcing: Rehydrating Aggregates with Snapshots

Поділитися
Вставка
  • Опубліковано 15 лис 2024

КОМЕНТАРІ • 89

  • @mrjamiebowman1337
    @mrjamiebowman1337 3 роки тому +13

    I really like that your videos are short, concise, and packed with information.

  • @kevinohara80
    @kevinohara80 2 роки тому

    This is one of those aspects of event sourcing that’s usually mentioned, but not explained. Thanks for the concise explanation!

  • @leandrogalvan1110
    @leandrogalvan1110 3 роки тому +1

    I like that you mentioned the example with Orders being kinda short-lived. Whenever I picture event-sourcing in my head I can't help but think of really long-lived stuff such as Users, and say 'man, am I really going to reconstruct this thing from like literally thousands of events?'. Good insight :)

    • @CodeOpinion
      @CodeOpinion  3 роки тому +1

      In my experience, most are finite and have a limited lifespan/ that doesn't contain an overwhelming number of events. Again, just the domains I've lived in likely.

  • @F2H16
    @F2H16 9 місяців тому

    Thanks a lot for all the content on event sourcing. Quick question, is there a chance that you could walk us through the comparison of Marten DB and EventStroDB ?

  • @codewithkashif
    @codewithkashif 3 роки тому

    Super awesome and realtime content! you never let down your standard, instead it is going next level in every post! Thanks a for helping!

  • @ZinchenkoOleg
    @ZinchenkoOleg 2 роки тому

    Great job, thanks!
    I have a question: What if we have to add an event in the constructor (for example Product Added Into The System)? How do we restore the state of the Product model from the event stream without a snapshot? If we use a constructor we will add the event on restoring from the state. To restore a product from the saved state we can use reflection. But if we don't use snapshots?
    While writing the question, I've realized that we cant use the initial event to create the Product object using reflection, then apply other events.

  • @andy_lamax
    @andy_lamax 3 роки тому

    Never mind my question from projections video. This one answered it

  • @codewithkashif
    @codewithkashif 3 роки тому +2

    Hey just curious to know that what next is coming in this wonderful series of event sourcing.
    Especially I am looking for a solution or workaround for -
    "What should we do with persisted events when that object structure is going to change"
    like for example earlier we had one "Name" property but now we have to segregate it in "FirstName", "MiddleName" and "LastName" or vice-versa

    • @CodeOpinion
      @CodeOpinion  3 роки тому +2

      Sounds like versioning is what you're after?

    • @codewithkashif
      @codewithkashif 3 роки тому

      @@CodeOpinion Yes you can say that it is kind of versioning or may be weak schema..do you have any plan to publish any new video around these topics

    • @codewithkashif
      @codewithkashif 3 роки тому

      In past we had faced similar issues with CQRS approach where we first persist data in relational db and then we also saves several other copies in elastic search just according to UI. so it makes read quite fast.
      However later in case of any major schema change in relational database makes all saved objects in elastic search obsolete. So in such scenario we used to keep running some kind of background/console process to refresh the elastic search data from latest relational db. It was kind of pain though.

  • @raghuveerdendukuri1762
    @raghuveerdendukuri1762 3 роки тому

    In an event sourcing & CQRS based system, we will use different projection criteria to create different read models and update them using event subscription.
    do we require aggregate in command side to depict current state in this scenario in addition to event store wherein, the same events will be subscribed to and are used to update aggregate in command side while read models are updated in query side of CQRS using another subscription to the same event?
    please share your inputs @Derek, thank you

    • @CodeOpinion
      @CodeOpinion  3 роки тому +1

      Your aggregate will replay the events from a stream (or from a snapshot + events since) to build up a projection used in the aggregate if it needs to check any invariants.

  • @robert5763
    @robert5763 3 роки тому +1

    Great content! Really like this videos where you show some implementation of the ideas you talk about.
    I have some questions:
    - Where should Saga state be stored when you use a message queue and events handling is async? For each event, a new thread/process is created and the Saga state has to be recreated up to the point after the latest event was processed.
    - I want to start a new side project and I'm wondering what would be the appropiate architecture, the app will be similar to Uber/food delivery apps with real-time map/chat/notifications etc.

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      I'm not sure I'm following in regards to the first question in regards to sagas. When you have a long running process that uses a saga, it's internal state is most often times handled by the messaging library. You could deal with it yourself, however each inbound message that your saga is handling would need some correlationID so it could get/save that state to some data store. But most often times that's handled by the messaging library if it supports sagas. By chance have you watch this video? ua-cam.com/video/rO9BXsl4AMQ/v-deo.html

    • @robert5763
      @robert5763 3 роки тому

      @@CodeOpinion I'm going to watch it now. But yes, I was thinking about managing the sagas manually, using a correlationID makes sense, I like the idea of having an identifier for every long process. I want to play with CQRS and Sagas in Typescript, but there aren't many messaging libraries like, for example, in .NET. Thank your for your answer!

    • @dsindun7224
      @dsindun7224 3 роки тому

      @@CodeOpinion is CorrelatonId about a user story ? In my opinion ,correlationid is used by many connected events .

  • @KonstantinKuda
    @KonstantinKuda 2 роки тому

    Great video. Thanks a lot!
    Which strategy do I have to choose to determine how often I need to do a snapshot?

    • @CodeOpinion
      @CodeOpinion  2 роки тому

      Snapshots are an optimization. I'd first be making sure I'm designing streams that are finite (usually). I'd only be going down the snapshot route if it was required because streams were taking too long rebuild an aggregate.

  • @aaronderenard3527
    @aaronderenard3527 2 роки тому

    Mr. Comartin says "Thou shalt not add 10!" Must add 11, or we might be children about it. haha

  • @dsindun7224
    @dsindun7224 3 роки тому

    Great job . I have a question that how do you persist snapshot object , in a realtional db table ? is Snapshot an event ,like productshiped?

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      Generally store the snapshot is just another event but often stored in another stream.

    • @dsindun7224
      @dsindun7224 3 роки тому

      @@CodeOpinion thank you !

  • @rustamhajiyev
    @rustamhajiyev 3 роки тому

    Awesome video! I have a question: let's say I have a domain event and the related Apply method have a bug. After couple wrongly applied events, the final aggregate state is incorrect. At some point I fix the bug and when the events are replayed I get the correct state but from a user perspective that probably will look quite weird: how the value changed without an interaction. What are the best practices for this kind of situation to both fix the bug and don't frustrate the user. Thanks!

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      I don't see it as being different then any other bug really. Let's say you were using RDBMS and you were doing a SUM() on a set of rows. However there was a bug in your WHERE statement that you fixed. Now the result of the SUM() is different and is shown correctly to the user. Would you do something in that case?

    • @rustamhajiyev
      @rustamhajiyev 3 роки тому +1

      @CodeOpinion, thanks for your answer. That makes sense. I'm just trying to figure out potential problems I could have with EventSourcing. Recently I watched a video ua-cam.com/video/FKFu78ZEIi8/v-deo.html where Greg Young is talking about cons of EventSourcing, and one of the problems he mentioned is versioning of domain events. Do you have an experience with it? Some techniques you would recommend? Maybe that would be a good topic for another video in this series. Again, thank you a lot for the great content!

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      @@rustamhajiyev Ya recently watched that video as well this week. I actually left a comment in regards to the Q&A portion. I think what Greg mentioned in regards to it being "different" is the biggest issue people have and not knowing the various problems/solutions that come with it.

  • @AlfonGeek
    @AlfonGeek Рік тому

    do you create a stream for each Product sku ?

  • @TheSydBarrett
    @TheSydBarrett 3 роки тому

    Awesome! This video is very helpful for understanding shanpshots.
    Btw, is there any built-in or additional extensions/helpers for working with Snapshots? Maybe some basic abstractions for avoding this Position calculation routine.

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      Nothing built-in that I'm aware of. Likely something you'd probably create and tailor to your needs that you could likely re-use.

  • @fredimachadonet
    @fredimachadonet 3 роки тому +2

    Great video! Thanks a lot. I've just joined the community as a developer.

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      Welcome aboard! Thanks for the support. I appreciate it.

  • @raghuveerdendukuri1762
    @raghuveerdendukuri1762 3 роки тому

    Can we consider that aggregates are maintained to be able to record snapshots when required while also being useful to do business rules validation of the data inputs?
    Please also share if we can access read model from command handler or if it violates CQRS, do we need to access through a different scope?

    • @CodeOpinion
      @CodeOpinion  3 роки тому +1

      Aggregates are consistency boundaries ultimately. Check out this video: ua-cam.com/video/64ngP-aUYPc/v-deo.html
      As for using a read model in a command handler, it depends why you're reading against it? If its to perform some validation, the moment you read/query it could be stale and something change it. Hence why you would want an aggregate.

    • @raghuveerdendukuri1762
      @raghuveerdendukuri1762 3 роки тому

      @@CodeOpinion thank you

  • @brendonvandoornum6123
    @brendonvandoornum6123 3 роки тому

    Great video, I like how the snapshots are just another event stream. I am curious, how do you handle the scenario where the ShipProduct quantity is greater than the available product quantity?

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      As usual "it depends". Depends if that command can fail or not. Unless you're going to (pessimistic) lock a record at a database level, you could ultimately end up shipping more product then is available. So if it's going to happen, you're likely going to create some use-case for if it occurs. The resolution to that might not be complicated and simple as a notification/report. Check out this part of a video I talked about if commands can fail: ua-cam.com/video/oJiUjiWeesQ/v-deo.html

  • @dansharpe4535
    @dansharpe4535 3 роки тому

    great video as always!!! have you used EventStoreDB in production at all? and if so, what are your experiences with it in terms of hosting/scaling etc?

    • @CodeOpinion
      @CodeOpinion  3 роки тому +2

      I have in the past, but not recently. The biggest issue was I did not want to have to manage a cluster. I wanted to have a managed service. Having said that, they do now have an cloud offering. Worth checking out.

    • @dansharpe4535
      @dansharpe4535 3 роки тому

      @@CodeOpinion have you used anything else or have any recommendations?

    • @CodeOpinion
      @CodeOpinion  3 роки тому +1

      @@dansharpe4535 If you're using a relational database, you might want to look at Marten or SqlStreamStore.

    • @dansharpe4535
      @dansharpe4535 3 роки тому

      @@CodeOpinion one last question, would you have an event for initial creation of the object (ie when a new product is created) or do you just use the constructor and apply one of the events then save?

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      @@dansharpe4535 There could be a specific event or many that must start at the beginning of a stream. It really depends on what it is. Eg, a warehouse product might start by ProductReceived. It can't be started by ProductShipped, since there isn't any yet.

  • @acegame1452
    @acegame1452 3 роки тому

    First of all thank you for all the videos you make. They are amazing and very much needed in today's ecosystem. Second, is it possible to get the codebase for this video. I am building a new microservice and I would really like to implement the concepts of this video. If it is possible for you to share, please let me know. Thanks

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      All the source code is available to my "Developer" channel members. Check out for more info.
      ua-cam.com/channels/3RKA4vunFAfrfxiJhPEplw.htmljoin
      If you don't have the JOIN button, you can support me on Patreon.
      www.patreon.com/codeopinion

    • @acegame1452
      @acegame1452 3 роки тому

      @@CodeOpinion Sure, I just did. Just so you know I did not support for the source code, but for the fact that you have been really helpful for me. Thanks again

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      @@acegame1452 I really appreciate that. Thank you.

    • @acegame1452
      @acegame1452 3 роки тому

      @@CodeOpinion one last thing. Can you please help me out for the topic I reached out in patreon messages.?

  • @juhairahamed5342
    @juhairahamed5342 Рік тому

    have 5 instances of account microservice which transfers the money from account A to account B and then updates the data in the Postgres database. My problem is
    A user sent five requests to the account service and all of my microservices are working in parallel each request went to all 5 services way but now the user doesn't have enough balance in the account and I am already checking if the user is having enough balance or not.
    but after 2 requests user doesn't have enough balance so I am in confusion how to check this and implement data consistency first before the request goes to another instance of the same microservice.
    How to get consistent currentQuantityOnHand value for different instances ..

    • @CodeOpinion
      @CodeOpinion  Рік тому

      You need to handle the concurrency. If you're talking specifically about event sourcing, most event stores have optimistic concurrency. I show EventStore as an example here: ua-cam.com/video/2Nk3kCCyu3A/v-deo.html

  • @surenot-ky6sy
    @surenot-ky6sy 3 роки тому

    I am just curious, why should we build the current state of the product by expensive computation of events and then struggling of the expenses making snapshots and then managing these snapshots?
    It seems more simple way is to store current object state and have a log of events with the same explanation of the reason of changes as supplementing part of the main object. Anyway most of the time we need the current state of the object and rarely path how we get there. So im my opinion it is more greenish way to waste heat of the computation power.

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      You could store current state/snapshot in a separate stream and always start there when building up an aggregate. However it won't be your point of truth, the event stream would be. As long as your event stream is the point of truth, the rest are implementation details.

  • @ghevisartor6005
    @ghevisartor6005 3 роки тому

    Ok so i get concetps of the video in general, but does anyone know where can i start to learn about these topics? Like a starting point. Is it all from DDD?

    • @CodeOpinion
      @CodeOpinion  3 роки тому +1

      Event Sourcing and DDD are two different things. Check out this video if you want an overview of what Event Sourcing is: ua-cam.com/video/AUj4M-st3ic/v-deo.html

    • @ghevisartor6005
      @ghevisartor6005 3 роки тому

      @@CodeOpinion thanks a lot !

  • @aruns78
    @aruns78 3 роки тому

    How would the Snapshot handle/correct for out of order events or late arriving events?

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      Not sure why you would have out of order events in your event stream?

    • @aruns78
      @aruns78 3 роки тому

      @@CodeOpinion The event store may be built to support an edge application. Think IoT, network latency or upstream application outages can cause late arriving and out of order events. Straightforward replaying of events from the event store may not be impacted if aggregates take into consideration event time stamps and starting from event 0. If snapshots are sequenced based on version#, the complexity is with correcting snapshots. A thought is to write 2 snapshots every 1000th event; one for 500 and one for 750. Build aggregate based on n-2 or 500. Have you had a need for this or something similar and had some solution?

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      @@aruns78 I'd be interested in if you can get the event stream to be in the correct order by using a type of defer?

    • @aruns78
      @aruns78 3 роки тому

      @@CodeOpinion Thanks, that is some to consider. BTW, thank you for posting these videos, it's great material and very well presented.

  • @rimbik1
    @rimbik1 2 роки тому

    super ❤
    concept clear like water.

    • @CodeOpinion
      @CodeOpinion  2 роки тому +1

      Thank you 🙂

    • @rimbik1
      @rimbik1 2 роки тому

      @@CodeOpinion solace eda summit is live

  • @coffee.coyote
    @coffee.coyote 3 роки тому

    Great video! Could you show us also how to create efficient search feature over current state of aggregators?

    • @CodeOpinion
      @CodeOpinion  3 роки тому

      Not entirely sure what you mean? Could you clarify a bit more?

    • @coffee.coyote
      @coffee.coyote 3 роки тому

      @@CodeOpinion so e.g. in our app we have products that have a price property on them. Our store has a wide range of products (thousands or even millions). The price may change over time. As a customer, I would like to find products ranging from $ 20 to $ 30. To do this, I need to find a snapshot on each of them and apply all events higher than the snapshot. Is there a more effective way to do this?

    • @CodeOpinion
      @CodeOpinion  3 роки тому +2

      @@coffee.coyote Check out my video on projections! ua-cam.com/video/bTRjO6JK4Ws/v-deo.html

    • @coffee.coyote
      @coffee.coyote 3 роки тому

      @@CodeOpinion Awesome, thanks

  • @asdasddas100
    @asdasddas100 2 роки тому

    I imagine this wouldn't be as useful for an application like Twitter? Or am I mistaken?

    • @CodeOpinion
      @CodeOpinion  2 роки тому +1

      Well depends how you think of Twitter from the trivial perspective of tweeting and a feed. From that perspective, probably not.

  • @JeffChentingwei628
    @JeffChentingwei628 Рік тому

    But it still has many versions of snapshots...

  • @JamesGardner159
    @JamesGardner159 3 роки тому

    Great video!

  • @raghuveerdendukuri1762
    @raghuveerdendukuri1762 3 роки тому

    The version thought process that is mentioned in here is to identify the event upto which data is used to create the snapshot. On other hand, Greg Young in the following url (ua-cam.com/video/FKFu78ZEIi8/v-deo.html) suggested to use versioning thought process so events can be versioned and thereby data transformation to be applied so data becomes compatible or near compatible to the most recent event version.
    In cases when such attempts are not possible, then he suggested to maintain both copies as a better possibility.
    please share your views on versioning of events especially in scenarios when we cannot take systems down for data transformation kind of...

    • @CodeOpinion
      @CodeOpinion  3 роки тому +2

      I'm talking about version as an ordered number of events in the stream. Not in versioning in the sense of making changes to the event types, similar to versioning an API. I will cover event versioning in future videos.

    • @raghuveerdendukuri1762
      @raghuveerdendukuri1762 3 роки тому

      @@CodeOpinion agree and thanks