Axon Framework is a Java microservices framework that helps you build microservices architecture in-line with Domain Driven Design (DDD) principles.
Apart from DDD, Axon Framework also allows you to implement microservices patterns such as Command-Query-Responsibility-Segregation (CQRS) and Event-Driven Architecture.
In this post, we try to understand how Axon Framework can be leveraged for building microservices.
Axon Framework and Domain-Driven-Design
Axon Framework is heavily inspired by Domain-Driven-Design concepts. Due to this, some of the concepts in the framework have been directly picked up from the Domain-Driven-Design jargon.
Let’s look at some of these concepts:
As with DDD, Axon Framework also uses Aggregate as the core idea of an application. Aggregate is basically your Domain Object. In other words, it is the reason for the existence of the application.
If you have worked with normal Spring Boot Microservices, you can compare these Aggregates to the Entity classes.
Axon Framework also embraces the concept of Domain-Events. In other words, any command targeting an Aggregate creates one or more events. These events are also known as Domain-Events.
Though Axon doesn’t demand it, these Domain-Events should reflect your domain language closely.
The Aggregate handles the events and changes the state of the Aggregate instance.
In the below snippet from an Event Sourcing sample application, we see a couple of methods annotated by @EventSourcingHandler. These methods are triggered whenever the event associated with them occurs.
In Domain Driven Design terminology, Repository is a very important concept. Repository is an abstraction that connects the domain representation of an object with its persistence representation.
In Axon, Aggregates are persisted based on the available data-source. If you’re using Axon with Spring Boot, the Spring Data JPA datasource is automatically wired up for persisting the Aggregates or the Domain Objects.
Message Driven Aspect of Axon Framework
Axon is predominantly message-driven. In other words, various components connected in an Axon-based application communicate through messages.
Messages contain the message payload, meta-data and some sort of identifier. Also, the messages in Axon Framework are immutable. Therefore, they are safe to use in a multi-threaded distributed environment.
There are different types of messages:
Commands are an intent to change the application state. They are mainly read-only POJOs.
The sender sends a Command to an application. The sender might not care about the command handling. However, it might want to know the outcome of the command. Therefore, you can return a result for a command that can be useful to the sender.
Events are basically objects that describe something that has happened in an application. From a Domain-Driven-Design perspective, events are applied on the Aggregate or the Domain Entity.
Events are usually side-effects arising from the Commands. Therefore, if a Command is successfully processed, usually an Event occurs on the Domain Entity.
A third type of message typical to the Axon Framework is Queries.
Queries typically describe a request for some information. In other words, it could be the current state of the Aggregate.
Axon Framework – Current State
Axon Framework currently is at version 4.0.
With this version, it has basically become more than a framework. Axon 4.0 is also officially known as the Axon Platform.
The Axon Platform basically comprises of the Axon Server and Axon Framework.
Axon Server acts as the glue between various microservices in a typical microservices architecture. It handles all the communication between various applications.
Working with Axon Server to build distributed applications is easy and intuitive.
Another great thing about Axon Framework is its synergy with Spring Boot. As you might be aware, Spring Boot is an extremely robust framework for building microservices. Spring Boot Microservices are fast to build and easy to maintain.
The good news is that you can tweak your Spring Boot application to work with Axon Framework with next to no-code or configuration. To do so, simply include Axon Spring Boot Starter as a dependency in a Spring Boot project.
Axon and Spring Boot together act as a great choice to build distributed systems based on a microservices architecture.
Axon Framework – Application Examples
At this point, you might be wondering how to use Axon Framework in your application.
Axon Framework aims to solve challenges arising out of microservices design patterns. Some of popular design patterns such as Event Sourcing, CQRS and Saga lend themselves very well to Axon.
At Progressive Coder, we have detailed code samples on each of these topics.
Event Sourcing with Axon Framework
Event Sourcing with Axon and Spring Boot – Part 1 deals with understanding the concept behind Event Sourcing.
In Event Sourcing with Axon and Spring Boot – Part 2 we deal with the implementing event sourcing using Axon Framework.
In Event Sourcing with Axon and Spring Boot – Part 3, we deal put the final touches to our implementation. We also test our application.
Event Souring and CQRS with Axon Framework
Event Sourcing and CQRS with Axon and Spring Boot – Part 1 deals with understanding how Event Sourcing and CQRS can work in conjunction.
In Event Sourcing and CQRS with Axon and Spring Boot – Part 2, we look at the implementation steps for Event Sourcing and CQRS. We also test our application.
Axon Server and Spring Boot
Axon Server helps scaling your distributed microservices. It acts as a hub for exchanging messages.
In Building Microservices with Axon and Spring Boot, we modify our Event Sourcing CQRS application to use Axon Server. We also look at the various features Axon Server provides.
Saga Pattern Implementation with Axon and Spring Boot
Saga Pattern Implementation with Axon and Spring Boot – Part 1 deals with understanding what is Saga Pattern and why do we need to use it in certain conditions. We also look at types of Saga implementations.
Saga Pattern Implementation with Axon and Spring Boot – Part 2 walks through the process of starting the Saga Implementation.
In Saga Pattern Implementation with Axon and Spring Boot – Part 3, we continue implementing the Saga. We also setup Axon Server.
Lastly, in Saga Pattern Implementation with Axon and Spring Boot – Part 4, we test our Saga Implementation.
Axon Framework is a great tool for developers wanting to build distributed microservices that are scalable, performant and easily maintainable.
Axon Framework with Axon Server adds another dimension to the framework.
However, one of the best parts about Axon is its ability to work well with other microservices frameworks such as Spring Boot. Based on my discussions, I can see more and more companies adopting the framework for handling production workloads.
You can also read more about Axon at the official website.