application architecture   service design   service api  

Icebergs, the Interface Segregation Principle and microservices

An essential characteristic of the microservice architecture is loose design-time coupling. In a loosely coupled architecture, changes to a service rarely require other services to be changed in lockstep. As a result, it’s easier to make changes. What’s more, teams need to spend much less time coordinating their work.

If you neglect design-time coupling, you risk creating a distributed monolith, which combines the complexity of the microservice architecture with the friction of a monolith. While such an architectural disaster might result in conference talk about why microservices are a bad idea, it could create an existential crisis for your business and is best avoided.

Services should be like icebergs

Ensuring that your services are loosely coupled requires careful design. One very helpful idea is the Iceberg principle.

A service should be like an iceberg, mostly below the waterline. Its API, which consists of operations and published events, is the visible part of the service. It should be much smaller than the service’s (hidden) implementation. That’s because what’s hidden can be easily changed. A service’s API should meet the clients’ needs while hiding much of the implementation.

When a service doesn’t look like an iceberg

Sometimes, however, a service doesn’t look like an iceberg. Consider, the takeout Burrito example from my 2021 QCON presentation on design-time coupling.

Takeout burritos version 1

There are two versions of the food ordering application. The first version looks like this:

The Restaurant Service has an API that implements <crud>Restaurant() operations and publishes Restaurant<crud> events, which contain the restaurant’s menu. The Order Service uses the Restaurant<crud> events to maintain a CQRS replica of restaurant menus, which it uses to validate and price an orders.

This architecture seems simple. But one drawback, however, is that the Order Service is coupled to the menu structure. Enhancing the application to support customized burritos requires the Restaurant Service and the Order Service to change in lock step.

Let’s look at an architecture alternative that has less coupling between the Order Service and Restaurant Service.

Takeout burritos version 2

The second version of the architecture reduces coupling by encapsulating all knowledge of the menu structure within the Restaurant Service. Responsibility for storing, validating the line items and calculating the order subtotal is moved from the Order Service to the Restaurant Service. The Order Service is primarily responsible for calculating fees, taxes, yet more fees and the order total.

In this version of the architecture, Restaurant Service’s API still defines <crud>Restaurant() operations. But, instead of publishing Restaurant<crud> events, it publishes simpler RestaurantOrder<crud> events, which contain the outcome of validating and pricing the order. The Order Service uses the subtotal from the RestaurantOrderCreated event to calculate the orderTotal.

On the one hand, the Order Service is significantly less coupled to the Restaurant Service. It has no knowledge of a menu’s structure. The Order Service now only consumes a very simple event that contains an outcome and a subtotal. But on the other hand, the complexity of Restaurant Service’s API has barely changed. It still doesn’t look like an iceberg.

Applying the interface segregation principle

An important OO design principle is Interface Segregation Principle (ISP). It states that a client should not be exposed to methods in an interface that it does not use. In other words, a class should have multiple smaller, specialized interfaces rather than one large interface. Perhaps, the same is true of services albeit for different reasons.

With the ISP in mind, another way of looking at the Restaurant Service is that it has two APIs.

The first API is Restaurant Management, which defines the <crud>Restaurant() operations. It’s used by the Restaurant Management UI to manage restaurants. In fact, you can think of this API as internal to the Restaurant Management capability. Consequently, the Iceberg principle does not need to apply.

The second API implemented by the Restaurant Service is Restaurant Order Management. This API publishes events such as RestaurantOrderCreated. It’s used by services outside of Restaurant Management, such as the Order Service. The Restaurant Order Management API should and, in fact, does conform to the Iceberg principle. It’s a simple API that encapsulates the implementation details of the Restaurant Service.


application architecture   service design   service api  


Copyright © 2023 Chris Richardson • All rights reserved • Supported by Kong.

About Microservices.io

Microservices.io is brought to you by Chris Richardson. Experienced software architect, author of POJOs in Action, the creator of the original CloudFoundry.com, and the author of Microservices patterns.

Chris helps clients around the world adopt the microservice architecture through consulting engagements, and training workshops.

Learn how to create a service template and microservice chassis

Take a look at my Manning LiveProject that teaches you how to develop a service template and microservice chassis.

New virtual bootcamp: Distributed data patterns in a microservice architecture

My virtual bootcamp, distributed data patterns in a microservice architecture, is now open for enrollment!

It covers the key distributed data management patterns including Saga, API Composition, and CQRS.

It consists of video lectures, code labs, and a weekly ask-me-anything video conference repeated in multiple timezones.

The regular price is $395/person but use coupon JUNVCEJE to sign up for $195 (valid until February 1st, 2023). There are deeper discounts for buying multiple seats.

Learn more

Signup for the newsletter


LEARN about microservices

Chris offers numerous resources for learning the microservice architecture.

Training classes

Chris teaches comprehensive workshops, training classes and bootcamps for executives, architects and developers to help your organization use microservices effectively.

Avoid the pitfalls of adopting microservices and learn essential topics, such as service decomposition and design and how to refactor a monolith to microservices.

Delivered in-person and remotely.


Get the book: Microservices Patterns

Read Chris Richardson's book:

Example microservices applications

Want to see an example? Check out Chris Richardson's example applications. See code

BUILD microservices

Ready to start using the microservice architecture?

Consulting services

Engage Chris to create a microservices adoption roadmap and help you define your microservice architecture,


The Eventuate platform

Use the Eventuate.io platform to tackle distributed data management challenges in your microservices architecture.

Eventuate is Chris's latest startup. It makes it easy to use the Saga pattern to manage transactions and the CQRS pattern to implement queries.

ASSESS your architecture

Assess your application's microservice architecture and identify what needs to be improved.

Consulting services

Engage Chris to conduct an architectural assessment.



Join the microservices google group

Topics

Note: tagging is work-in-process

anti-patterns   ·  application api   ·  application architecture   ·  architecting   ·  architecture documentation   ·  dark energy and dark matter   ·  deployment   ·  development   ·  devops   ·  docker   ·  implementing commands   ·  implementing queries   ·  inter-service communication   ·  loose coupling   ·  microservice architecture   ·  microservice chassis   ·  microservices adoption   ·  microservicesio updates   ·  multi-architecture docker images   ·  observability   ·  pattern   ·  refactoring to microservices   ·  resilience   ·  sagas   ·  security   ·  service api   ·  service collaboration   ·  service design   ·  service discovery   ·  service granularity   ·  service template   ·  software delivery metrics   ·  success triangle   ·  team topologies   ·  transaction management   ·  transactional messaging

All content


Posts

24 Jul 2017 » Revised data patterns