Layered Architecture

Organize code into layers, like presentation, business logic, and data access, where each layer only talks to the one next to it.

What is it?

A growing app does three broad kinds of work:

  • Showing things to the user (screens, API responses).
  • Applying business rules (is this order allowed? what's the discount?).
  • Reading and writing data (a database, a file, another service).

If these can call each other in any direction, like a screen running SQL or a database function holding pricing rules, the codebase becomes a web. Nobody can change one thing without tracing the whole system.

Layered architecture stacks the code into layers, commonly:

- A presentation layer (UI or API endpoints). - A business logic layer (the app's rules and decisions, also called the domain or service layer). - A data access layer (reading and writing storage).

Each layer only talks to the layer directly next to it. The presentation layer never touches the database. It goes through business logic, which goes through data access.

This is more than a filing convention. To change how data is stored (switch databases, add caching), you only touch the data access layer, since nothing above depends on storage details. To change the display, you can't break a business rule, because the presentation layer holds none.

Explain like I'm 10

A company org chart: front-desk staff (presentation) don't set policy or handle the filing cabinets. They escalate to managers (business logic), who decide what should happen and ask records staff (data access) to look something up or file it.

Examples

Before: layers bypassed

// api/getUserProfile.js  (presentation layer)
app.get("/profile/:id", async (req, res) => {
  const row = await db.query("SELECT * FROM users WHERE id = ?", [req.params.id]);
  const discount = row.isVip ? 0.2 : 0; // business rule, sitting in the API handler
  res.json({ name: row.name, discount });
});

The presentation layer queries the database directly and holds a business rule (the VIP discount). A schema change and a discount change both mean editing this same file.

After: layers respected

// dataAccess/userRepository.js
export async function getUserById(id) {
  return db.query("SELECT * FROM users WHERE id = ?", [id]);
}

// business/userService.js
export async function getUserProfile(id) {
  const user = await getUserById(id);
  const discount = user.isVip ? 0.2 : 0;
  return { name: user.name, discount };
}

// api/getUserProfile.js  (presentation layer)
app.get("/profile/:id", async (req, res) => {
  const profile = await getUserProfile(req.params.id);
  res.json(profile);
});

The API handler no longer knows SQL exists, and the repository doesn't know what a 'discount' is. You can swap the database or change the discount rule without touching the other layers.

How it works

Dependencies flow one way: presentation depends on business logic, and business logic depends on data access. Never the reverse, and never skipping a layer.

A quick check: if data access code would change because the UI changed, or presentation code contains a business rule, a layer has been skipped or a responsibility has leaked into the wrong place.

Presentation Layer   (API routes / UI)
          |
          v
  Business Logic Layer (rules, decisions)
          |
          v
  Data Access Layer    (database, files, external APIs)

Why does it exist?

Layering keeps each kind of change (a UI redesign, a new business rule, a database migration) inside one part of the codebase. Without layers, these changes smear across the same files, and every change risks touching unrelated logic.

It also makes the codebase predictable: any engineer can guess where logic lives just from what kind of concern it is.

When to use it

Layering pays off once an app has real business logic worth protecting, anything beyond simple CRUD passthroughs. It's one of the first structural decisions to make on purpose in a growing codebase. It scales down (three folders and a convention) and up (formal boundaries enforced by tooling).

When not to use it

In a trivial CRUD script where the 'business logic' is just "call the database and return the result", a full business logic layer is ceremony with no payoff.

You can also over-layer: forcing a rigid three-layer split on a tiny module adds indirection without real protection against change.

Common mistakes

  • Letting the presentation layer skip business logic and call data access directly 'just this once'. These exceptions pile up until the layering means nothing.

  • Putting business rules in the data access layer (e.g., a repository function that applies a discount) because it was convenient.

  • Creating layers in name only: three folders that still import each other in every direction, with the look of structure but none of the benefit.

Practice exercises

  1. Easy:

    A function fetches a product from a database and calculates its sale price in the same block. Split it into a data access function and a business logic function.

  2. Medium:

    Design the three layers (presentation, business logic, data access) for a 'submit a support ticket' feature. List what each layer does and what it must not do.

  3. Hard:

    Review a real API endpoint, yours or from an open-source project. Find any place a layer is skipped or a responsibility has leaked into the wrong layer, and propose a fix.

Interview questions

What is layered architecture?

Organizing a codebase into layers, commonly presentation, business logic, and data access. Each layer has a distinct job and only talks to the adjacent layer, so dependencies flow one way.

Why shouldn't the presentation layer talk directly to the database?

It couples the UI or API to storage details and skips the layer where business rules belong. Business logic then leaks into presentation code, and database changes ripple into unrelated places.

What's a sign that a layered architecture isn't actually being respected?

Business rules inside the data access layer, or the presentation layer querying storage directly. Either way, a layer boundary is being bypassed even though the folders exist.