Search code examples
domain-driven-designaggregateroot

Populate aggregate root from multiple databases


I am a beginner in DDD and would like to understand if it is a bad practice to try to populate a single aggregate root from multiple database ?

I am trying to design a system where all the properties of an aggregate root are being populated from one database except the name. The name happens to be in a different database and needs to be populated in the aggregate root so that it can be used.

TIA


Solution

  • I am a beginner in DDD and would like to understand if it is a bad practice to try to populate a single aggregate root from multiple database ?

    Yes. The happy path isn't the expensive one.

    Reads aren't necessarily an issue; if all you are doing is producing a report, pulling data from multiple sources is fine (although we of course recognize that those sources might not be "consistent" with each other.

    But trying to manage writes with two databases, it becomes difficult to ensure the integrity of your data when things start going wrong - a crash in the middle of your update does not leave your data in a healthy state.

    The good news? If the data is already spread across two databases, it is likely that you have two aggregates to model, rather than one. It is perfectly normal to take some concept like "customer" and share it among multiple aggregates.

    Once you've got your aggregate boundaries correct, you can arrange the aggregates among your databases in any way that makes sense; each modification in the model updates one database per transaction, and the business invariant is maintained.

    Mauro Servienti's talk All Our Aggregates Are Wrong may be a useful starting point.