In Event Sourcing, you store all individual Domain Events that have happened for one Aggregate instance, known as Event Stream. Along with Event Stream you also store a Stream Version.
Should the version be related with each Domain Event, or it should be related with transactional changes (aka commands)?
Example:
Our current state of Event Store is:
aggregate_id | version | event
-------------|---------|------
1 | 1 | E1
1 | 2 | E2
A new command is executed in aggregate 1. This command produces two new events E3 and E4.
Approach 1:
aggregate_id | version | event
-------------|---------|------
1 | 1 | E1
1 | 2 | E2
1 | 3 | E3
1 | 4 | E4
With this approach optimistic concurrency can be done by storage mechanism using unique index but replaying the events until version 3 could leave the aggregate/system in a inconsistent state.
Approach 2:
aggregate_id | version | event
-------------|---------|-----
1 | 1 | E1
1 | 2 | E2
1 | 3 | E3
1 | 3 | E4
Replaying the events until version 3 leave the aggregate/system in a consistent state.
Thanks!