Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you have one-database-to-rule-them-all setup or separated database for each bounded context?

In DDD, as far as I understand it, it helps or guides you on how to structure complex application. Now in an application, you should identify your Bounded Context. Say you have more than 10 BCs.

I read somewhere (forgive me I cannot give any links), that its not ideal to have 1-big database for a complex application. That it should be separated for each BC. If that's the easier route to take. How should one structure an app if each BC have their own database.

I tried searching on github but cannot find one.

like image 337
Boy Pasmo Avatar asked Apr 21 '16 17:04

Boy Pasmo


1 Answers

It depends if they only share the same database or also some tables - i.e. data.

Sharing a database but not tables can be perfectly fine. Except if you aim for scalability and intend to make your BC's independently deployable and runnable units like microservices, in which case they should probably have their own data store instance.

I see a few more drawbacks to database tables shared by 2 or more Bounded Contexts :

  • Tight coupling. The reason we have distinct BC's is that they represent different domain spaces that are likely to diverge their own way. Changing a concept in one of the BC's might impact the underlying table, forcing the other BC's that use this table to change as well. You get rigidity where there should be suppleness. You might also have inconsistencies or "holes" in the data due to the multiple possible sources of change.

  • Concurrency. In highly concurrent systems, some entities and the tables underneath are subject to strong contention. Bounded Contexts are one of the ways to lighten the load by separating different types of writes, but that only works if they don't lock the same data at the end of the day. Same is true for reads in non-CQRS systems where they query the same database where writes are done.

  • ORM friendliness. Most ORMs won't allow you to map to 2 or more classes from the same database table without a lot of convolutions and workarounds.

How should one structure an app if each BC have their own database.

To some extent (e.g. that may include the UI layer or not), just as if you had multiple separate applications. Please be more specific if you have precise questions in mind.

like image 156
guillaume31 Avatar answered Sep 18 '22 16:09

guillaume31