Search code examples
microservicesdomain-driven-designcqrsevent-sourcing

What should be projection primary key on query side - CQRS, Event Sourcing, Microservices


I have one thing that confuses me. I have 2 microservices. One creates commands and other consumes commands and produces events (events are stored in Event Store).

In my example aggregates have Guid as Entity ID, and Guid is created when aggregate is created.

Thing that confuses me is, should that key (generated on write side) be transfered via Event to query side (microservice that created command)? Or maybe query side (projection) should have separate id in read DB. Or maybe I should generate some shared key?

What is best solution here?


Solution

  • I think it all depends on your setup.

    If you are doing CQRS, and you have a separate read-service (within the same bounded context), then it is up to the read-side service to model the data as it wish, either reusing the same keys or not.

    If you are communicating between two different services (separate bounded contexts) then I recommend you create new primary keys in the receiving service and use the incoming key as a foreign key. Just as you would do with relationships between two tables in a SQL-database.