Layered Architecture

Organize your application into presentation, business logic, and data access layers.

Layered (or n-tier) architecture organizes an application into horizontal layers, each with a specific responsibility. It’s the most common architecture for enterprise applications.

The Layers

┌──────────────────────────┐
│  Presentation Layer       │  HTTP handlers, controllers, views
├──────────────────────────┤
│  Business Logic Layer     │  Domain models, services, rules
├──────────────────────────┤
│  Data Access Layer        │  Repositories, ORM, external APIs
└──────────────────────────┘

Dependency Rule

Dependencies point downward only. The presentation layer depends on business logic; business logic depends on data access. Never the reverse.

When to Use

When to Avoid

Variations

Variation Description
Strict layering Each layer can only call the layer directly below it
Relaxed layering A layer may skip layers (e.g., presentation calls data access directly)
Clean Architecture Dependency inversion — business logic defines interfaces, outer layers implement

Code Structure (Example)

myapp/
├── handlers/       # Presentation (HTTP)
├── services/       # Business Logic
├── repositories/   # Data Access
└── models/         # Domain Objects

Trade-offs

Pro Con
Simple to understand Can become a “big ball of mud”
Easy to test in isolation Temptation to skip layers
Works well with CRUD Not great for complex workflows
Single deployment unit Scaling is all-or-nothing

Next: Microservices