aggregatedomain-driven-designbounded-contexts

Can we use a Database per aggregate in DDD


I am designing an enterprise application and there is a big question for me if it is ok to use one Database per each aggregates in Domain-Driven Design and apply CQRS for them.

For example I have one Domain that contains several Bounded Context and each BC have two or more aggregates, so can i use a relational Database like MSSQL and no-sql Database like MongoDb for one or more aggregate?


Solution

  • The concept (Domain-Driven Design) do not discuss the exact implementations. So it does not limit the use of database implementations. Go ahead with what you are trying if it suits your use case. The only thing is to go through some planning ahead for design, which can change if required sometime later. I would recommend having event sourcing in the blend as well. It'll really help the through denormalization in the mix with CQRS.

    The main concern is to take care of commands reflecting state consistently through all databases. For example, if you have one aggregate root having an entity and some value objects spread over multiple databases, make sure that all the adapters behave similarly so that the domain then has no concern over how the data is stored (separated) across databases. If that is achieved neatly, then domains are free to have only domain logic. I mean this in terms of how the interfaces are designed for multiple databases. If the NoSQL DB interface shows methods that convey documents and the SQL DB shows it works on the tables, the domain will definitely take a hit switching between documents and tables. Abstract that logic (may be using Hexagonal architecture) and you're in a good position with multiple DBs.