ArchitectureSystem Design

Layered Architecture: The Classic Pattern

TT
TopicTrick Team
Layered Architecture: The Classic Pattern

Layered Architecture: The Classic Pattern


1. The Four Layers of Truth

  1. Presentation Layer: The UI (React, HTML, or CLI). Its only job is to show data and get user input.
  2. Business Layer (Service): The "Brain." This is where you calculate taxes, check passwords, and enforce business rules.
  3. Persistence Layer (Data): The code that talks to the database. (SQL Queries live here).
  4. Database Layer: The actual storage (PostgreSQL).

2. The Strict Layering Rule

In a professional system, a layer can only talk to the layer directly below it.

  • The Sin: The UI calling SQL_EXECUTE directly.
  • Why? If you change your database from SQL to NoSQL, you should only have to change the Persistence Layer. If the UI calls SQL, you have to rewrite the WHOLE app. Layering creates "Isolation" that saves you thousands of hours of refactoring.

3. Benefits: Easy Testing

If the Business Layer is separate from the Database, you can "Mock" (Fake) the database during your tests.

  • You can write a test: "If the user is from France, the tax is 20%."
  • You don't need a real database running to verify this math. This makes your build cycle $10x$ faster and more reliable.

4. The Downsides: "Sinkhole" Layers

Sometimes, your Service Layer does nothing. "The UI asks for a User -> The Service asks for a User -> The Data layer asks for a User."

  • If 80% of your layers are just "Passing data through," your architecture is too heavy.
  • The Fix: In 2026, we allow for "Layer Bypassing" in simple cases, provided it's documented and doesn't break the modularity of the system.

Frequently Asked Questions

Is this the same as MVC? Model-View-Controller (MVC) is a "Subset" of layered architecture. MVC is specifically for the Presentation part of your app. A professional system uses MVC for the UI, but still has a separate Business and Data layer behind it.

Is it too old for 2026? No. While "Clean Architecture" (Module 173) is more modern, it is much more complex. For 80% of small-to-medium business apps, a 4-Layered architecture is the "Sweet Spot" of simplicity and organization.


Key Takeaway

Layering is the "Foundation of Order." By mastering the separation of UI, Logic, and Data, you build systems that are easy to understand, easy to test, and ready for future changes. You graduate from "Writing spaghetti code" to "Architecting Layers of Responsibility."

Read next: Hexagonal Architecture: Ports and Adapters →


Part of the Software Architecture Hub — engineering the hierarchy.