N-Tier

3 niveles · Multinivel

Escrito por Darjus Kazakevic · revisado el

The same layers, but each one runs as its own process.

N-Tier takes a layered app and puts a deployment boundary between layers. The classic three-tier shape is a browser, an application server with the business logic, and a database server. Each tier runs in its own process, usually on its own machine, and talks to its neighbour over the network.

The key idea is layer versus tier. A layer is a group of code; a tier is a place where code runs. One process can have five layers and one tier. N-Tier does not change which way dependencies point. It changes where code runs, and every new tier adds latency and new ways to fail.

En esencia — Deploy the layers as separate processes so they can scale and be secured on their own, and accept a network call at every boundary you add.

Cuándo recurrir a él

Recurre a él cuando

  • The client runs on hardware you do not control, like a browser or phone
  • Tiers must scale apart: many app servers in front of one database
  • You need a network security boundary between public code and data

Reconsidéralo cuando

  • One team, one deployable, no scaling or security reason: layers are enough
  • The boundary would be chatty, turning one action into many round trips
  • You want to deploy each business capability on its own: see microservices

The tiers

Layer vs. tier (the distinction that matters)
A layer is a code boundary the compiler sees. A tier is a process boundary the network sees. A function call takes nanoseconds; a call to another machine in the same data centre takes about 0.5 ms.
Presentation tier (the client)
What runs on the user's device: a browser, a mobile app, a desktop client. It renders and collects input. You do not control it once it ships, so it cannot be trusted with rules or secrets.
Application tier (middle tier)
The servers that hold the business logic. Adding this tier is what turned two-tier client–server into N-Tier: rules live in one place you control, and clients stop connecting to the database.
Data tier (the database server)
Storage on its own machines, usually on a private network. Only the application tier can reach it, which makes it a security boundary as well as an architectural one.

Deciding where to put a boundary

Add a tier only for a reason you can name
Good reasons: independent scaling, a security boundary, or hardware you do not control. Splitting only because the layers look splittable buys latency and partial failure for nothing.
Never trust a tier you do not own
Validation in the browser helps honest users; it is not a control. Check every rule that matters again on the application tier, because anything on the client can be changed.
Make the tier boundary coarse
A call across tiers is remote: it can be slow, fail halfway or be retried. Give it an API that does a whole unit of work, not a chatty one that copies in-process method calls.
Keep the layers, even inside one tier
Tiers are a deployment choice you can change later. Layers are the seams that make that change possible. Merge the layers and the split is gone for good.

Errores habituales

  • A chatty boundary that copies in-process calls across the wire. One page load then makes 40 requests.
  • Validation only in the browser, trusting a client you do not own.
  • Splitting into tiers for tidiness, paying the network cost with no scaling or security gain.
  • Mixing up tier count and layer count: three tiers does not mean three layers.