Using Domain-Driven Design to to Create Microservice App
Tags: softwareengineering, architecture, microservices, eventdriven
Published: 2024-06-24 07:18:01

Creating scalable and maintainable applications remains a constant challenge in the software development industry. As digital ecosystems grow more complex and user demands evolve rapidly, developers face increasing pressure to build systems that can adapt and expand seamlessly.

Scalability presents a multifaceted challenge. Applications must handle growing user bases, increasing data volumes, and expanding feature sets without compromising performance. This requires careful architectural decisions, efficient resource utilization, and the ability to distribute workloads effectively across multiple servers or cloud instances.

Maintainability, on the other hand, focuses on the long-term health of the codebase. As applications grow in complexity, keeping the code clean, understandable, and easily modifiable becomes crucial. This involves creating modular designs, adhering to coding standards, and implementing robust testing strategies. A maintainable codebase allows for faster bug fixes, easier feature additions, and smoother onboarding of new team members.

The intersection of scalability and maintainability often leads to trade-offs. Highly optimized systems for scalability may sacrifice code readability, while overly modular designs for maintainability might introduce performance overhead. Striking the right balance requires deep domain knowledge, technical expertise, and a forward-thinking approach to software design.

The rapid pace of technological change adds another layer of complexity. Developers must create systems that not only meet current needs but also remain flexible enough to incorporate future technologies and methodologies. This forward-looking approach is essential in preventing technical debt and ensuring the longevity of the application.

Rapid pace of technological change adds another layer of complexity. Developers must create systems that not only meet current needs but also remain flexible enough to incorporate future technologies and methodologies. This forward-looking approach is essential in preventing technical debt and ensuring the longevity of the application.

Domain-Driven Design, first introduced by Eric Evans, emphasizes aligning software design with business needs. It provides a set of patterns and practices that help architects and developers create flexible, modular systems that can evolve with changing requirements. By centering the design process around the core domain and domain logic, DDD facilitates the creation of software that truly reflects the underlying business model.

Domain-Driven Design provides a framework for creating systems that are both scalable in terms of functionality and maintainable in terms of code organization. It offers strategies for managing complexity, facilitating communication between technical and non-technical stakeholders, and creating flexible systems that can evolve with changing business needs.

# Domain-Driven Design Quick Overview

Domain-Driven Design (DDD) is a software development approach that places the project's core focus on the domain and domain logic. Introduced by Eric Evans in his seminal book, DDD provides a set of principles and patterns for creating complex software systems that closely mirror the business domain they serve.

At the heart of DDD lies the concept of the **ubiquitous language**. This shared vocabulary, meticulously crafted by developers in collaboration with domain experts, forms the foundation of the model. It ensures that all stakeholders, from business analysts to programmers, communicate effectively using terms and concepts directly related to the domain. This alignment significantly reduces misunderstandings and helps create software that truly reflects business needs.

**Bounded contexts** represent another cornerstone of DDD. These are explicit boundaries within which a particular domain model applies. In large systems, different parts may have different domain models, each with its own ubiquitous language. Recognizing and defining these boundaries helps manage complexity and allows teams to work independently on different parts of the system.

**Aggregates** are a key tactical pattern in DDD. An aggregate is a cluster of domain objects treated as a single unit for data changes. The aggregate root, a single entity within the aggregate, ensures the consistency of changes within the aggregate and controls access to its members. This pattern is particularly useful in defining clear boundaries for transactions and maintaining data integrity.

**Domain events** are another crucial concept in DDD. These represent significant occurrences within the domain. By modeling important changes as events, DDD facilitates loose coupling between different parts of the system, enabling more flexible and scalable architectures.

In the context of microservices architecture, DDD proves exceptionally valuable. The bounded contexts in DDD often align well with individual microservices, providing a natural way to decompose a complex system into manageable, independently deployable services. This alignment helps in creating a modular system where each service has a clear responsibility and a well-defined interface.

The strategic patterns of DDD, such as **context mapping**, help in understanding and defining the relationships between different bounded contexts or microservices. This is crucial for managing the complexity that arises from distributed systems and ensuring that the overall system remains coherent.

By applying DDD principles, developers can create software that's not only aligned with business needs but also inherently more maintainable and scalable. The focus on the core domain ensures that development efforts are concentrated where they provide the most value. The clear boundaries and well-defined interfaces facilitate easier changes and extensions to the system over time.

In the following sections, we will explore how these DDD concepts can be applied to create a Twitter-like application using a two-microservice architecture. This practical example will demonstrate how DDD can guide the design of complex systems, resulting in a flexible and maintainable solution.

# Defining the Bounded Contexts

In applying Domain-Driven Design (DDD) to our Twitter-like application, the first step is to identify and define the bounded contexts. Bounded contexts are crucial in DDD as they delineate the boundaries within which a particular domain model applies. For our simplified Twitter-like platform, we'll focus on two primary bounded contexts, each corresponding to a microservice: `UserManagementService` and `MessagingService`.

## UserManagementService Bounded Context

The `UserManagementService` encompasses all aspects related to user accounts and profiles. This context is responsible for managing user identities, authentication, and user-specific information. Within this bounded context, the core concepts include:

- **User** - the central entity representing an individual account on the platform, which contains user-specific details such as display name, bio, and profile picture, authentication information like username and password, etc.
- **Role** - defines user role in the whole system, whether he's a messages producer or messages consumer only

The **ubiquitous language** within this context includes terms like "register", "login", "logout", "authentication" and "authorization". The `UserManagementService` operates independently, focusing solely on user-related operations without direct concern for messaging or content creation.

## MessagingService Bounded Context

The `MessagingService` handles all aspects of content creation, distribution, and consumption. This context is more complex as it manages both the creation of messages (tweets) and the subscription system. Key concepts in this bounded context include:

- **Message**: The core entity representing a piece of content (similar to a tweet).
- **Subscription**: Represents the follow relationship between users.
- **Feed**: A collection of messages from subscribed users.
- **Producer**: A user who creates content (messages).
- **Consumer**: A user who consumes content from their subscriptions.

The ubiquitous language here includes terms like "post message", "subscribe", "unsubscribe", "consume message". This context deals with the dynamic aspects of the platform, managing the flow of content between users.

## Context Interactions

While these bounded contexts are separate, they do interact. The `MessagingService` needs to reference users, but it does so without delving into the internal complexities of user management. Instead, it might use a simplified representation of a user, containing only the necessary information for messaging purposes.

![Context Interactions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm3m0vfz5zt36z3asd99.png)Figure 1. Context Interactions between bounded contexts

For instance, when a user posts a message, the `MessagingService` doesn't need to know about the user's password or email address. It only needs a user identifier and perhaps a display name. This separation allows each context to evolve independently while maintaining necessary connections.

This separation is further reinforced by implementing the database-per-service pattern. In this approach, each microservice maintains its own dedicated database. The `UserManagementService` has a database that stores comprehensive user information, including sensitive data like passwords and email addresses. On the other hand, the `MessagingService` database focuses on message content, user subscriptions, and a minimal set of user data required for its operations.

To maintain necessary connections between services, the `MessagingService` keeps a minimal replica of user data required for its operations. This typically includes the user ID only. If `MessagingService` requires some data related to the user, it may request that information from `UserManagementService`. This may be useful for checking user roles for example or user name to display.

Technically speaking, this approach introduces some data redundancy, however organized correctly, such a redundancy may be reviewed as nothing much but as foreign keys used between different tables in RDBMS.

It allows each service to operate independently most of the time, enhancing overall system resilience. It also aligns well with the DDD principle of respecting bounded context boundaries, as each service has full control over its own data model and storage.

## Context Mapping

To manage the relationship between these bounded contexts, we employ context mapping. In this case, we might use a Customer-Supplier relationship, where the `UserManagementService` (supplier) provides necessary user data to the `MessagingService` (customer). This could be implemented through API calls or message queues, ensuring that each service has the information it needs without tightly coupling the two contexts.

Implementation of this context mapping involves several key aspects. First, the `UserManagementService` exposes a well-defined API that the `MessagingService` can use to retrieve essential user information. This API is designed to provide only the necessary data, such as user IDs and user roles, without exposing sensitive information. For example, when a new message is posted, the `MessagingService` might make an API call to the `UserManagementService` to verify the user's existence and his role.

![Context Mapping](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lh3a5qlwnltsdi37q8hk.png)Figure 2. Context Mapping between different bounded contexts

As part of the Customer-Supplier relationship, we define clear Service Level Agreements (SLAs) between the services. These SLAs specify the expected request and response structure, timeline for API calls, the frequency of event publications, and the handling of service outages.

Leveraging these patterns we will create a foundation for a modular, maintainable system. Each context has a clear responsibility and a well-defined interface, allowing for independent development and scaling. This separation also provides flexibility for future expansions, such as adding new features or integrating with external systems.

In the following sections, we'll delve deeper into each of these bounded contexts, exploring their domain models, aggregates, and services in detail.

# User Management Service

The `UserManagementService` forms a crucial part of our Messaging application, handling all aspects related to user accounts and roles. This service embodies its own bounded context, focusing solely on user-related operations. Let's delve into the key components of this service, structured according to Domain-Driven Design principles.

## Domain Model

At the core of the `UserManagementService` lies the User aggregate. In DDD, an aggregate is a cluster of domain objects treated as a single unit. The User aggregate encapsulates all user-related data and behavior.

The User aggregate root contains essential attributes such as `userId`, `username`, `email`, and `password`. It also includes methods for user authentication, user roles and user authorization. By centralizing these responsibilities within the User aggregate, we ensure that all user-related operations maintain data consistency and adhere to business rules.

Associated with the User aggregate are several value objects. These are immutable objects that describe characteristics of the User but have no conceptual identity. Examples include Email and Password. These value objects encapsulate validation logic, ensuring that email addresses are properly formatted in accordance with [RFC 2822](https://datatracker.ietf.org/doc/html/rfc2822#section-3.4) and passwords meet security requirements by its length and other criteria.

The Profile value object represents user-specific details like displayName, bio, and profilePictureUrl. By modelling Profile as a separate value object, we allow for easy expansion of profile-related features without cluttering the User aggregate.

## Repository

The `UserRepository` interface defines methods for persisting and retrieving User aggregates. This abstraction allows us to separate the domain model from the underlying data storage mechanism. The implementation of this repository interfaces with our chosen database system, in this case, MySQL.

The repository includes methods such as findById, findByUsername, and regular CRUD operations (CREATE-READ-UPDATE-DELETE). It also provides more specific query methods like findByEmail, which might be used during the user registration process to ensure email uniqueness.

Data model, which is also scoped to User aggregation only, might be represented as on the figure below.

![UMS Data Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gsgsnvi5ud053y1adkm.png)Figure 3. User Management Data Model

## Domain Services (Domain Flows)

While most business logic resides within the User aggregate, some operations involve multiple aggregates or require external resources. For these, we use domain services which may exist as separate services or might be defined as modules inside a single service. As of now we will prefer to define those operations as separate reusable modules inside one service.

The `UserRegistrationModule` handles the process of user registration, which might involve checking for existing users, validating input, and triggering welcome emails. This module coordinates between the User aggregate, UserRepository, and potentially external services like email providers.

The `AuthModule` manages user login processes, including password verification and generation of authentication tokens, as well as handles requests to identify user roles. This module works closely with the User aggregate but keeps the authentication and authorization logic separate, allowing for easier updates to authentication and authorization mechanisms in the future.

`ApplicationGatewayModule` is responsible for communication with other external components and services. In some other sources it is also called a Controller, however we try to include here only logic, responsible for terminating incoming traffic, data serialization, input validation and forming responses to return them to the requesters. This module acts like a communication facade between different external services and internal modules, providing SLA for them, and at the same time keeping flexibility of  internal modules.

The last module we may want to use is `DataAccessLayerModule`. We already defined the data model, now we need to determine the way to communicate with it. Using direct calls to the database from any point of the code is possible but may not be considered as a good idea because of many reasons, main ones are correlated to the high coupling and low cohesion between data model and application logic. Using this module we may decompose and abstract access our logic from accessing data. Instead of creating a specific SQL query to select specific user and access his permissions, we may simply create a method, called `getUserWithRoles`, and hide the implementation behind it. In that case we reach high consistency by calling the only one method, which we will be a single point to access data.

![Domain Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8cc29z5cg11vtz4hqw7.png)Figure 4. Block Diagram of Domain Modules

## Infrastructure Concerns

While not strictly part of the domain model, the `UserManagementService` also addresses several infrastructure concerns. These include implementing security measures like password hashing and token-based authentication, setting up database connections and ORM mappings, and configuring API endpoints.

The service also implements event publishing for significant domain events. When a user updates their profile, for instance, the service publishes a UserProfileUpdated event. Other services, like our further considering `NotificationService`, can subscribe to these events to maintain consistency across the system and deliver different state changes.

By structuring the `UserManagementService` according to DDD principles, we create a robust, maintainable service that clearly encapsulates all user-related functionality. This design allows for easy extension of user management features and provides a solid foundation for our Messaging application.

# MessagingService

The MessagingService forms the core of our Messaging application's content creation and distribution system. This service encompasses its own bounded context, managing messages (tweets) and subscriptions. Let's explore the key components of this service, structured according to Domain-Driven Design principles.

## Domain Model

The central aggregate in the `MessagingService` is the Message. This aggregate encapsulates the content created by users, along with metadata such as creation time and author information. The Message aggregate root contains attributes like `messageID`, `content`, `authorID` (which is a `userID`), and `createdAt`. It also includes methods for creating, editing (if allowed), and deleting messages.

Another crucial aggregate is the `Subscription`, representing the follow relationship between users. The Subscription aggregate contains a `producerID` and a `subscriberID`, which are no more than `userID` correlated to the appropriate data from the `UserManagementService`, along with subscription status and creation date. This aggregate is responsible for managing the relationships between content producers and consumers.

Value objects in this context might include `MessageContent`, which encapsulates the actual text or media content of a message and its author ID, to establish and determine the subscription status.

## Repository

The `MessagingService` might include several repositories in case we would like to split it into standalone services to manage messages and subscriptions. In this article we consider that functionality as closely related and manage it under the single umbrella of the `MesagingService` and within the single database for this service. 

The MessageRepository handles persistence and retrieval of Message aggregates. It includes methods like regular CRUD, `findByMessageId`, `findByAuthorId`, and `findBySubscriberId`. We may also implement another method allowing users to filter messages by the time when they were created, which may be another convenient use case, and call this method as `findByCreatedAt`.

As it is seen, this database includes all those entities related to the messages and subscriptions. It's schema might be represented like on the figure below.

![Messaging Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jz7i2fy1p7giil23axrh.png)Figure 5. Messaging Service Data Model

## Domain Services (Domain Flows)

In this service we may define the same amount of modules as in the `UserManagementService` - 4, where 2 of them would be identical - `DataAccessLayerModule` and `ApplicationGatewayModule`, as these modules' responsibilities are generic and service agnostic. These 2 modules are intended provide border modules for data communication between `MessagingService` other ones. Eventually their goal is to convert and adjust incoming or outgoing data with its internal representation happening inside this Domain.

`MessagingModule` in this case this is the core module for the whole service as it manages messages creation, retrieval and editing (if supported). This modules accept data from different sources, including user input, like message's text, user data, obtained from `UserManagementService` by using User ID, provided by user and using internal logic performs the requested operation - create, retrieve or edit (if supported) message.

The `SubscriptionModule` handles the complex logic of managing user subscriptions, including creating new subscriptions, handling unsubscribes, and managing subscription statuses like muting or blocking.

![Domain Flows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvqxv8n0chiynx01jjb7.png)Figure 6. Block Diagram of Domain Modules

## Infrastructure Concerns

The `MessagingService` implements several infrastructure-level features to support its operations, as concerns for this service should be different than for the `UserManagementService`. These include setting up message queues for handling high volumes of new messages, implementing caching mechanisms for frequently accessed subscriptions, and managing database connections for efficient data retrieval.

The service also may publish domain events such as `MessageCreated` and `SubscriptionChanged` to be consumed by other parts of the system or external services for features like notifications or analytics.

By structuring the MessagingService according to DDD principles, we create a robust and scalable system for handling the core functionality of our Messaging application. This design allows for efficient management of messages, subscriptions, while providing clear boundaries for future extensions and optimizations.

# Scalability and Performance Considerations

As our application grows in popularity, scalability and performance become critical concerns. This section explores strategies to ensure our microservices architecture can handle increasing loads while maintaining responsiveness and reliability. Generally speaking those techniques below are not critical for application functionality, however they become those when the system starts experiencing the high load.

## Horizontal Scaling

Both the `UserManagementService` and `MessagingService` are designed to be stateless, allowing for easy horizontal scaling. As user traffic increases, we can deploy multiple instances of each service behind a load balancer. This approach distributes the load across multiple servers, improving overall system capacity and resilience.

## Database Scaling

As the volume of data grows, database performance can become a bottleneck. To address this, we may implement several strategies for that.

For the `UserManagementService`, we establish a system of read replicas, as this service load assumes prevail retrieval operations over writing ones. This setup allows us to distribute read operations across multiple database instances, significantly reducing the load on the primary database. By directing read-heavy operations to these replicas, we ensure that the primary database can focus on write operations, thereby improving overall system performance and responsiveness.

The `MessagingService`, which deals with a substantially larger volume of data, requires a more robust scaling solution. Here, we may implement database sharding, a technique that involves partitioning data across multiple database instances based on user ID or message ID ranges. This approach not only distributes the data itself but also spreads the query load across multiple servers. As a result, we can handle a much larger number of concurrent operations and store a greater volume of data than would be possible with a single database instance.

In addition to these structural changes, we should place a strong emphasis on query optimization and indexing. Our database administrators and developers work in tandem to continuously monitor query performance, identifying frequently used query patterns and ensuring that appropriate indexes are in place to support them. This ongoing process of refinement helps maintain database efficiency even as the volume and complexity of data grow.

## Caching Strategies

Caching plays a pivotal role in performance optimization strategy, serving as a crucial layer between our services and the database. In the `UserManagementService`, we implement a sophisticated caching mechanism for user profile data. We may utilize any in-memory storage, like Redis, for high-performance in-memory data store, where we may cache frequently accessed user information. This approach significantly reduces the number of database queries required for common operations, such as retrieving user details for display or authentication purposes.

The `MessagingService` employs a more complex, multi-tiered caching strategy to handle the high-volume, real-time nature of social media feeds. We may use a combination of in-memory caching with Caffeine for the most recent and frequently accessed feed entries, providing near-instantaneous access to this hot data. For older or less frequently accessed feed data, we leverage Redis as a second-level cache. This tiered approach allows us to balance the need for ultra-fast access to recent content with the ability to efficiently store and retrieve a larger volume of historical data.

As our system scales horizontally, maintaining cache consistency across multiple service instances becomes critical. To address this, we configure Redis in cluster mode, ensuring that our caching layer is both distributed and consistent. This setup allows us to scale our caching infrastructure in tandem with our application services, maintaining performance as user numbers grow.

## Eventual Consistency and CAP Theorem Considerations

In designing our distributed system, we've had to grapple with the fundamental trade-offs described by the CAP theorem, which states that in the event of a network partition, a distributed system must choose between consistency and availability. Given the nature of our application, where the immediacy of information flow is a key feature, we've opted to prioritize availability and partition tolerance over strict consistency in many scenarios.

![CAP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p7o1a3fwodl3k74f9gv.jpg)Figure 7. CAP Theorem Problem

This decision manifests in our embrace of eventual consistency. For instance, when a user updates their profile information in the `UserManagementService`, we acknowledge and apply this change immediately in that service. However, we accept that there may be a short delay before this update is reflected in the `MessagingService` or in cached data across the system. During this brief period, different parts of the system may have slightly different views of the user's data.

While this approach introduces the possibility of temporary inconsistencies, it allows our system to remain highly available and responsive, even in the face of network issues or high load. We mitigate the impact of these inconsistencies through careful system design, such as including timestamps with data updates to help resolve conflicts, and by setting appropriate user expectations about the speed of data propagation.

This eventual consistency model extends to other areas of our system as well, such as follower counts or message distribution. By relaxing the requirement for immediate, system-wide consistency, we can process high volumes of operations more efficiently, leading to a more scalable and responsive system overall. However, we're also careful to identify those areas of our application where strong consistency is necessary, such as in financial transactions or security-critical operations, and design those components accordingly.

# Conclusion

The journey of creating a Twitter-like application using Domain-Driven Design (DDD) and microservices architecture offers valuable insights into building complex, scalable systems. Through our exploration of the `UserManagementService` and `MessagingService`, we've demonstrated how DDD principles can guide the development of a robust, maintainable, and flexible application architecture.

By focusing on clearly defined bounded contexts, we've created services that are independently deployable and scalable, yet cohesive in their overall functionality. The use of aggregates, repositories, and domain services has allowed us to encapsulate complex business logic within each service, promoting code that is both more understandable and more closely aligned with the underlying business domain. 

Our approach to data management, including the implementation of the database-per-service pattern and sophisticated caching strategies, showcases how modern distributed systems can handle large volumes of data while maintaining performance and responsiveness. The adoption of event-driven architecture for inter-service communication highlights the power of loose coupling in creating systems that are both resilient and adaptable to change.

Throughout this process, we've had to make important architectural decisions, balancing concerns such as data consistency, scalability, and system complexity. Our choice to embrace eventual consistency in certain areas of the application demonstrates the nuanced thinking required when designing distributed systems, taking into account the trade-offs described by the CAP theorem.

While our implementation focuses on core functionalities, omitting features like notifications and commenting, the architecture we've designed provides a solid foundation for future expansion. The principles and patterns we've applied can be extended to accommodate new features and services as the application grows.

It's important to note that the journey doesn't end with implementation. Continuous monitoring, performance optimization, and iterative improvement are crucial for maintaining and evolving such a system. As user behavior changes and new technologies emerge, our Twitter-like application must be ready to adapt and scale accordingly.

The combination of Domain-Driven Design and microservices architecture offers a powerful approach to building complex, scalable applications. By focusing on clear domain boundaries, embracing eventual consistency where appropriate, and leveraging modern technologies for data management and inter-service communication, we can create systems that are not only capable of handling current demands but are also well-positioned to evolve with future needs.