Most books describe architecture. This one builds it. Explore Layered, Hexagonal, Modular Monolith, and Microservices architectures through the development of a single application, implemented in Java, step by step, with all their trade-offs.
If you have ever had to maintain a complex “big ball of mud” enterprise application, while the business pushes for new features under an aggressive schedule, you know better than anyone how important it is to have a solid architecture. That means clearly defined rules that let us make modifications while understanding which parts of the system each change will impact.
However, it is not enough to look at beautiful architecture diagrams or to read long explanations about the benefits of microservices or modularity. Learning how to create solid structures requires solid coding practices. We have to learn which constructions our programming language offers, beyond classes and functions, to organize large applications. Only then will we be able to craft different software structures and start recognizing their benefits and drawbacks. Equally important, we have to learn how to validate the architecture rules automatically, because a diagram without enforcement eventually ends up as spaghetti code.
Throughout this book, we will iterate on a single case study, a cinema ticket system, to show how to code different architecture styles. We start with the essential concepts, such as the different views of software architecture, testing practices, and how to partition software horizontally (by technical layer) and vertically (by functionality). We also show several ways to validate boundaries, from package-private visibility to Ant, Checkstyle, ArchUnit, Maven multi-modules, and the Java Module System. Then, in each iteration, we study and implement an architecture style. We go through the layered architecture, comparing a Rich Domain Model with the classic procedural Transaction Script approach, then hexagonal (or ports and adapters), the modular monolith, and finally microservices. Before implementing microservices, we discuss the fundamental concepts behind distributed architectures, such as partial failures, the CAP theorem, two-phase commit, eventual consistency, and Sagas, together with the reasons that justify distributing a system. All of this is supported by solid architecture concepts, simple designs, and good practices, and every chapter closes with an honest discussion of the trade-offs of each approach.
We use Java 23 to implement all the styles, with Spring Boot and Hibernate, using the frameworks minimally so that the architecture does not depend on them. All the source code is available to the reader on GitHub. Although the implementation is done in Java, the same concepts can be applied in languages such as C#, Python, PHP, or Ruby.