How DDD Layered Architecture Transforms Microservices Design
This article explains the principles, types, and layer responsibilities of DDD layered architecture, compares it with traditional four‑layer and three‑layer designs, and shows how microservices can evolve by reorganizing aggregates and applying dependency inversion across UI, application, domain, and infrastructure layers.
DDD Layered Architecture Overview
Domain‑Driven Design (DDD) layered architecture separates a system into four explicit layers—User Interface, Application, Domain, and Infrastructure—to achieve high cohesion and low coupling, especially in microservice environments.
Core Principle
Each layer may only depend on the layer directly below it. Direct access from a lower layer to a higher layer is prohibited; interaction can occur only via patterns such as Observer or Mediator .
Layer Types
Strict Layers Architecture : a layer can interact only with its immediate lower layer.
Relaxed Layers Architecture : upper layers may also depend on lower layers (e.g., UI needing infrastructure services). Most real‑world systems adopt this style.
Traditional vs Optimized Four‑Layer Architecture
Traditional layout (bottom‑up): UI → Application → Domain → Infrastructure. The Infrastructure layer resides at the bottom and provides persistence, messaging (MQ, SMTP, SMS), file storage, caching, etc.
Optimized layout (top‑down): Infrastructure implements interfaces defined by higher layers, thereby inverting the dependency direction. This follows the Dependency Inversion Principle (DIP) and makes the system easier to refactor.
When applying DIP, the Infrastructure layer should depend on abstractions (interfaces) declared in the Domain or Application layers, not on concrete business types.
Responsibilities of Each Layer
2.1 User Interface Layer
Handles user interaction, request routing, and presentation concerns.
Performs validation that is distinct from domain validation (e.g., format checks, authentication).
Exposes APIs (REST, gRPC, etc.) for external clients; acts as the direct consumer of the Application layer.
2.2 Application Layer
Coordinates use‑cases, orchestrates multiple domain objects, and composes domain services.
Contains no core business rules; those belong in the Domain layer.
Implements cross‑cutting concerns such as security, transaction control, and event publishing.
Serves as a façade for UI changes and as an integration point between microservices.
2.3 Domain Layer
Encapsulates the core business logic, including entities, value objects, aggregates, and domain services.
Entities typically follow the rich (anemic) model, exposing behavior alongside state.
Domain services handle operations that span multiple entities or aggregates.
2.4 Infrastructure Layer
Provides technical services: database access, message brokers, external APIs, file systems, caching, etc.
Implements the interfaces defined by higher layers, enabling easy replacement of concrete resources.
Contains repository implementations; each aggregate has its own repository directory to simplify migration.
Microservice Evolution with DDD Layers
Aggregates are the primary unit of evolution. When an aggregate experiences high traffic, it can be extracted into a dedicated microservice. Conversely, aggregates may be merged or moved to another service as business boundaries shift.
Extract a hot‑spot aggregate into its own service to improve scalability.
Merge an aggregate into a service where its domain model aligns better.
Iteratively refactor the set of aggregates, e.g., {a,b,c} → {b,c,d}.
Service Evolution Inside a Microservice
Domain services are composed into application services, which are further composed into higher‑level services. When multiple application services repeatedly invoke the same ordered set of domain services, those domain services can be merged and moved down, reducing composition complexity.
Transition from Traditional Three‑Layer to DDD Layered Architecture
Traditional three‑layer architecture (Presentation, Business Logic, Data Access) is logical and centralized, unsuitable for distributed microservices. DDD re‑classifies these concerns:
Presentation becomes the User Interface layer, introducing DTOs for data transfer.
Business Logic splits into Application (use‑case orchestration) and Domain (core rules) layers.
Data Access moves to the Infrastructure layer via the Repository pattern, decoupled through interfaces.
Repositories belong to the Infrastructure layer, but each aggregate maintains its own repository directory to simplify future migration or database replacement.
Repository Pattern Details
For each aggregate:
src/main/java/com/example/aggregateA/
│ ├─ domain/ // entities, value objects, domain services
│ ├─ application/ // use‑case services
│ └─ infrastructure/
│ └─ repository/ // Repository interface (in domain) + implementation (in infrastructure)When the underlying database changes, only the implementation under infrastructure/repository needs to be replaced.
Key Benefits
Clear separation of concerns makes each layer independently testable.
Dependency inversion isolates business logic from technical details, enabling easy swapping of infrastructure components.
Microservice boundaries can evolve by moving aggregates without breaking other layers.
Changes to a layer affect only the adjacent upper layer, reducing regression risk.
Illustrative Diagrams
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
