Skip to content

Layered Architecture

Layered Architecture

Separation of concerns through horizontal layers with well-defined responsibilities

1. Overview: Layered Architecture, which confines change impact within a layer by separating concerns horizontally

    flowchart LR
    A["Business logic, UI, and DB access<br/>mixed in monolithic code —<br/>change impact spreads widely"] --"Separate concerns,<br/>define layer boundaries"--> B["Presentation, Business,<br/>Persistence, Database —<br/>responsibilities split by layer"] --"Isolated change,<br/>independent testing"--> C["Improved maintainability<br/>and reusability"]

    style A fill:#FFEBEE,stroke:#D32F2F,color:#000
    style B fill:#E3F2FD,stroke:#1976D2,color:#000
    style C fill:#E8F5E9,stroke:#388E3C,color:#000
  

Definition: The most common architectural pattern, in which a software system is split horizontally into layers by concern, with each layer depending only on the layer directly below it. This confines the impact of change to within a layer and lets each layer be developed, tested, and replaced independently.

Characteristics: (One-way dependency) Upper layers use lower layers, but lower layers know nothing about upper layers. (Separation of Concerns, SoC) Each layer focuses on a single responsibility, increasing cohesion and reducing coupling. (Foundation of major frameworks) The default structural pattern of major enterprise frameworks such as Java (Spring MVC), .NET, and Django.


2. Core Structure of Layered Architecture

A. Layer Structure and Separation of Responsibilities

    flowchart TD
    L1["Presentation Layer<br/>receives user requests,<br/>returns responses<br/>Controller, View, DTO"]
    L2["Business Logic Layer<br/>domain rules,<br/>business processing<br/>Service, Domain Model"]
    L3["Persistence Layer<br/>data access,<br/>transformation, mapping<br/>Repository, DAO, ORM"]
    L4["Database Layer<br/>data storage and retrieval<br/>RDBMS, NoSQL, Cache"]

    L1 -->|"business call"| L2
    L2 -->|"data request"| L3
    L3 -->|"SQL / API"| L4

    style L1 fill:#E3F2FD,stroke:#1976D2,color:#000
    style L2 fill:#F3E5F5,stroke:#7B1FA2,color:#000
    style L3 fill:#FFF3E0,stroke:#F57C00,color:#000
    style L4 fill:#1E3A5F,stroke:#1E3A5F,color:#fff
  
LayerKey ResponsibilityIncluded ElementsReplacement Example
PresentationHTTP request handling, input validation, response serializationController, REST API, View TemplateWeb → CLI → Mobile swap
Business LogicApplies domain rules, coordinates transactions, executes use casesService, Domain Model, Use CaseOnly this layer changes when business rules change
PersistenceORM mapping, CRUD abstraction, query optimizationRepository, DAO, JPA EntityJPA → MyBatis → JDBC swap
DatabaseData storage, retrieval, indexingMySQL, PostgreSQL, Redis, MongoDBOnly the Persistence layer changes when the DB is swapped

B. Inter-Layer Dependency Rules and Variant Patterns

    flowchart TD
    subgraph R1[" "]
        direction LR
        STR["Strict Layered<br/>only adjacent-layer calls allowed<br/>complete separation of concerns"]
        REL["Relaxed Layered<br/>direct calls to non-adjacent layers allowed<br/>used when performance optimization is needed"]
    end
    subgraph R2[" "]
        direction LR
        HEX["Hexagonal Architecture<br/>ports and adapters pattern<br/>isolates external dependencies"]
        ONI["Onion Architecture<br/>domain placed at the innermost core<br/>dependencies point inward only"]
    end

    style STR fill:#E3F2FD,stroke:#1976D2,color:#000
    style REL fill:#F3E5F5,stroke:#7B1FA2,color:#000
    style HEX fill:#FFF3E0,stroke:#F57C00,color:#000
    style ONI fill:#E8F5E9,stroke:#388E3C,color:#000
    style R1 fill:none,stroke:none
    style R2 fill:none,stroke:none
  

Anti-patterns in Inter-Layer Dependencies

Anti-patternDescriptionResolution
Architecture SinkholeA request passes through a layer without any real processingMinimize the number of layers, remove pass-through delegation
Layer skippingPresentation calls Persistence directlyAdopt relaxed layering or route through an interface
Circular dependencyA lower layer references back to an upper layerCorrect the dependency direction by applying DIP
Bloated serviceAll logic concentrates in the Business LayerDistribute logic into domain models using DDD Aggregates

Pattern Comparison

PatternCharacteristicsSuitable Scale
Traditional layeredSimple, easy to understand, fast to developSmall-to-medium CRUD applications
HexagonalIsolates external technology via ports and adaptersComplex domains where testing and replaceability matter
Clean ArchitectureDependency inversion, domain-firstLong-term maintenance, large enterprise systems
MSASeparated by service unit instead of layersLarge systems needing independent deployment and scaling

3. Expected Benefits and Application of Layered Architecture

CategoryKey Expected BenefitApplication and Practical Use
MaintainabilityChange impact is confined within a layer, reducing modification costOnly the Persistence layer changes on a DB swap; upper layers unaffected
TestabilityEach layer can be unit- and integration-tested independentlyReplace lower layers with mocks to test the Business Layer in isolation
Team division of laborDedicated teams per layer enable parallel developmentSplit roles across Frontend (Presentation), Backend (Business), and DBA (DB)
Technology replacementLayer boundaries allow independent tech-stack swapsOnly the affected layer changes on an ORM swap or UI framework change