Master software architecture patterns for scalable, maintainable systems. From monoliths to microservices, from MVC to hexagonal - choose the right structure for every challenge.
"Architecture is the art of making the complex manageable. Choose patterns that reveal intent, not hide it."
CODE is what the system DOES.
ARCHITECTURE is what the system IS.
Bad architecture makes good code hard to write.
Good architecture makes bad code easy to fix.
Architecture decisions are expensive to change.
Choose wisely. Change reluctantly.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STRUCTURAL PATTERNS β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
β β
β LAYERED β Horizontal separation of concerns β
β HEXAGONAL β Ports and adapters, domain-centric β
β CLEAN β Dependencies point inward β
β VERTICAL SLICE β Feature-based organization β
β MODULAR MONOLITH β Bounded contexts in single deployment β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DISTRIBUTED PATTERNS β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
β β
β MICROSERVICES β Independent deployable services β
β EVENT-DRIVEN β Async communication via events β
β CQRS β Separate read and write models β
β EVENT SOURCING β Store events, derive state β
β SAGA β Distributed transactions β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββ
β PRESENTATION LAYER β β UI, Controllers, Views
βββββββββββββββββββββββββββββββββββββββ€
β APPLICATION LAYER β β Use Cases, Services
βββββββββββββββββββββββββββββββββββββββ€
β DOMAIN LAYER β β Business Logic, Entities
βββββββββββββββββββββββββββββββββββββββ€
β INFRASTRUCTURE LAYER β β Database, External APIs
βββββββββββββββββββββββββββββββββββββββ
RULES:
β’ Dependencies flow DOWN only
β’ Each layer knows only layer below
β’ Domain layer has ZERO dependencies
When to Use:
β Traditional business applications
β CRUD-heavy systems
β Teams familiar with MVC patterns
β Moderate complexity
β Highly dynamic requirements
β Event-heavy systems
β Complex business logic
βββββββββββββββββ
β PRIMARY β
β ADAPTERS β
β (Controllers) β
βββββββββ¬ββββββββ
β
βββββββββΌββββββββ
β β
βββββββββ€ PORTS βββββββββ
β β (Interfaces) β β
β βββββββββ¬ββββββββ β
β β β
β βββββββββΌββββββββ β
β β β β
β β DOMAIN β β
β β (Core) β β
β β β β
β βββββββββ¬ββββββββ β
β β β
β βββββββββΌββββββββ β
β β β β
βββββββββ€ PORTS βββββββββ
β (Interfaces) β
βββββββββ¬ββββββββ
β
βββββββββΌββββββββ
β SECONDARY β
β ADAPTERS β
β (Repositories)β
βββββββββββββββββ
CORE INSIGHT:
Domain knows nothing about the outside world.
Adapters translate between domain and infrastructure.
When to Use:
β Domain-driven design projects
β Systems needing high testability
β Multiple input/output channels
β Long-lived business applications
β Simple CRUD applications
β Rapid prototypes
β Small teams with tight deadlines
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FRAMEWORKS & DRIVERS β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β INTERFACE ADAPTERS β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β APPLICATION LAYER β β β
β β β βββββββββββββββββββββββββββββββββββββββββββ β β β
β β β β DOMAIN LAYER β β β β
β β β β (Entities & Rules) β β β β
β β β βββββββββββββββββββββββββββββββββββββββββββ β β β
β β β (Use Cases) β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β (Controllers, Gateways, Presenters) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β (Web, UI, DB, External Interfaces) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
THE DEPENDENCY RULE:
Dependencies point INWARD only.
Inner circles know nothing about outer circles.
Traditional (Layered): Vertical Slice:
ββββββββββββββββββββ βββββββ¬ββββββ¬ββββββ
β Controllers β β F β F β F β
ββββββββββββββββββββ€ β E β E β E β
β Services β β A β A β A β
ββββββββββββββββββββ€ β β T β T β T β
β Repositories β β U β U β U β
ββββββββββββββββββββ€ β R β R β R β
β Database β β E β E β E β
ββββββββββββββββββββ β 1 β 2 β 3 β
βββββββ΄ββββββ΄ββββββ
INSIGHT:
Group by FEATURE, not by layer.
Each slice is independent and complete.
When to Use:
β Feature teams
β Rapid iteration
β Varying complexity per feature
β CQRS systems
β Highly shared logic
β Small applications
β Strong layer conventions required
βββββββββββ βββββββββββ βββββββββββ
β Service β β Service β β Service β
β A ββββββΆβ B ββββββΆβ C β
βββββββββββ βββββββββββ βββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββ βββββββββββ βββββββββββ
β DB A β β DB B β β DB C β
βββββββββββ βββββββββββ βββββββββββ
PRINCIPLES:
1. Single responsibility per service
2. Own your data
3. Communicate via well-defined APIs
4. Deploy independently
5. Design for failure
The Microservices Decision:
CONSIDER MICROSERVICES WHEN:
β’ Multiple teams need autonomy
β’ Different scaling requirements per component
β’ Polyglot persistence needed
β’ Independent deployment critical
β’ Organization is distributed
AVOID MICROSERVICES WHEN:
β’ Small team (< 10 developers)
β’ Simple domain
β’ Tight deadlines
β’ Limited DevOps capability
β’ Unknown domain boundaries
ββββββββββββ βββββββββββββββ ββββββββββββ
β Producer ββββββββββΆβ Event Bus ββββββββββΆβ Consumer β
ββββββββββββ β (Kafka/RMQ) β ββββββββββββ
βββββββββββββββ
β
βββββββ΄ββββββ
βΌ βΌ
ββββββββββββ ββββββββββββ
β Consumer β β Consumer β
ββββββββββββ ββββββββββββ
EVENT TYPES:
β’ Domain Events: Something happened (OrderPlaced)
β’ Integration Events: Cross-boundary communication
β’ Commands: Request to do something
β’ Queries: Request for information
βββββββββββββββββββββββββββββββββββββββββββββββ
β Client β
βββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β
βββββββββββ΄ββββββββββ
βΌ βΌ
βββββββββββββββββ βββββββββββββββββ
β COMMANDS β β QUERIES β
β (Write) β β (Read) β
βββββββββ¬ββββββββ βββββββββ¬ββββββββ
β β
βΌ βΌ
βββββββββββββββββ βββββββββββββββββ
β Write Model β β Read Model β
β (Normalized) ββββΆβ (Optimized) β
βββββββββββββββββ βββββββββββββββββ
INSIGHT:
Reads and writes have different needs.
Optimize each independently.
S - Single Responsibility
Each component has one reason to change
O - Open/Closed
Open for extension, closed for modification
L - Liskov Substitution
Components should be replaceable
I - Interface Segregation
Clients shouldn't depend on unused interfaces
D - Dependency Inversion
Depend on abstractions, not concretions
START HERE:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β What is your team size? β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β < 5 developers β Consider Modular Monolith β
β 5-15 developers β Consider Vertical Slices β
β 15+ developers β Consider Microservices β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
THEN ASK:
β‘ How complex is the domain? (Simple β Layered)
β‘ How testable must it be? (High β Hexagonal/Clean)
β‘ How often does it change? (Often β Vertical Slices)
β‘ How independent are components? (Very β Microservices)
β‘ What are the scaling needs? (Variable β CQRS)
For migrating from legacy:
PHASE 1: Create new system alongside old
ββββββββββββ ββββββββββββ
β Legacy β β New β
β System β β System β
ββββββββββββ ββββββββββββ
PHASE 2: Route new features to new system
ββββββββββββ ββββββββββββ
β Legacy ββββββΆβ New β
β (shrink) β β (grows) β
ββββββββββββ ββββββββββββ
PHASE 3: Migrate remaining features
ββββββββββββ
β New β
β System β
ββββββββββββ
PRINCIPLE: Never big-bang rewrite.
Gradually strangle the old with the new.
SYMPTOMS:
β’ No clear structure
β’ Everything depends on everything
β’ Changes have unpredictable effects
β’ Only original authors understand it
CAUSES:
β’ No upfront design
β’ Deadline pressure
β’ Lack of refactoring
β’ Knowledge silos
SOLUTION:
β’ Identify bounded contexts
β’ Extract modules gradually
β’ Establish clear interfaces
β’ Apply Strangler Fig
SYMPTOMS:
β’ Microservices that must deploy together
β’ Shared databases
β’ Synchronous call chains
β’ Coupled release cycles
CAUSES:
β’ Wrong service boundaries
β’ Shared data without events
β’ Missing async patterns
SOLUTION:
β’ Merge tightly coupled services
β’ Introduce event-driven communication
β’ Apply domain-driven design
β‘ Clear separation of concerns
β‘ Dependencies point in one direction
β‘ Domain logic isolated
β‘ External dependencies abstracted
β‘ Components independently testable
β‘ Scaling strategy defined
β‘ Failure modes understood
β‘ Monitoring and observability planned
| Need | Pattern |
|-------------------------|----------------------|
| Simple CRUD | Layered |
| Complex domain | Hexagonal/Clean |
| Feature teams | Vertical Slices |
| Team autonomy | Microservices |
| Read/write separation | CQRS |
| Audit trail | Event Sourcing |
| Distributed txns | Saga |
| Legacy migration | Strangler Fig |
"The best architecture is the one that makes the right thing easy and the wrong thing hard."