Ari Seyhun
Ari Seyhun

Reputation: 12531

Handling Correlation ID Changes in Event Sourcing When an Entity Switches Context

I'm working on an event-sourced application that crawls sports betting games from different bookmakers. I have two primary aggregates in my system:

Problem

I want to ensure that all events within a stream share the same correlation ID. Ideally:

This works well until a game's start time changes significantly (more than 30 minutes). In this case, the game is removed from its original combined game and placed into a new combined game bucket. The issue here is that the correlation ID from the original game stream becomes outdated, as it still points to the old combined game. Since correlation IDs shouldn’t be changed for previous events in a stream, there’s no straightforward way to update the correlation ID for future events related to this game.

Proposed Solution

One idea I had was to create a new stream when the game’s start time changes significantly. The old stream would emit a GameMovedToNewStream event, which points to the new stream. However, this results in the game’s event history being split across multiple streams.

Questions

I'm looking for advice on whether my current approach aligns with best practices in event sourcing or if there's a more elegant way to handle this situation.

Upvotes: 1

Views: 103

Answers (1)

Ari Seyhun
Ari Seyhun

Reputation: 12531

After reevaluating how I'm handling CombinedGame and BookmakerGame events within my existing projections, I've decided to change the approach.

Instead of having the bookmaker games share a correlation ID with the combined game, I instead duplicate the "game paired/combined" event in the BookmakerGame stream itself, within the same transaction as the original CombinedGame event. This removes the dependency on the combined game stream entirely.

So when a game is paired, two events are saved in a single transaction:

  • CombinedGame::GameAdded
  • BookmakerGame::GameAddedToCombinedGame

Upvotes: 0

Related Questions