The geometry of microservices - cubes, hexagons, triangles, and more
One of my favorite talks on the microservice architecture is my JFokus 2020 talk Cubes, Hexagons, Triangles, and More - Understanding Microservices. It uses a series of shapes as visual metaphors to explain key microservice architecture concepts.
Let’s have a look at the first two shapes: the success triangle, which provides the motivation for using microservices, and the scale cube, which defines the microservice architecture.
The success triangle
In order to thrive in today’s crazy world, businesses need to be more nimble, more agile and innovate faster. And since most modern businesses are powered by software, IT must deliver software rapidly, frequently, and reliably as measured by the DORA metrics.
Delivering software rapidly, frequently, and reliably requires what I call the success triangle. The success triangle consists of the following elements:
- Process - DevOps as defined by the DevOps handbook
- Organization - a network of loosely coupled, cross functional, autonomous teams as described by Team Topologies
- Architecture - a loosely coupled, modular, highly deployable and testable architecture that supports the loosely coupled network of DevOps teams.
The scale cube
The Monolithic architecture is suitable for many application, especially those that are small and are developed by a small number of teams. However, for larger, more complex applications that are developed by many teams, it’s often better to use the Microservice architecture. My definition of the microservice architectures corresponds to Y-axis scaling from AKF Scale Cube, which functionally decomposes an application into services.
The hexagonal architecture
The hexagonal architecture, a.k.a. ports and adapters, is a great way to structure a service. Unlike the more traditional layered architecture, the hexagonal architecture puts the business logic at the heart of the service. Inbound adapters that handle requests from the service’s clients and outbound adapters which make requests depend on the business logic.
An essential characteristic of the microservice architecture is loose design-time coupling. Services should rarely need to be changed in lockstep. One way to achieve loose design-time coupling is to design services so that they resemble icebergs. The API, which is the visible part of the service, should be much smaller than the invisible implementation. By keeping the implemention hidden, developers can change it without affecting clients.
Another essential characteristic of the microservice architecture is loose runtime coupling. Tight runtime coupling between services reduces availability and increases latency. To minimize runtime coupling, services should be self-contained. A self-contained service responds to a synchronous request without waiting for another service to respond. If necessary, it uses asynchronous messaging to complete the request. The cylinder depicts a message channel, which is how services communicate asynchronously.
Even today, many organizations often rely on manual testing. Yet automated testing is essential if you want to deliver software, rapidly, frequently and reliably. Moreover, the microservice architecture simultaneously simplifies testing and also creates some challenges. The sixth and final shape is the test pyramid. The test pyramid is a visual metaphor that shows the relative proportions for the different types of tests that you need to write for a service.
The tests at the bottom of the pyramid are fast, cheap to write and reliable, while as you move up the pyramid, the tests become progressively slower, more expensive to write and less reliable. The test pyramid is consists of the following layers:
- unit tests - fast, in-memory tests of classes or clusters of classes. If your service implements complex algorithms, you should test them thoroughly with unit tests.
- integration tests - tests that verify that the service’s adapters work as expected. A service’s adapters are those classes that communicate with the outside world, eg. controllers, which handle HTTP requests, and repositories, which access a database. Integration tests also include consumer-driven contract tests, which verify that a service communicates correctly with its clients and dependencies.
- component tests - tests that (unlike unit tests and integration tests, which test part of a service) verify that the service as a whole works correctly. A service’s component test might, for example, package the service as a container image, and test the running container.
- end-to-end tests - tests that verify that the application as a whole works correctly. It’s important to remember, however, that a defining characteristic of the microservice architecture is that services are independently deployable. A service that passes the unit, integration and component tests must be production ready.
The test pyramid is not a perfect metaphor, however. You might, for example, have more integration tests than unit tests. However, a key idea is that you should push testing down the pyramid as much as possible. In particular, you should avoid end-to-end testing, at least on the path from developer laptop to production.
Want to learn more about testing microservices?
My Microservices patterns book has two chapters dedicated to testing microservices.
Need help adopting microservices?
I provide consulting and training.