Skip to Content
Mengty Learns
Architecture

Vertical Slice Architecture

Organize code by feature slice (request → handler → data) instead of horizontal technical layers, minimizing cross-feature coupling.

explanationintermediateTested with typescript 7.0.2Updated Nov 27, 2026

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

TL;DR

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.

Each vertical slice owns one use case end-to-end; shared code is extracted only when genuinely needed.

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: GetOrderHistory can run a direct SQL query and return a flat DTO—no repository interface or domain entity needed.
  • Complex slices stay rich: PlaceOrder can invoke a tactical DDD Order aggregate if it has complex pricing/inventory invariants.

Real-world analogy

▶ Flow: watch it work

Vertical Slice: Request → Feature Slice → DB

Overview of Vertical Slice: Request → Feature Slice → DB. 4 steps.
Overview4 steps in this flow

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

When to use
  • Feature-heavy APIs and web backends where most endpoints are independent use cases.
  • Teams tired of 5-file boilerplate for a simple read endpoint.
When to avoid
  • 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

Pros
  • 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).
Cons
  • 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