Bookmarks

Layered architecture

An architectural style in which the code of a system is divided into horizontal layers — presentation, logic and data — and each layer depends only on the layers beneath it.

Also: multilayer architecture, n-tier architecture, multi-tier architecture

checked 8 Nov

All terms

The point is separation of responsibilities: each layer is responsible for its own part and calls only the layers beneath it. The presentation layer shows the interface, the logic layer enforces the business rules and the data layer stores and retrieves information. This makes the code easier to change piece by piece, so a change to one layer does not disturb the whole system.

Typical layers and the dependency rule

LayerResponsible forExamples
Presentationthe interface: forms, pages, REST and GraphQL endpointsDjango views, React components
Logicbusiness rules and domain scenariosservices, model managers
Data accesssaving and reading, queries, transactionsORMs, repositories
Databasestoring the data itselfPostgreSQL, MongoDB

The main rule is that dependencies point downwards. The presentation layer calls the logic layer, and the logic layer calls the data layer, but not the other way round: a lower layer knows nothing about the layers above it. This breaks circular dependencies and makes it possible to change or extract the lower layers without touching the upper ones. Layers can be closed — a layer may call only the layer immediately beneath it — or open, where skipping over a layer is allowed. Closed layers keep fewer connections and behave more predictably under change; open layers save extra calls but tie the layers more tightly together.

Layers are a logical division of the code, not a physical one: all three layers can run in a single process on a single server. Deploying layers on separate machines turns them into tiers; hence the names of deployment schemes — two-tier, three-tier, n-tier. How many layers to move into separate tiers is decided by the load and by security requirements.

What it looks like in code

pythonA call passes through the layers from top to bottom and does not skip a layer
# request -> view (presentation) -> service (logic) -> repository (data)def create_order_view(request):    dto = parse_order(request)          # presentation layer    order = order_service.place(dto)    # business logic layer    return render(request, order)       # presentation layer

When it fits and when it gets in the way

  • Suits typical business applications: CRUD, admin panels, online shops, internal services.
  • The structure is familiar to most teams: new developers do not have to learn the approach from scratch.
  • A large body of business logic is the weak point: when it all lives in a single “middle” layer, domain rules dissolve into services without a separate domain model.
  • Every request passes through all the layers, so other styles are sometimes needed for high loads.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • MVCmodel–view–controller

    MVC is a scheme inside the presentation layer: roles within the interface; layers, by contrast, divide the whole system by dependency — data access, logic, interface.

  • hexagonal architecture

    Hexagonal architecture directs dependencies towards the core through ports and adapters rather than downwards through the layers.

  • clean architecture

    In clean architecture the rule is stricter: dependencies point inwards, towards the business rules, not simply downwards.

In depth in the references

where the topic is covered in detail