Event Sourcing: Rehydrating Aggregates with Snapshots

Поделиться
HTML-код
  • Опубликовано: 25 дек 2024

Комментарии • 89

  • @mrjamiebowman1337
    @mrjamiebowman1337 3 года назад +13

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

    • @CodeOpinion
      @CodeOpinion  3 года назад

      Thanks! Hope they're helpful.

  • @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.

  • @kevinohara80
    @kevinohara80 3 года назад

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

  • @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!

  • @andy_lamax
    @andy_lamax 3 года назад

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

  • @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.

  • @F2H16
    @F2H16 10 месяцев назад

    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 ?

  • @aaronderenard3527
    @aaronderenard3527 2 года назад

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

  • @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? ruclips.net/video/rO9BXsl4AMQ/видео.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 .

  • @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.

  • @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.

  • @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.

  • @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.

  • @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.

  • @rimbik1
    @rimbik1 2 года назад

    super ❤
    concept clear like water.

    • @CodeOpinion
      @CodeOpinion  2 года назад +1

      Thank you 🙂

    • @rimbik1
      @rimbik1 2 года назад

      @@CodeOpinion solace eda summit is live

  • @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 !

  • @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: ruclips.net/video/oJiUjiWeesQ/видео.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.

  • @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: ruclips.net/video/64ngP-aUYPc/видео.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

  • @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: ruclips.net/video/2Nk3kCCyu3A/видео.html

  • @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 ruclips.net/video/FKFu78ZEIi8/видео.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.

  • @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.
      ruclips.net/channel/UC3RKA4vunFAfrfxiJhPEplwjoin
      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.?

  • @JamesGardner159
    @JamesGardner159 3 года назад

    Great video!

  • @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.

  • @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.

  • @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: ruclips.net/video/AUj4M-st3ic/видео.html

    • @ghevisartor6005
      @ghevisartor6005 3 года назад

      @@CodeOpinion thanks a lot !

  • @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! ruclips.net/video/bTRjO6JK4Ws/видео.html

    • @coffee.coyote
      @coffee.coyote 3 года назад

      @@CodeOpinion Awesome, thanks

  • @asdasddas100
    @asdasddas100 3 года назад

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

    • @CodeOpinion
      @CodeOpinion  3 года назад +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...

  • @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 (ruclips.net/video/FKFu78ZEIi8/видео.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