Decomposing Microservices into Layers

Tyler Kron
2 min readJan 10, 2021

A microservice can be decomposed into three distinct layers: endpoint, service, and persistence layer. These layers serve different but important functions.

Endpoint Layer

The primary function of the endpoint layer is to validate requests.

  • Does the request have the required information?
  • Does the information make sense? Are they within expected ranges?
  • Is the requester authorized to make this request?

If validation passes, the endpoint layer calls the service layer.

Note: You might hear this layer called the controller layer. I prefer endpoint but to each their own.

Service Layer

The service layer is where the actual business logic lives. The service layer calls into the persistence layer.

Persistence Layer

The persistence layer communicates to the persistent backing service (Postgres, MongoDB, Elasticsearch, etc…). This provides encapsulation from the actual backing technology.

Note: You might also hear this layer called the DAO layer or the repository layer.

Why do we need encapsulation?

Imagine if the service layer communicated with the backing service directly and we wanted to change the from Postgres to MongoDB. The changes would likely be widespread and complicated. Instead, we make a contract between the service layer and the persistence layer. Any changes would be localized to a single place.

The service layer shouldn’t care or even know what persistence technology you choose to use.

--

--

Tyler Kron

Husband, Software Engineer, Entrepreneur, Broncos and Nuggets fan