Event Sourcing
Last updated
Last updated
The concept of event sourcing is to store (persisted) all the change history of the business entity and arranged according to the chronological order of the changes (time-ordered sequence).
When an entity's status is changed, a message event
will be sent to save the entity's change history. We can persist the change immediately when the event arrives, or snapshot
periodically if the entity has a large number of events that we want to optimize. Event Sourced systems such as finance, banking or insurance are frequently used, the purpose is to store all changes in the system so that they can be processed later. this if needed.
Its strength is that we can reconstruct the state of the entity at any time, by replaying all the events that occurred up to that time. This is useful in reproducing bugs or rolling back the entity's state if an error occurs. However, this replaying all must be done with caution, because it is very possible that when replaying events command
that affect other services, for example the OrderPlaced event is emitted, it is very possible that the OrderFulfilled event will be executed. appear more than once.
Event sourcing is often used as part of CQRS to synchronize changes Command
with Query
. However, not all systems must have sourced events, but it depends on each bounded context
Aggregate's business or operations. Sourcing events should not be published outside their bounded context to avoid unnecessary dependencies between bounded contexts.