Service Layer Pattern

The layer of operations that sits at the edge of an application: one method per use case that checks access, loads domain objects, runs one transaction and fires side effects. Know what belongs in it and what does not.

Overview

A service layer is the set of operations an application offers to the outside world, written as one method per use case: place_order, cancel_subscription, transfer_funds. Every caller (a web controller, a CLI command, a message consumer, a scheduled job) goes through the same method, so the steps of a use case live in one place instead of being copied into each entry point.

The pattern is in Martin Fowler's *Patterns of Enterprise Application Architecture* (2003), where the Service Layer entry was written by Randy Stafford. The catalog page describes it as a layer of services that defines the application's boundary: it sets out the operations clients may call and coordinates the application's response in each one, including control of transactions. The problem it solves is duplication. When a web page, a data loader and an integration gateway all need the same interaction, coding it three times gives three slightly different versions.

Interviewers use the pattern to check whether you can say what a service method coordinates (access checks, loading, the transaction, side effects), what it must not contain (the business rules themselves), and where the transaction starts and ends. Two neighboring topics place this layer in a bigger picture: Layered Architecture stacks it between presentation and data access, and Clean Architecture calls the same thing use cases.

Where the service layer sits

Every entry point calls the same service method, and only the service reaches the data and the outside systems.

Each of the four layers in the picture has a job and a list of things it must stay away from.

  • Controller or handler: parses the request, calls one service method and shapes the response. It must not hold business rules, transactions or SQL.
  • Service layer: holds the steps of each use case: check access, load, call the domain, commit, notify. It must not hold the rules themselves (prices, limits, state changes).
  • Domain objects: hold the rules, such as order.mark_paid() and product.reserve(qty). They must not make database calls, HTTP calls or send email.
  • Repositories: load and save domain objects (see the Repository Pattern topic). They must not hold business rules or commit.

Interview questions

  • What belongs in a service layer, and what belongs in the controller?
  • What is the difference between an application service and a domain service?
  • Where does the transaction start and end, and what about calls to other systems?
  • What is an anemic domain model, and how does a service layer cause it?
  • Your services call each other in loops and one service has grown to forty methods. How do you fix it?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio