Layered System

Client can’t tell if connected to end server

“Architecture style is defined by a layered system constraint… restricted usage behavior to a single layer.” – Roy Fielding

When to use

For security, load balancing, and legacy encapsulation.

Why it matters

  • Security: You can put a security gateway (WAF) in front of the API.
  • Scalability: You can put a Load Balancer in front of the API.
  • Simplicity: The client just talks to “The API”. It doesn’t know there are 5 servers and a Redis cache behind it.

Signs of Violation

  • Client needs to manually connect to different servers for different tasks (e.g., “Connect to Auth Server for token, then Connect to Data Server”). Ideally, the Gateway handles this routing.

Explanation

Problem

If the client talks directly to the database server or internal service, you can’t move things around.

Solution

Intermediaries. Proxy servers. Gateways. The client talks to A. A talks to B. B talks to C. Client thinks it is just talking to A.

Real world analogy

sending a package. You give it to the local post office. They give it to a truck driver. They give it to a plane. You don’t know (or care) about the layers. You just know “Post Office accepted it”.

Pros and Cons

Pros Cons
  • Encapsulation
  • Shared Caching
  • Latency (more hops)
  • Comparison

    • 12-Factor: Backing services are often hidden behind layers.

    Code example

    Typescript

    Bad (Violation)

    Good (Adherence)

    PHP

    Good (Adherence)