SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
  • Client-Server
  • Monolithic Architecture
  • Microservices
  • Serverless
  • Event-Driven Architecture
  • Peer-to-Peer (P2P)
  • Hexagonal Architecture
  • CQRS
  • Event Sourcing
  • Domain-Driven Design (DDD)
  • Layered (N-Tier) Architecture
  • Containers and Docker
  • Kubernetes Basics
  • Clean Architecture and Modular Monolith
  • Multi-Tenancy
  • Cloud Basics: Regions, Zones and Edge
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Architecture/Architectural Patterns

Layered (N-Tier) Architecture

Stack the app in layers (presentation, business, data) where each layer only talks to the one below.

⏱ 12 min readBeginnerMust KnowInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

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.

  • The classic four layers are presentation (UI), business (rules), data access and the database.
  • A request goes down through the layers, and the answer comes back up.
  • It is easy to understand and a good fit for most business apps, and it is the style many frameworks suggest by default.
  • Its weak point: one change often cuts through every layer, and the business rules end up depending on the data layer.

02Concept: What is 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.

The classic four layers
1Presentationpages, forms, controllers, JSON responses
2Business (service)rules, workflows, validation
3Data accessrepositories, queries, mapping
4Databasetables and storage
Calls go down. Results come back up. A layer should not skip a layer or call upwards (strict layering).

Layers and tiers are not the same thing

  • A layer is a way to organise code: folders, packages or modules in one program.
  • A tier is a place where code runs: a separate machine or process. A three-tier web app has a web tier, an app tier and a database tier.
  • You can have many layers in one tier (a single program), or put layers on different tiers. People often say "n-tier" when they mean "layered", so check which one is meant.
Three tiers on three machines
🌐Browser
→↓HTTPS
🖥️Web tierserves pages
→↓
⚙️App tierbusiness logic
→↓
🗄️Data tierdatabase
Each tier can be scaled and secured on its own. The browser is a client and is not counted as a tier here.

Strict or relaxed

  • Strict layering: a layer may only call the one directly below. Safer, but you get pass-through code.
  • Relaxed layering: a layer may call any layer below. Less boilerplate, but more coupling.
🧠Dependencies point downwards
In a classic layered app the business layer depends on the data layer. In hexagonal and clean designs the arrows are flipped, so the business rules depend on nothing.

03Real-Life Example

🍵The Restaurant With Four Stations
A customer tells the waiter what they want (presentation). The waiter hands the order to the chef who knows the recipes and rules, such as "no nuts for this guest" (business). The chef asks the stockroom clerk to fetch ingredients (data access), and the clerk goes into the pantry (database). The customer never walks into the kitchen, and the waiter never opens the pantry. Everyone does one job and only talks to the next person in line.
Restaurant
Layered architecture
The waiter taking the order
Presentation layer
The chef with the recipes and rules
Business layer
The stockroom clerk
Data access layer
The pantry
Database
The order slip passed along
A request moving down the layers
The waiter who just passes the slip on unchanged
A pass-through (sinkhole) layer
Changing the menu: waiter, chef and clerk all learn it
A change that cuts through every layer
One request, down and up
🙋Click
→↓down
🧠Rules
→↓down
🔎Query
→↓down
🗄️Rows
↑result comes back up

04Why Do We Need It?

🧭Easy to understand
A new developer knows where to look: UI code here, rules there, queries there.
🧪Easy to test
You can test the business layer with a fake data layer, without a real database.
🔄Swap a layer
A new look for the UI or a different database product can be done in one layer.
👥Team split
Front-end, back-end and database people can each work on their own layer.
🔒Controlled access
Only the data layer touches the database, which is a good place for security and auditing.
📈Scale by tier
With separate tiers you can add web servers without touching the database.
Give each layer one job and one neighbour to talk to, and the app stays simple to read, test and change in small ways.

05Where Do We Use It?

🏢Business apps
Internal tools, back-office systems and customer portals with forms and reports.
🌐Classic web frameworks
MVC frameworks encourage controllers, services and repositories, which are layers.
🏛️Enterprise Java and .NET
Many long-lived enterprise systems follow a layered structure.
🧱Monoliths
A single deployable app is very often organised in layers inside.
🧩Inside one microservice
A small service may still use controller, service and repository layers.
🎓Teaching and prototypes
A familiar shape that gets a first version working quickly.
📝Note
Layered is a good default for simple to medium apps. When rules become the hard part, many teams move toward hexagonal or clean designs.

06Types / Parts / How It Works

What lives in each layer

LayerJobContainsShould not contain
PresentationTalk to the user or callerControllers, views, request and response shapesBusiness rules, SQL
BusinessDecide what is allowed and what happensServices, validation, workflows, domain rulesHTML, HTTP details, SQL
Data accessRead and write stored dataRepositories, queries, mapping to objectsBusiness decisions
DatabaseKeep the data safe and fast to findTables, indexes, constraintsApplication logic (keep it light)

Layered compared with its neighbours

Which way do the dependencies point?
Layered
🖥️UI
↓
🧠Business
↓
🔎Data access
Hexagonal / Clean
🖥️UI
🔎Database adapter
↑both depend on
🧠Core rules
Arrow = depends on
In a layered design everything leans on the data layer. In hexagonal, the core owns the interfaces, and the outside plugs into it.
StyleMain ideaGood forWatch for
LayeredStack of layers, calls go downSimple and medium apps, small teamsChange cuts through all layers, rules tied to the data layer
Hexagonal / CleanCore in the middle, tools plug inRich business rules, long-lived systemsMore interfaces and set-up
Feature slicesFolders by feature, each with its own layersApps where features change independentlySome repeated code between features
MicroservicesIndependent deployable servicesBig systems with many teamsNetwork calls, operations cost
VisualizerA request through four layers, and a change that spreadsStep through a request and spot the pass-through layer. Then make a change and compare organising code by layer with organising it by feature.
What do you want to see?
Which request?
↓Presentation (UI)Reads the form and checks it is filled in
2Business (service)rules and workflows
3Data accessqueries, repositories
4Databasetables and storage
Step 1 of 6
The user clicks
A customer presses Place order. The browser sends a request to the top layer.

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.

07How to Use It / Steps / Implementation

Building a layered app well

  1. 1
    Name the layers
    Agree on three or four layers and what each may contain. Write it down so the whole team follows it.
  2. 2
    Pick strict or relaxed
    Decide whether a layer may skip the one below. Strict is safer for a growing team.
  3. 3
    Keep rules out of the UI
    Controllers should translate the request and call a service. No pricing or approval logic there.
  4. 4
    Keep SQL out of the business layer
    Hide queries behind a repository interface, so the service asks for what it needs and not how to fetch it.
  5. 5
    Pass simple data across layers
    Use small request and response objects, so a table change does not leak up into the UI.
  6. 6
    Consider cutting by feature
    Inside each feature folder, keep the layers. This keeps related files close together.
  7. 7
    Check the arrows
    Add a build or lint rule that fails when a lower layer imports an upper one.
💡Dependency inversion helps
Let the business layer own an interface such as OrderRepository, and let the data layer implement it. The arrow now points from data to business, which is the first step toward a hexagonal design.
⚠️Do not add layers for show
If a layer only forwards calls, it adds code and no value. A layer should earn its place with real work or a real boundary.

08Key Terms to Know

TermMeaning in easy words
LayerA group of code with one kind of job, such as UI or business rules
TierA place where code runs, such as a web server, an app server or a database server
Separation of concernsKeeping different kinds of work in different places
Presentation layerThe part that talks to users or callers (pages, controllers, API responses)
Business layerThe part that holds the rules and workflows of the application
Data access layerThe part that reads and writes stored data, often through repositories
Strict layeringA layer may only call the layer directly below it
Sinkhole anti-patternRequests that pass through a layer without any work being done there
CouplingHow much one piece of code depends on another
DTOData transfer object: a simple object used to move data between layers
Feature sliceA folder that holds all the layers for a single feature

09Pros, Cons and Trade-offs

✅ Pros
  • Simple and familiar to most developers
  • Clear place for each kind of code
  • Layers can be tested on their own
  • Works with most frameworks out of the box
  • A good fit for small and medium apps
⚠️ Cons
  • One feature change often touches every layer
  • Business rules end up depending on the data layer
  • Pass-through layers add boilerplate
  • Easy to let layers leak into each other over time
  • Hard to scale a single feature on its own

Trade-offs

ChoiceWe gainWe lose
More layersFiner separation of dutiesMore files and pass-through code
Fewer layersLess ceremony, quicker to writeMixing of concerns as the app grows
Strict layeringClear rules, less couplingBoilerplate that only forwards calls
Relaxed layeringShorter code pathsLayers become tangled
Cut code by layerSame kinds of files sit togetherA single feature is spread across many folders
Cut code by featureEverything for a feature is togetherShared code needs care to avoid copies

10Common Problems and Mistakes

Common mistakes

  1. Business logic in the controller. Rules end up copied across several endpoints.
  2. SQL in the business layer. The rules become hard to test and tied to one database.
  3. Pass-through services. A service that only calls the repository and adds nothing.
  4. The anemic model. Objects that are plain data bags, with all the rules in big service classes.
  5. Skipping layers when in a hurry. The UI reads the database directly, and the structure erodes.
  6. Sharing database entities with the UI. A table change then breaks the screens.
  7. Calling the layers "tiers" and expecting scale. Layers in one program do not scale separately.

Things to watch

🕳️Sinkhole requests
If most requests only pass through a layer, rethink that layer.
🧶Tangled arrows
Lower layers importing upper ones is a sign the design is leaking.
🐘Fat service classes
One huge service holding all the rules. Split by business concept.
🔗Database as the model
The whole design shaped by the table layout, not by the business.

11Real-World Examples

🌱Spring-style apps
Controller, service and repository packages are a common layered shape in Java apps.
🔷.NET business apps
Presentation, business and data projects are a long-standing pattern in many enterprise systems.
💎MVC frameworks
Model, view and controller split the work in many web frameworks, and often sit on top of a service layer.
🧰Internal tools
Reports and approval tools are often small layered apps with a relational database.
☁️Cloud web apps
A web tier behind a load balancer, an app tier and a managed database form three tiers.
🧱Modular monoliths
Each module often keeps its own small set of layers inside.
📝Note
These are typical patterns, not claims about any one company. Frameworks differ in naming and in how strict they are.
Mini case: a three-tier web shop
👥Users
→↓
⚖️Load balancer
→↓
🖥️Web 1
🖥️Web 2
→↓
⚙️App tier
→↓
🗄️Database
Web servers are stateless, so you can add as many as you like. The database tier is scaled with care.

12Related Topics to Read Next

Monolithic Architecture→

A single deployable app is very often organised in layers.

Hexagonal Architecture→

Flips the dependency arrows so the rules depend on nothing.

Clean Architecture and Modular Monolith→

Cleaner boundaries and modules that can later become services.

Client-Server→

The simplest tier split: a client in front and a server behind.

Microservices→

Splits the app into many small services, each with its own layers.

Stateless vs Stateful→

Stateless web and app tiers are what let you scale them out.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔What: stack the app as presentation, business, data access and database.
  • ✔Rule: calls go down to the layer below, and results come back up.
  • ✔Layer vs tier: a layer organises code, a tier is a place where code runs.
  • ✔Good for: simple and medium business apps, small teams, quick starts.
  • ✔Weak spots: changes cut through every layer, and rules depend on the data layer.
  • ✔Smells: pass-through layers, SQL in services, rules in controllers.
  • ✔Next step: cut by feature, and invert the dependency with an interface.
  • ✔One line: “A tidy stack of floors: simple to learn, but a change may need every floor.”
← Previous
Domain-Driven Design (DDD)
Next →
Containers and Docker