Stack the app in layers (presentation, business, data) where each layer only talks to the one below.
Layered architecture (also called n-tier) stacks an application in layers. Each layer has one job and normally talks only to the layer directly below it.
Think of a building with floors. People on the top floor do not use the basement stairs directly. They call down to the floor below, and that floor calls the one below it. Separation of concerns is the idea: each layer has one kind of work and hides it from the layers above.
| Layer | Job | Contains | Should not contain |
|---|---|---|---|
| Presentation | Talk to the user or caller | Controllers, views, request and response shapes | Business rules, SQL |
| Business | Decide what is allowed and what happens | Services, validation, workflows, domain rules | HTML, HTTP details, SQL |
| Data access | Read and write stored data | Repositories, queries, mapping to objects | Business decisions |
| Database | Keep the data safe and fast to find | Tables, indexes, constraints | Application logic (keep it light) |
| Style | Main idea | Good for | Watch for |
|---|---|---|---|
| Layered | Stack of layers, calls go down | Simple and medium apps, small teams | Change cuts through all layers, rules tied to the data layer |
| Hexagonal / Clean | Core in the middle, tools plug in | Rich business rules, long-lived systems | More interfaces and set-up |
| Feature slices | Folders by feature, each with its own layers | Apps where features change independently | Some repeated code between features |
| Microservices | Independent deployable services | Big systems with many teams | Network calls, operations cost |
A teaching example with made-up files. The number of files that change is the same in both layouts. What differs is how many folders you have to visit. Neither layout wins every time.
| Term | Meaning in easy words |
|---|---|
| Layer | A group of code with one kind of job, such as UI or business rules |
| Tier | A place where code runs, such as a web server, an app server or a database server |
| Separation of concerns | Keeping different kinds of work in different places |
| Presentation layer | The part that talks to users or callers (pages, controllers, API responses) |
| Business layer | The part that holds the rules and workflows of the application |
| Data access layer | The part that reads and writes stored data, often through repositories |
| Strict layering | A layer may only call the layer directly below it |
| Sinkhole anti-pattern | Requests that pass through a layer without any work being done there |
| Coupling | How much one piece of code depends on another |
| DTO | Data transfer object: a simple object used to move data between layers |
| Feature slice | A folder that holds all the layers for a single feature |
| Choice | We gain | We lose |
|---|---|---|
| More layers | Finer separation of duties | More files and pass-through code |
| Fewer layers | Less ceremony, quicker to write | Mixing of concerns as the app grows |
| Strict layering | Clear rules, less coupling | Boilerplate that only forwards calls |
| Relaxed layering | Shorter code paths | Layers become tangled |
| Cut code by layer | Same kinds of files sit together | A single feature is spread across many folders |
| Cut code by feature | Everything for a feature is together | Shared code needs care to avoid copies |