Bonus content

Service Design

Bonus

Designing Service Boundaries

The principles can also guide service design. We can ask what each service owns, what it needs from other services, and whether a change in one requires changes in another.

Suppose the store runs orders and message delivery as separate services. When the orders service marks an order shipped, it publishes an OrderShipped event. A message broker delivers the event to email and push delivery services. Because these services can be deployed separately, they need to agree on the event’s data and meaning.

The service boundary

An event connects the services

Orders serviceRecords the shipment
OrderShipped eventDelivered by a message broker
Email delivery serviceCalls the email provider
Push delivery serviceCalls the push provider
The orders service publishes one event. The broker delivers it to each subscribed delivery service.

Single Responsibility

Before
Orders serviceTracks shipments and sends email
After
Orders serviceTracks shipments
OrderShipped eventThrough the broker
Email delivery serviceSends email
The two services can change and deploy for different reasons.

The orders service owns shipment status; the email delivery service owns sending email. A change to email retries can be released without changing the service that marks orders shipped.

Open/Closed

Orders servicePublishes OrderShipped
Message broker
Email delivery service
Push delivery serviceNew subscriber
The new push service subscribes through the broker. The orders service still publishes the same event.

The orders service publishes OrderShipped without naming its subscribers. Adding a push delivery service changes the broker’s subscriptions, while the orders service keeps publishing the same event.

Liskov Substitution

Current service
OrderShipped eventExisting required fields
Email service v1Accepts the event
Replacement service
Same OrderShipped eventNo locale field required
Email service v2Still accepts the event
A replacement cannot require fields the current event contract does not provide.

A service-level parallel is replacing email service v1 with v2 while keeping the same input contract. V2 cannot require a new locale field in OrderShipped; older events lack it. It can use a default locale.

Interface Segregation

Before
OrderChanged eventShipment, payment, and refund data
Email delivery serviceDepends on the broad event
After
OrderShipped eventShipment data only
Email delivery serviceDepends on this event
The delivery service depends on the event contract it uses.

A broad OrderChanged event makes the email delivery service inspect shipment, payment, and refund updates. A specific OrderShipped event lets it depend on only the shipment information it needs.

Dependency Inversion

Code dependencies
Orders service
Delivery services
OrderShipped eventOrder-domain contract
Both services use the order-domain event contract. Provider details stay in the delivery services.

The orders service publishes an order-domain event rather than calling an email provider. Delivery services translate OrderShipped into provider requests, so the orders service does not import their clients or vendor SDKs.

Putting it into practice

The cost of a service boundary

Separate services need reliable event delivery, retries, and compatible event formats. Those costs make sense when teams need to deploy or operate the services independently. Otherwise, the same responsibilities can stay separated inside one application.

Sources: AWS on events between services, Merson on design principles for services, Robinson on service contracts, and Martin on architectural boundaries.