What it is
Vertical slice architecture organises an application by feature instead of by technical layer. Each slice holds everything one request needs, from the endpoint to the database: the request and response types, validation, the handler with its logic and its data access. The term was popularised by Jimmy Bogard around 2018.
Features/
PlaceOrder/ PlaceOrderEndpoint.cs PlaceOrderRequest.cs PlaceOrderValidator.cs PlaceOrderHandler.cs
CancelOrder/ ...
OrderHistory/ OrderHistoryEndpoint.cs OrderHistoryQuery.cs OrderRow.cs
Domain/ Order.cs
Infrastructure/ AppDbContext.cs
The guiding principle: minimise coupling between slices and accept, even embrace, coupling within a slice.
Why it exists
Requirements arrive as features ("show the item count in the order history"), but layered code is cut horizontally. Every feature change touches the controller, the service and the repository, classes shared by every other feature, which grow without limit and become merge-conflict hot spots. Layers also impose the same abstractions on every request, whether it is a complex command or a one-line read. Slicing by feature aligns the code with the way it changes.
How it works
One request, one slice. A minimal-API endpoint maps the HTTP request to a request object and calls its handler. The handler validates, does the work and returns a response. In .NET this is often wired with a mediator library or with plain classes registered in DI; neither is required.
public static class OrderHistory
{
public sealed record OrderRow(Guid Id, DateTime PlacedAt, decimal Total, int ItemCount);
public static async Task<IReadOnlyList<OrderRow>> Handle(Guid customerId, IDbConnection db) =>
(await db.QueryAsync<OrderRow>(
"""
SELECT o.Id, o.PlacedAt, o.Total, COUNT(l.Id) AS ItemCount
FROM Orders o JOIN OrderLines l ON l.OrderId = o.Id
WHERE o.CustomerId = @customerId
GROUP BY o.Id, o.PlacedAt, o.Total
""", new { customerId })).AsList();
}
Each slice chooses its tools. A command that enforces business rules loads the Order aggregate with EF Core and saves it in a transaction. A read projects straight from SQL into its response type. This is a natural fit for CQRS-style separation.
Shared code is deliberate. Slices share the domain model where real business rules live, infrastructure such as the DbContext, and cross-cutting behaviour (logging, validation, authorisation) through pipeline behaviours or middleware. They do not call each other's handlers.
When to use it
- Applications whose changes are mostly feature additions and modifications: typical web APIs and line-of-business systems.
- Teams where several people work on different features at once.
- Codebases where reads and writes need very different data access.
- Inside the modules of a modular monolith, as the structure of each module.
When not to
- Rich domains with complex invariants shared by many features, unless combined with a solid domain model that slices use.
- Teams without agreed conventions: each slice may grow its own style.
- Very small services where a single file per endpoint is already the whole application.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Group by feature | Changes stay local; easy to find and delete a feature | Technical concerns spread across folders |
| Slice-specific data access | Simplest fit per request | Several data access styles to know |
| Tolerating duplication | Independent slices | Some repeated mapping and validation |
| Small shared kernel | Rules exist once | A shared part that must be guarded from growth |
Alternatives
- Layered architecture: uniform and familiar, but feature changes cut across shared layers.
- Clean / hexagonal architecture: protects a rich domain behind ports; often combined with slices for the use cases ("slices on top of a clean core").
- Modular monolith: slices organise code inside a module; modules organise slices by business capability.
Failure modes
- Slices calling slices: one handler invoking another, often through the mediator, recreates hidden coupling.
- Shared kernel creep: everything "might be reused", so the shared folder becomes the old service layer under a new name.
- Logic scattered in handlers: business rules duplicated across slices instead of living once in the domain.
- Inconsistent slices: without conventions, each slice handles errors, validation and logging differently.
In production
Agree on a slice template (endpoint, request, validator, handler, response) and on what may be shared. Put cross-cutting concerns in middleware or pipeline behaviours so slices stay small. Test slices through their endpoint or handler with a real database where data access is slice-specific. Architecture tests can forbid references between Features.* namespaces.
Common misconceptions
- "Vertical slices mean no layers at all." Each slice still separates HTTP, logic and data concerns inside itself; it just does not share layer classes with other features.
- "You need a mediator library." Slices are about code organisation. Plain handler classes work.
- "Duplication is always a smell." Between slices, a little duplication is often cheaper than coupling.
- "Slices are microservices." They live in one application and one deployment.
Interview angle
Explain the axis change: layers group by technical role, slices by feature, so the most common change touches one place. Mention per-slice data access and the link to CQRS, then show maturity about the downsides: how you keep shared code small, avoid slices calling slices, and keep conventions consistent. Combining slices with a domain model for rich rules is a strong closing point.
Model assumptions
This lesson is a scripted illustration, not a simulation: the engine models running components, not how code is organised in folders. Every packet is placed by hand. Request packets show which classes a request passes through; change pulses show which files a change edits; the error packet marks the coupling created by a cross-slice call. Packets are slowed to 30–45 ms per hop so they can be followed; in-process calls take microseconds. Folder and class names are examples.