Q13 · What are the trade-offs between layered, vertical slice, and hexagonal architectures?
Back to 24 Architecture Questions.
The question is not “which is modern”. The axis it turns on: where a change lands, and how much unrelated code it drags with it. All three organize the same code; they differ in what a feature change touches, and what a technology change touches.
What the answer should contain
Layered: changes walk the stack. UI, business, data: one feature touches all three, plus the seams between them. The strength is a simple mental model and obvious reuse; the cost is that every feature is smeared across the codebase, and layers organized by kind (all the controllers here, all the repositories there) tempt the business rules to leak into the wrong layer.
Vertical slice: changes land in one place. A feature is one slice, UI to data. Adding a feature adds a slice; deleting one deletes a slice. The cost is duplication across slices and the constant temptation to grow a “common” module that quietly becomes a new layer everyone depends on. Discipline: a thin shared kernel, deliberately starved.
Hexagonal: the domain never learns the technology. The core holds the business rules and exposes ports; adapters translate HTTP, databases, and message brokers into them. Swapping an adapter touches the core not at all, and the core is testable without infrastructure. The cost is ceremony: for a CRUD app with two rules, ports and adapters buy indirection with nothing to protect.
Pick by change profile, and say which changes you expect. Features churn fastest: slices win. The delivery technology churns (framework migration, DB swap, adding a message bus): hexagonal wins. Small team, simple CRUD: layered is honest and cheap. And they compose: hexagonal inside a vertical slice is a common, sound combo.
The test is the drag metric. For the last ten features: how many files did each touch, and how many of those were about something else? That number, trended, tells you more than any diagram.
Ideas worth mentioning out loud
- “Architecture is a bet on which kind of change will be frequent. Layers bet on technology churn, slices bet on feature churn, hexagonal bets the domain outlives its adapters.”
- Organizing a team by layers guarantees that every feature needs three teams. Conway’s law, at code level.
- The shared “commons” module is where all three architectures go to die.
Follow-ups to expect
- Where does validation live? Where the rule is: input shape at the boundary, business invariants in the core or the slice that owns them.
- How do slices share code? Through a deliberately small kernel; anything either slice can change without the other’s review goes there, and nothing else.
- Is hexagonal worth it without a domain? No. Its whole value is protecting a domain worth protecting.
Red flags in an answer
- Layer counts as the argument (“ours has five layers”).
- A giant common module shared by every slice.
- Hexagonal applied to a CRUD form: ceremony with nothing to isolate.
- No answer to “what kind of change did this architecture bet on?”
Sources
- Hexagonal Architecture, Alistair Cockburn: the pattern from its originator, motivated exactly by the axis here, business logic that survives swaps of delivery technology.
- Vertical Slice Architecture, Jimmy Bogard: the case that a feature should be organized around the change, not the kind of code, including the organizational drag argument.
- PresentationDomainDataLayering, Martin Fowler: the layered side, honest about where the pattern fits and where it smears features.