Principles For Microservice Design - Think IDEALS, Rather Than SOLID
Principles For Microservice Design - Think IDEALS, Rather Than SOLID
AOT or JIT: Faster Startup, Faster Code, or Faster Both? (May 6th Webinar) - Save Your
Seat
Key Takeaways
For object-oriented design we follow the SOLID principles. For microservice design
we propose developers follow the “IDEALS”: interface segregation, deployability (is
on you), event-driven, availability over consistency, loose-coupling, and single
responsibility.
Interface segregation tells us that different types of clients (e.g., mobile apps, web
apps, CLI programs) should be able to interact with services through the contract
that best suits their needs.
Deployability (is on you) acknowledges that in the microservice era, which is also
the DevOps era, there are critical design decisions and technology choices
developers need to make regarding packaging, deploying and running
microservices.
Event-driven suggests that whenever possible we should model our services to be
activated by an asynchronous message or event instead of a synchronous call.
Availability over consistency reminds us that more often end users value the
availability of the system over strong data consistency, and they’re okay with
eventual consistency.
Loose-coupling remains an important design concern in the case of microservices,
with respect to afferent (incoming) and efferent (outgoing) coupling. Single
responsibility is the idea that enables modeling microservices that are not too large
or too slim because they contain the right amount of cohesive functionality.
https://www.infoq.com/articles/microservices-design-ideals/ 1/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
In 2000 Robert C. Martin compiled the five principles of object-oriented design listed
below. Michael Feathers later combined these principles in the SOLID acronym. Since
then, the SOLID principles for OO design have been described in books and became
well-known in the industry.
A couple of years ago, I was teaching microservice design to fellow developers when one
of the students asked, "Do the SOLID principles apply to microservices?" After some
thought, my answer was, "In part."
Months later, I found myself searching for the fundamental design principles for
microservices (and a catchy acronym to go with it). But why would such a question be
important?
This landscape can make a novice microservice developer dizzy with the many design
decisions and technology choices they can face in just one microservice project.
In this space, a core set of principles can help developers to aim their design decisions in
the right direction for microservice-based solutions.
Thus, we propose the following set of core principles for microservice design:
Interface segregation
Deployability (is on you)
https://www.infoq.com/articles/microservices-design-ideals/ 2/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
Event-driven
Availability over consistency
Loose coupling
Single responsibility
The principles don’t cover the whole spectrum of design decisions for microservices-
based solutions, but they touch the key concerns and success factors for creating modern
service-based systems. Read on for an explanation of these principles applied to
microservices -- the much-needed microservice "IDEALS."
Interface Segregation
The original Interface Segregation Principle admonishes OO classes with "fat" interfaces.
In other words, instead of a class interface with all possible methods clients might need,
there should be separate interfaces catering to the specific needs of each type of client.
The goal of interface segregation for microservices is that each type of frontend sees the
service contract that best suits its needs. For example: a mobile native app wants to call
endpoints that respond with a short JSON representation of the data; the same system
has a web application that uses the full JSON representation; there’s also an old desktop
application that calls the same service and requires a full representation but in XML.
Different clients may also use different protocols. For example, external clients want to
use HTTP to call a gRPC service.
https://www.infoq.com/articles/microservices-design-ideals/ 3/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
Instead of trying to impose the same service contract (using canonical models) on all
types of service clients, we "segregate the interface" so that each type of client sees the
service interface that it needs. How do we do that? A prominent alternative is to use an
API gateway. It can do message format transformation, message structure
transformation, protocol bridging, message routing, and much more. A popular
alternative is the Backend for Frontends (BFF) pattern. In this case, we have an API
gateway for each type of client -- we commonly say we have a different BFF for each
client, as illustrated in this figure.
https://www.infoq.com/articles/microservices-design-ideals/ 4/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
As developers, we have long been aware of the importance of properly packaging and
deploying software to an appropriate runtime topology. However, we have never paid so
much attention to the deployment and runtime monitoring as today with microservices.
The realm of technology and design decisions that here we’re calling "deployability" has
become critical to the success of microservices. The main reason is the simple fact that
microservices dramatically increase the number of deployment units.
So, the letter D in IDEALS indicates to the microservice developer that they are also
responsible for making sure the software and its new versions are readily available to its
happy users. Altogether, deployability involves:
Here is a list of strategies and technologies that developers should consider in any
microservice-based solution to improve deployability:
https://www.infoq.com/articles/microservices-design-ideals/ 5/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
https://www.infoq.com/articles/microservices-design-ideals/ 6/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
Event-Driven
The microservice architecture style is for creating (backend) services that are typically
activated using one of these three general types of connectors:
The first two are typically synchronous, HTTP calls being the most common alternative.
Often, services need to call others forming a service composition, and many times the
interaction in a service composition is synchronous. If instead, we create (or adapt) the
participating services to connect and receive messages from a queue/topic, we’ll be
creating an event-driven architecture. (One can debate the difference between message-
driven and event-driven, but we’ll use the terms interchangeably to represent
asynchronous communication over the network using a queue/topic provided by a
message broker product, such as Apache Kafka, RabbitMQ, and Amazon SNS.)
An important benefit of an event-driven architecture is improved scalability and
throughput. This benefit stems from the fact that message senders are not blocked
waiting for a response, and the same message/event can be consumed in parallel by
multiple receivers in a publish-subscribe fashion.
https://www.infoq.com/articles/microservices-design-ideals/ 7/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
Event-driven microservice
https://www.infoq.com/articles/microservices-design-ideals/ 8/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
Some business operations do require strong consistency. However, as Pat Helland points
out, when faced with the question of whether you want it right or you want it right now,
humans usually want an answer right now rather than right.
For microservices, the main strategy that enables the availability choice is data
replication. Different design patterns can be employed, sometimes combined:
A CQRS design we often use at my workplace is shown in the figure next. HTTP requests
that can change data are processed by a REST service that operates on a centralized
Oracle database (this service uses the Database per Microservice pattern nonetheless).
The read-only HTTP requests go to a different backend service, which reads the data
from an Elasticsearch text-based data store. A Spring Batch Kubernetes cron job is
executed periodically to update the Elasticsearch store based on data changes executed
on the Oracle DB. This setup uses eventual consistency between the two data stores. The
query service is available even if the Oracle DB or the cron job is inoperative.
https://www.infoq.com/articles/microservices-design-ideals/ 9/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
Loose-Coupling
In software engineering, coupling refers to the degree of interdependence between two
software elements. For service-based systems, afferent coupling is related to how service
users interact with the service. We know this interaction should be through the service
contract. Also, the contract should not be tightly coupled to implementation details or a
specific technology. A service is a distributed component that can be called by different
programs. Sometimes, the service custodian doesn’t even know where all the service
users are (often the case for public API services). Therefore, contract changes should be
avoided in general. If the service contract is tightly coupled to the service logic or
technology, then it is more prone to change when the logic or technology needs to
evolve.
Services often need to interact with other services or other types of components thus
generating efferent coupling. This interaction establishes runtime dependencies that
directly impact the service autonomy. If a service is less autonomous, its behavior is less
predictable: in the best-case scenario, the service will be as fast, reliable, and available as
the slowest, least reliable, and least available component it needs to call.
https://www.infoq.com/articles/microservices-design-ideals/ 10/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
The letter L in IDEALS prompts us to be attentive to coupling for services and therefore
microservices. Several strategies can be used and combined to promote (afferent and
efferent) loose coupling. Examples of such strategies include:
Single Responsibility
The original Single Responsibility Principle (SRP) is about having cohesive functionality
in an OO class. Having multiple responsibilities in a class naturally leads to tight
coupling, and results in fragile designs that are hard to evolve and can break in
unexpected ways upon changing. The idea is simple, but as Uncle Bob pointed out, SRP
is very easy to understand, but difficult to get right.
https://www.infoq.com/articles/microservices-design-ideals/ 11/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
The notion of single responsibility can be extended to the cohesiveness of services within
a microservice. The microservice architecture style dictates that the deployment unit
should contain only one service or just a few cohesive services. If a microservice is
packed with responsibilities, that is, too many not quite cohesive services, then it might
bear the pains of a monolith. A bloated microservice becomes harder to evolve in terms
of functionality and the technology stack. Also, continuous delivery becomes
burdensome with many developers working on several moving parts that go in the same
deployment unit.
On the other hand, if microservices are too fine-grained, more likely several of them will
need to interact to fulfill a user request. In the worst-case scenario, data changes might
be spread across different microservices, possibly creating a distributed transaction
scenario.
Right-grained microservices
A service (e.g., a REST service) can have the scope of a DDD aggregate.
A microservice can have the scope of a DDD bounded context. Services within that
microservice will correspond to the aggregates within that bounded context.
For inter-microservice communication, we can use: domain events when
asynchronous messaging fits the requirements; API calls using some form of an
anti-corruption layer when a request-response connector is more appropriate; or
data replication with eventual consistency when a microservice needs a substantial
amount of data from the other BC readily available.
Conclusion
https://www.infoq.com/articles/microservices-design-ideals/ 12/13
24/4/2021 Principles for Microservice Design: Think IDEALS, Rather than SOLID
IDEALS are the core design principles to be followed in most typical microservice
designs. However, following the IDEALS is not a magic potion or spell that will make our
microservice design successful. As always, we need to have a good understanding of the
quality attribute requirements and make design decisions aware of their tradeoffs.
Moreover, we should learn about the design patterns and architecture tactics that can be
employed to help realize the design principles. We should also have a good grasp of the
technology choices available.
Many thanks to Joe Yoder for helping to evolve these ideas into IDEALS.
17
https://www.infoq.com/articles/microservices-design-ideals/ 13/13