Skip to content

Instantly share code, notes, and snippets.

@statianzo
Created January 16, 2012 21:49
Show Gist options
  • Save statianzo/1623210 to your computer and use it in GitHub Desktop.
Save statianzo/1623210 to your computer and use it in GitHub Desktop.

Aggregate

class Ship
  def load_cargo(name)
    apply :cargo_loaded, :name => name
  end

  def paint(color)
    apply :painted, :color => color
  end

  private
  def apply_cargo_loaded(e)
    @cargo = e[:name]
  end

  def apply_painted(e)
    @paint_color = e[:color]
  end
end

Scenario

  • Two LoadCargo commands(C1, C2) for Ship aggregate "abc" come in at the same time.
  • The command handlers(async) both load up the ship to the latest version (let's say 5).
  • C1 completes before C2 (making the aggregate version 6 at persistence level).
  • When C2 commits, a ConcurrencyException is thrown due to C2 only knowing about events up to version 5.
  • The ship's event stream repopulates up to 6.
  • The uncommitted events are compared against the newly loaded events. Is this comparison better as a blacklist or whitelist?
  • If there is a conflict, throw ConflictException and C2 never completes, otherwise, attempt C2 again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment