Its been a while since microservices appeared as an attempt to answer challenges we face in modern application development. At the core, the microservices architecture is created around the principles of domain-driven design.
Let's understand what domain-driven design is and the guidelines it provides for software development.
Domain driven design (DDD)
Domain driven design is the philosophy behind microservices. It does not provide practical ways to implement software architecture but focuses on a few guiding principles which can help in building maintainable software. We will take the real world example of a pizza shop to understand these concepts.
Let's look at a few terms.
In simple terms, it is a subject around which our application is built. Every component in the application was chosen, programmed and deployed keeping in mind the needs of the domain.
The domain in our shop is the pizza and everything needs to be built around it. The chefs, the ingredients, the menu, the advertising boards, etc.
The setting around the domain. In our case, the shop becomes our context. Everything that is required to fulfil the needs related to the domain is encapsulated by the context.
Building blocks of the domain. The different parts which combine to solve a problem. In our case, the people in their different roles, the ingredients, the pizza, the furniture, the machines, etc. become our models.
The language and terminology that is used while talking about anything that comes under the context.
A subsystem or a division of responsibility. Every employee in the shop will have their own set of responsibilities. It is unlikely that the chef and the cashier switch roles from time to time and need to know in depth about each other's work.
So now that we have the terminology in place, let's take a look at the principles.
Principles of DDD
Model the software around the business domain
The business domain forms the basis of all architectural decisions. The business models and the software components should be mapped to each other. Whether a term is spoken by a developer or a business executive, it means the same. The final software is a reflection of how the business operates.
In our shop, if the owner of the shop uses the terms small, medium and large, it is recommended that the cashier also uses the same terms instead of describing pizza size in inches. It makes the conversations easier to understand for both parties.
Software evolves within a bounded context
A bounded context describes a boundary within which the subsystem needs to evolve and think. It should not be worried about how other bounded contexts change and not try to solve problems for them.
Pizza delivery can evolve without needing the consent of the chefs. Similarly, the delivery person does not dictate what ingredients need to be used in the kitchen. They take care of their own problems and make improvements to their work independently.
One subdomain does not corrupt the functioning of the other.
Build domains by prioritizing opinions of domain experts
The development team does not need to be ignorant of the needs of the business. They should understand the requirements from the business perspective first before thinking about the technical domain.
Domain experts have the responsibility of refining the requirements. They capture requirements of a domain and are a point of contact to resolve any ambiguities. Domain experts do not necessarily have to be non-technical. They can be anyone who has studied the domain closely and has experience in working with it.
When our shop needs an advertising board, the owner goes to marketing specialists and designers and does not decide the design of the banner himself. Nor does he let the actual banner maker take that decision.
Benefits of DDD
Let's look at the benefits of following domain-driven design
- Easier communication - because domain experts are guiding all conversations
- Flexibility - for each component to evolve independently
- Reduced misunderstanding - due to use of consistent language and terms.
- Better team co-ordination - due to narrowed focus areas
- Cleaner architecture - because the separation of concern reduces the risk of software components getting bloated.
When not to practice DDD?
DDD is not a silver bullet to solve all problems. It can often be overkill to practice it religiously. Some situations when you should 'doublethink' before using it:
- The software is not expected to grow rapidly
- The initial cost needs to be kept low
- When the time to deliver is a concern
Thanks for reading. I hope the article was useful to help you get an insight into Domain driven design.
You can find more about me here
Connect with me on Twitter for more content.
Top comments (5)
A problem occurs when customers keep telling the delivery guy that the pizzas have way to little cheese and the delivery guy don't let the chefs know.
Narrow focus is not always the best approach; at least not in a real world context. In the technical domain you can separate the "delivery guy model" into navigation, transport and customer interaction systems. The customer interaction system feeds data into the feedback store. The recepie builder system uses data from the feedback store to make adjustments to pizza recepies and continously update the recepie data store. The chef system in turn keeps using the recepie store when making pizza.
The recepie bilder also uses other data stores with data coming from the economy systems to make sure the recepies are profitable.
The real world is way to complex to be separated into clean concerns and need more open paths of communication. In a micro service architecture we can model this by using data stores that emulates shared knowledge. In real world we can't rely on the same clean collection and separation of data.
As an introduction it is a nice koncept with a restaurant as a domain since it is full of roles with somewhat narrow concerns. It is important to note though that it is not a 1:1 conversion and explain how real world communication translates into decoupled systems and data.
Nice post to repeat what the main aspects of DDD were.
I also think this gives a nice introduction to beginners who did not know DDD yet.
Thanks Marco. I'm planning to use more of these analogies to set the stage before getting into more technical Microservices articles.
Nicely written article, Abhinav!