pedroassumpcao/incident

Event Ordering Not Guaranteed

Closed this issue · 0 comments

Hey! Just wanted to file an issue here based on our slack conversation.

Based on https://github.com/pedroassumpcao/incident/blob/master/lib/incident/aggregate_state.ex#L19, events are currently ordered according to the value returned by DateTime.utc_now() at the time the event struct is created.

This is extremely unsafe, because event order is absolutely critical for the proper function of an EventSourced system.

  1. DateTime.utc_now() is not monotonically increasing. 3 calls in a row can return times in the past, present or future in any order. This is fine for
  2. Even if you use the monotonically increasing time calls, there is no guarantee that they will not return the same value several calls in a row, meaning you can't tell after the fact in which order the events happened.
  3. This won't work at all across nodes, where clocks can be easily and importantly out of sync.

Event order needs to be perfectly unambiguous. Given that you're storing events in postgres, a good old fashioned serial bigint column should work just fine. For the in memory backend you probably want :erlang.unique_integer([:positive, :monotonic])