Vertical Slice Architecture
Organize code by feature slice (request → handler → data) instead of horizontal technical layers, minimizing cross-feature coupling.
TL;DR
Vertical Slice Architecture organizes code by feature (features/place-order/, features/cancel-order/) rather than horizontal layers (controllers/, services/, repos/). Each slice owns its request, logic, and query end-to-end, so changing one feature doesn't ripple through shared layers.
Intent
Maximize cohesion along the axis of change—when a product feature changes, you open one folder and edit one slice instead of touching five horizontal layers.
Problem
In a traditional Layered Architecture, adding one field to PlaceOrder forces edits across OrderController, OrderService, OrderMapper, OrderRepository, and shared OrderDtos used by 12 other endpoints. Shared "Service" classes grow to thousands of lines, and every feature pays the abstraction tax of the most complex feature.
Solution
Flip the axis from horizontal layers to vertical slices:
- Each use case (
PlaceOrder,GetOrderHistory,CancelOrder) is a self-contained slice with its own input schema, handler, and persistence call. - Simple slices stay simple:
GetOrderHistorycan run a direct SQL query and return a flat DTO—no repository interface or domain entity needed. - Complex slices stay rich:
PlaceOrdercan invoke a tactical DDDOrderaggregate if it has complex pricing/inventory invariants.
Real-world analogy
▶ Flow: watch it work
Vertical Slice: Request → Feature Slice → DB
Press Start or tap any step dot to trace the path.
▸All steps (4)
Code
A self-contained place-order slice in TypeScript:
// src/features/place-order/handler.ts
export interface PlaceOrderInput {
customerId: string;
items: Array<{ sku: string; priceCents: number; qty: number }>;
}
export async function handlePlaceOrder(
input: PlaceOrderInput,
saveOrder: (row: { id: string; totalCents: number }) => Promise<void>,
) {
if (input.items.length === 0) throw new Error('Empty order');
const totalCents = input.items.reduce((s, i) => s + i.priceCents * i.qty, 0);
const id = crypto.randomUUID();
await saveOrder({ id, totalCents });
return { orderId: id, totalCents };
}When to use / when to avoid
- Feature-heavy APIs and web backends where most endpoints are independent use cases.
- Teams tired of 5-file boilerplate for a simple read endpoint.
- Core domain logic shared across 10+ endpoints—extract shared Aggregates/Value Objects rather than copy-pasting rules.
How to implement
Group by feature folder
Create src/features/<use-case>/ containing handler.ts, schema.ts, and handler.test.ts.
Keep slices independent
Never import features/a/handler from features/b/handler; extract shared domain rules to src/domain/ only when truly shared.
Pros / Cons
- Minimal merge conflicts and fast onboarding—each feature fits in one folder.
- Each slice can pick the right level of abstraction (raw SQL vs DDD aggregate).
- Can lead to duplicated business rules if developers copy-paste complex logic instead of extracting domain helpers.
- Requires discipline to keep cross-cutting concerns (auth, logging) in middleware rather than repeated in every slice.
Relations
Check yourself
Check Yourself
1. How does Vertical Slice Architecture decide whether to use raw SQL or a rich Domain Aggregate for an endpoint?
2. What is the main risk to watch for as a Vertical Slice codebase grows?
Sources
Related Notes
Dependency Inversion in Practice
Break compile-time coupling between business logic and infrastructure by having both depend on an interface owned by the domain.
Modular Monolith
Organize a single deployable application into isolated business modules that communicate only through public APIs—with zero shared database tables.