2 WriteFlow's wake-up call: When good architecture intentions aren't enough
WriteFlow’s story begins with a familiar success that gradually turns into a liability: a layered monolith that was perfectly reasonable at first, but became harder to change as the product grew. New requirements such as audit trails, GraphQL access, and multi-tenancy pushed domain concerns into controllers, services, and persistence code all at once. The result was a tightly coupled system where a single concept, like Article, carried HTTP, database, and business responsibilities together, making tests slow and changes expensive.
The chapter then shows the core idea of hexagonal architecture through the flow of a single article-creation request. A REST adapter translates external input into a command object, an input port exposes the use case, a handler orchestrates the work, and output ports connect to infrastructure through secondary adapters. The essential rule is that dependencies point inward only: the core knows only the interfaces it owns, while transport and infrastructure stay at the edges. This keeps the application testable, replaceable, and easier to migrate incrementally.
Finally, the WriteFlow migration is presented as a practical five-phase path rather than a risky rewrite. The team first extracts real domain logic, then defines ports, wraps old code behind facade adapters, refactors orchestration into pure handlers, and replaces adapters one by one while verifying boundaries. Along the way, the chapter highlights common mistakes such as putting business rules in adapters or designing ports too close to existing implementations. It also emphasizes the stakeholder conversation: the case for change is strongest when framed in terms of resilience, lower risk, and evidence from a small working prototype rather than abstract architectural ideals.
WriteFlow v1: layered architecture showing Article entity carrying @Entity, @JsonProperty, and @Table annotations simultaneously. Arrows show how a single change to Article ripples through all layers.
A single HTTP POST /articles request flowing through WriteFlow's hexagonal architecture: REST adapter → input port → handler → output port → secondary adapter. Arrows show both the flow of control and the direction of dependencies, which always point inward toward the core.
The request flow from figure 2.2, annotated with component type labels: Primary Adapter, Input Port, Handler, Domain Entity, Output Port, Secondary Adapter. Each label links to the corresponding row in table 2.1.
The WriteFlow five-phase migration map: Phase 1 Extract → Phase 2 Define → Phase 3 Wrap → Phase 4 Refactor → Phase 5 Replace. Each phase box contains its primary goal and a chapter reference.
Summary
- WriteFlow's migration from a tightly coupled layered architecture to hexagonal architecture followed five incremental phases—Extract, Define, Wrap, Refactor, Replace—each delivering working software before the next phase began. No big bang rewrite. No production risk.
- The migration decision is as much a stakeholder conversation as a technical one. The strongest argument is a working prototype showing millisecond handler tests and a successful adapter swap, not a diagram. Lead with numbers, not principles.
- Hexagonal architecture produces six standard component types: primary adapters, input ports, handlers, domain entities, output ports, and secondary adapters. Table 2.1 maps each one to its responsibility and to the chapter where it is built in this book.
- For teams coming from layered architecture, the components are familiar (controllers, services, repositories) but the critical difference is ownership. The core defines the port interfaces. Adapters conform to the core's terms. That single reversal, the Dependency Inversion Principle in practice, is what makes the core independently testable and the adapters independently swappable.
- The two most common migration mistakes are business logic leaking into adapters and ports designed around infrastructure methods rather than domain needs.
- ArchUnit is not optional. Without automated boundary enforcement, dependency leaks return within weeks.
Clean Applications with Hexagonal Architecture ebook for free