Overview

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.
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.
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 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.
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.

FAQ

What problem did WriteFlow’s layered architecture eventually run into?Its original layered monolith worked well at first, but as the system grew, business pressure caused concerns to accumulate in the same classes. The Article concept ended up carrying HTTP, persistence, and validation responsibilities together, making the code harder to test, change, and extend.
Why was the original architecture considered “good” at the beginning?It matched the team’s early needs: a small team, a narrow feature set, and fast delivery. Spring MVC controllers, service classes, and JPA repositories were a sensible choice for that context, so the initial design was not a mistake.
What changed that made the old architecture a liability?New requirements such as audit trails, GraphQL integration, and multi-tenancy were added quickly under pressure. Instead of redesigning, the team kept extending the same structure, which thickened service classes, increased annotations on entities, and slowed the test suite.
How does a request flow through WriteFlow’s hexagonal architecture?A REST adapter receives the request, translates it into a command, and calls an input port. A handler implements that input port, orchestrates domain logic, and calls output ports when it needs outside services. Secondary adapters implement those output ports, and the response is mapped back at the boundary.
What is the main rule that makes hexagonal architecture work?Dependencies always point inward. The core depends only on ports and domain abstractions, while adapters depend on the core’s interfaces. This keeps the core testable, adapters replaceable, and the system easier to migrate incrementally.
What are the six standard components of the hexagonal architecture described in the chapter?The chapter identifies: primary adapter, input port, handler, domain entity, output port, and secondary adapter. Together they separate translation, orchestration, business rules, and infrastructure concerns into clear roles.
How are familiar layered-architecture concepts mapped to hexagonal ones?A controller becomes a primary adapter, a service class becomes a handler, an entity becomes a domain entity, a repository interface becomes an output port, and a repository implementation becomes a secondary adapter. The big difference is that the core owns the abstractions in hexagonal architecture.
What is the safest way to migrate a layered monolith to hexagonal architecture?Do it in phases, not with a big-bang rewrite. The WriteFlow team used a five-phase path: extract the domain, define ports, wrap existing code with facade adapters, refactor into handlers, then replace and verify each adapter one at a time.
What mistake did WriteFlow make by putting business logic in adapters?They initially placed validation in the REST adapter, which caused different adapters to drift into different rule sets. The chapter treats this as a trap: business rules belong in the domain model or handler, so every adapter sees the same behavior consistently.
How should the migration be explained to stakeholders and management?Frame it in terms they care about: resilience and future adaptability for technical leadership, reduced risk and no big-bang disruption for managers, and proof through working tests and adapter swaps for skeptical developers. A small prototype with fast tests is stronger than a diagram when making the case.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Clean Applications with Hexagonal Architecture Clean Applications with Hexagonal Architecture ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Clean Applications with Hexagonal Architecture Clean Applications with Hexagonal Architecture ebook for free