oskardudycz / EventSourcing.JVM

Examples and Tutorials of Event Sourcing in JVM languages
Creative Commons Attribution Share Alike 4.0 International
271 stars 41 forks source link

How to add multiple products at once #10

Closed baranowskikamil closed 2 years ago

baranowskikamil commented 2 years ago

Hey. Can you explain (and maybe add some example) how to add multiple products at once? In relational database I have transaction and warranty that all entities will be saved (or nothing if exception occurs for at least one). How EvenStoreDb handle it? Is it possible to create multiple (or none) streams?

oskardudycz commented 2 years ago

@baranowskikamil, typically, the best option is to try to keep all of the data within a single stream. E.g. if you have a shopping cart, you can have a stream per instance. Having that, you can add more than one product as a single operation and put this information either into a single event or a series of events. Event stores support adding multiple events to the same stream as an atomic operation.

Event stores (usually, or at least ESDB) cannot do cross-stream transactions. Also, keeping all in a single stream is not always the best idea. Typically it's better to keep them (and aggregates) granular. The best option is to model the specific process as an aggregate with a dedicated stream and connected saga/process manager. I added a sample showing how to do a group (batch) checkout of the guests in the hotel in PR https://github.com/oskardudycz/EventSourcing.JVM/pull/24.

See the README and the code for a more detailed explanation: https://github.com/oskardudycz/EventSourcing.JVM/tree/main/samples/distributed-processes.