DDD: Part IV (DDD and Microservices)
DDD: Part IV (DDD and Microservices)
We've all heard of the many benefits of microservices architecture, but how exactly does that relate to Domain-Driven Design? Read on to find out.
Join the DZone community and get the full member experience.Join For Free
Finally, I got a chance to write the fourth part! For those of you who didn't get the chance to read the previous parts, you can read them starting with Part 1 here.
In this fourth part, I will try to elaborate on the microservices architecture and its relationship with Domain Driven Design (DDD).
First things first: let's describe what microservices architecture is all about.
Microservices architecture is a kind of an architectural pattern that focuses on developing/building small, reusable, and scalable services. Microservices allow us to break our large system into a number of independent collaborating processes, thus it can help to avoid a big monolith application, which is difficult to maintain, especially if you have big projects with a big team.
Microservices also can be very useful when you have to create services for polyglot devices such as wearables, Internet of Things (IoT), mobile, desktop, and web.
Microservices itself is not a new term. It was coined in 2005 by Dr. Peter Rodgers, who mentioned micro web services based on SOAP. It has become more popular since 2010.
The keys to microservices architecture are:
- It is independently deployable in terms of stack, framework, and/or languages used.
- It is deployed as a small service that keeps focusing on doing one thing well, i.e. domain-driven services.
- Well-defined interfaces and minimal functionality.
- Avoiding cascading failures and synchronous calls — reactive designing for failure.
- Every service has its own data and uses events/messages to communicate with other services.
- Every service is deployed independently and isolated from other services (usually running in its own container) and automatically scalable.
The main goal is to provide you with a small set of independent services which run independently in different environments, but can communicate with each other (tight cohesion). As we know, coupling is a two-headed beast. On one side, tightly-coupled code is difficult to test, difficult to maintain/reuse, and typically exhibits “whack-a-mole” bug behavior (i.e. fixing one bug most likely will produce one or more new bugs). On the other side, a certain amount of coupling is also necessary since completely uncoupled code doesn’t do anything; thus, coupling is necessary but should be carefully managed. This is what we call “Loosely Coupled, Highly/Tightly Cohesive.”
- Easy to scale as individual components.
- Easy to deploy and reduce time deployment, i.e. you can deploy each module without interrupting any other module.
- Easy to maintain due to its smaller code size, and you can have a small, focused team.
- It’s difficult to achieve strong consistency in distributed services.
- It’s harder to debug and trace issues compared to monolithic applications.
There are many other challenges that you will know yourself later, once you implement microservices. Nevertheless, it will depend on how you design and manage your microservices by following some well-known best practices, like API Management, Traffic Management, Services Monitoring, etc. I'll try to encapsulate them later in another series, if I get the chance.
How Does DDD Relate to Microservices?
As mentioned earlier, in microservices, we build each service to serve only one thing and do one thing well. Each service is also isolated from the others. On this matter, DDD principles can help us to keep the scope of the service small through what it calls "bounded context."
Subsequently, DDD is going to help you investigate and know your domain and all subdomains well through the communication you build with the domain experts. By knowing your domain and subdomains well, you will know the map contexts and how all subdomains interact with each other, which will help you in designing and choosing the type of your microservices architecture and what kind of approaches you use to implement them, whether a reactive approach, orchestration approach, or hybrid... it will depend on your knowledge about the domain you're working on. There are pros and cons for each approach that need to be evaluated based on the project and your domain knowledge. DDD will help you make a decision on this matter.
That's all for now... please let me know if you have any more insight into how DDD relates to microservices!
Opinions expressed by DZone contributors are their own.