Layered architecture

On this page 7

Layered architecture is a software architecture style that divides an application into horizontal layers with distinct responsibilities, where each layer offers services to the layer above it and depends only on the layers below. In software architecture it is one of the oldest and most widely taught styles, often called the n-tier or layered pattern, and it remains the default structure of many enterprise and desktop applications.

The style trades some performance and flexibility for separation of concerns: user interface code, business rules and persistence logic live in separate places, so each can be understood, tested and changed with limited impact on the others.

How a layered architecture organizes code

A layered architecture works by assigning every module to exactly one layer and enforcing a one-directional dependency rule: code in a higher layer may call code in a lower layer, but never the reverse. A request from a user typically travels down through the stack, for example from a web controller to a service, then to a repository and finally to the database, and the result travels back up.

Each layer exposes an interface or contract that hides its implementation. The presentation layer does not know whether data comes from a relational database or a remote service; it only knows the operations the business layer offers. This information hiding is what allows a team to replace one layer, such as swapping an object-relational mapper, without rewriting the others.

The classic four-layer arrangement below appears in most textbook descriptions and in many framework templates.

LayerMain responsibilityTypical contents
Presentation layerAccept input and render outputViews, controllers, API endpoints
Application or service layerCoordinate use cases and transactionsService classes, workflow logic
Domain or business layerHold business rules and invariantsEntities, validation, calculations
Data access or persistence layerStore and retrieve dataRepositories, queries, ORM mappings
Typical layers and their responsibilities

Strict and relaxed layering

Layered systems differ mainly in how strictly they apply the dependency rule. In strict (closed) layering, a layer may call only the layer directly beneath it, so every request passes through every layer. In relaxed (open) layering, a layer may skip intermediate layers and call any lower layer, which reduces boilerplate but weakens isolation.

Architects often mix both forms: most layers stay closed, while a shared utility or infrastructure layer is marked open so that logging, configuration and similar cross-cutting services are reachable from anywhere. The choice should be recorded explicitly, for instance in an architecture decision record, because unrecorded exceptions tend to multiply.

VariantCalling ruleMain benefitMain cost
Strict layeringOnly the layer directly belowStrong isolationPass-through code
Relaxed layeringAny lower layerLess boilerplateWider coupling
Mixed layeringClosed layers plus open shared layersBalanced controlRules need documenting
Dependency-inverted layeringInner layers define interfacesDomain independent of storageMore abstractions
Layering variants compared

Layered architecture is a logical code organization, which distinguishes it from styles that describe deployment or distribution. A three-tier architecture splits a system into presentation, application and data tiers that usually run on separate machines or processes; a layered application may run entirely inside one process. The terms are often used interchangeably, but layers are logical and tiers are physical.

Variants such as hexagonal, onion and clean architecture keep the idea of concentric responsibilities but invert the dependency between business logic and infrastructure, so the domain sits at the center and depends on nothing external. Other neighbors, such as microservices or event-driven architecture, partition the system vertically by business capability or by event flow rather than horizontally by technical concern.

StylePrimary partitioningRelation to layering
Three-tier architecturePhysical tiers on separate hostsPhysical counterpart of logical layers
Monolithic architectureSingle deployable unitOften layered internally
Modular monolithBusiness modules in one deployableEach module may contain its own layers
MicroservicesIndependently deployed servicesLayers appear inside each service
Clean or onion architectureConcentric rings around the domainLayering with inverted dependencies
How layered architecture relates to neighboring styles

When to choose a layered architecture

A layered architecture fits best when an application is predominantly request-response and data-centric, the team is small to medium in size, and the business rules are moderate in complexity. Because the structure is familiar, it lowers onboarding cost and gives new developers a clear answer to the question of where a piece of code belongs.

It fits less well when different parts of the system must scale or deploy independently, when a feature change routinely touches every layer, or when the domain is so complex that a purely technical partitioning hides the business structure. In those cases, teams often move toward a modular monolith or vertical slices.

SituationRecommendationReason
Line-of-business CRUD applicationUse classic layeringSimple, well understood, quick to build
Complex domain with many rulesLayering with dependency inversionProtects the domain model from infrastructure
Many teams on one codebaseModules first, layers insideReduces cross-team conflicts
Uneven scaling needs per featureConsider separate servicesLayers cannot scale independently in one process
Decision guide for adopting layering

Limitations and common mistakes

The most frequent failure of layered systems is the architecture sinkhole anti-pattern, in which most requests pass through several layers without any processing, adding code and latency but no value. A small amount of pass-through is normal; when it dominates, relaxed layering or a different style is usually justified.

  • Leaking abstractions: database entities or SQL details appear in views, which couples the top layer to storage.
  • Anemic domain layer: business rules drift into controllers or stored procedures, leaving the domain layer as plain data holders.
  • Upward dependencies: a lower layer imports a class from a higher layer, often through a shared model, which creates cycles.
  • Layer-wide changes: adding one field requires edits in every layer, a sign that the partitioning ignores business boundaries.

These problems are best prevented with automated dependency checks in the build, such as architecture rules in unit tests or module visibility settings, because code review alone rarely catches every violation of the layer boundary.

Explained without jargon

Think of a restaurant. Guests talk only to the waiter, the waiter passes orders to the kitchen, and the kitchen takes ingredients from the storeroom. The guest never walks into the storeroom, and the storeroom never takes orders directly from guests.

Layered architecture builds software the same way. The screen talks to the rules, and the rules talk to the stored data. Each part has one job and talks only to its neighbor below, so a change in the storeroom does not force the waiter to learn a new routine.

A typical use case

A regional insurance company builds an internal claims application with a team of eight developers. They structure it in four layers: web controllers for the claims screens, an application layer that coordinates submitting and approving a claim, a domain layer that checks coverage limits and deductibles, and a repository layer that talks to a relational database.

Two years later, the company moves its database to a new managed service with a different query dialect. Because all queries live in the repository layer, the team rewrites about 40 repository classes while the controllers and coverage rules remain untouched. Automated tests on the domain layer pass without modification, and the migration ships in six weeks instead of the estimated four months that a full rewrite would have required.