Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Architecture Patterns Tutorial

Part of the unified study index: Home · canonical decision guidance in selection guide.

Two tracks: how code is structured inside one project vs how services and infrastructure communicate at scale. All pages are original prose with text-native Mermaid diagrams and illustrative pseudocode.

Application Architecture (Code Structure)

  • Layered — horizontal layers, each talks only to the layer below.
  • Hexagonal — core logic isolated behind ports, adapters face the outside.
  • Onion — domain model at the center, inward-pointing dependencies.
  • Clean — concentric rings around Entities and Use Cases, strict dependency rule.
  • Vertical Slice — organize by feature slice, not technical layers.
  • MVC / MVVM / MVP — UI separations compared in one table.
  • Each application page below now includes its complex scenario, edge cases, and expanded failure-mode guidance.

System & Distributed Architecture (Infrastructure & Scale)

  • Monolithic — one codebase, one deployable unit.
  • Modular Monolith — one deployable, strict module boundaries inside.
  • Microservices — small independently deployable services, each owning its data.
  • Event-Driven — async events via a broker decouple producers from consumers.
  • Serverless — ephemeral functions and managed backends, no servers to run.
  • SOA — larger reusable enterprise services, often coordinated via an ESB.
  • CQRS & Event Sourcing — separate read/write models, every change stored as an event.
  • Each system page below now includes its complex scenario, edge cases, and expanded failure-mode guidance.

Suggested Study Order

  1. Layered, Monolithic — the baseline everything else reacts to.
  2. Vertical Slice, Modular Monolith — first escapes from pure layering.
  3. Hexagonal, Onion, Clean — ports, rings, and dependency rules as a group.
  4. MVC / MVVM / MVP — UI separations after core structure clicks.
  5. Microservices, Event-Driven — distribution and async as a group.
  6. SOA, Serverless, CQRS & Event Sourcing — enterprise, functions, and data separation last.

Comparisons

Hexagonal vs Onion vs Clean

QuestionHexagonalOnionClean
CenterApplication coreDomain modelEntities + Use Cases
MechanismPorts (interfaces) + adaptersConcentric rings, inward depsRings + strict dependency rule
Choose whenSwapping UI/DB/API matters mostDomain purity with infra outsideEnforcing layered rules at scale

Layered vs Vertical Slice

QuestionLayeredVertical Slice
Organized byTechnical layerFeature
Change shapeCross-layer for one featureOne folder per feature
Choose whenSmall team, uniform stackFeatures evolve independently

Modular Monolith vs Microservices

QuestionModular MonolithMicroservices
DeployablesOneMany
BoundariesIn-code modules, enforcedService + database per capability
Choose whenOne team, need boundaries firstIndependent scaling and deploys outweigh ops cost

Event-Driven vs CQRS & Event Sourcing

QuestionEvent-DrivenCQRS & Event Sourcing
CenterEvents triggering decoupled servicesRead/write separation + immutable event log
TransportBroker deliveryEvent log as source of truth
Choose whenReacting across servicesReads and writes scale or evolve differently

Decision Guide

  • Solo project, simple CRUD → Layered + Monolithic.
  • Growing team, one deployable → Vertical Slice + Modular Monolith.
  • Swappable UI/DB/providers → Hexagonal (or Onion/Clean if domain-centric).
  • UI-heavy client → MVC / MVVM / MVP per platform norms.
  • Independent scaling per capability → Microservices; add Event-Driven for async flows.
  • Enterprise reuse across business units → SOA with ESB governance.
  • Spiky workloads, minimal ops → Serverless functions + managed backends.
  • Read-heavy with complex writes → CQRS; add Event Sourcing when audit/replay matters.

Combinations are normal: e.g., Clean inside a Modular Monolith module, or Vertical Slices each exposing ports, or Microservices communicating via events with CQRS on the hot path.