How Kafka Relates to Axon Framework?

Reading Time: 3 minutes

Axon and Kafka are used for different purposes, Axon is used for Event-Driven Architecture and provides the application-level support for domain modeling and Event Sourcing, as well as the routing of Commands and Queries, while Kafka serves as an Event Streaming system.

The basic fundamental of Axon is to implement CQRS and Event Sourcing-based architecture.  With the help of this, we can design & develop applications by treating them as a System of Events.

Apache Kafka is a very popular system for distributed event store and stream-processing platforms. Its architecture is fundamentally different from most messaging systems and combines reliability, durability, etc.

Note:
Event streaming-: Event streaming is the practice of capturing data in real-time from event sources like databases, sensors, mobile devices, cloud services, and software applications. Storing these event streams durably for later retrieval, manipulating, processing, and reacting to the event streams in real-time and retrospectively and routing the event streams to different destination technologies as needed.

Event streaming ensures a continuous flow and interpretation of data so that the right information is at the right place, at the right time.

Axon provides an extension dedicated to publishing and receiving event messages from Kafka. The Kafka Extension is an alternative approach to distributing events, besides Axon Server (the default). It’s recommended to use a built-for-purpose event store like Axon Server, or alternatively an RDBMS-based (JPA or JDBC)

Kafka Extension components from Axon

Kafka Extension components are used from Axon and make sure the axon-Kafka module is available on the application classpath. Using the extension requires setting up and configuring Kafka in the project’s requirements.

Kafka is a perfectly fine event distribution mechanism but it is not a good fit for an event store.

Axon Publishing Events to Kafka

When Event Messages are published to an Event Bus (or Event Store), they can be forwarded to a Kafka topic using the KafkaPublIn summarise.

Since the KafkaEventPublisher is an event message handler in Axon terms, we can provide it to any Event Processor to receive the published events.

Configuring Event Publication to Kafka

Axon provides the DefaultProducerFactory implementation of the ProducerFactory, which should be instantiated through the provided DefaultProducerFactory.Builder class.

Topic partition publication considerations

Kafka ensures message ordering on a topic-partition level, not an entire topic. To control events of a certain group to be placed in a dedicated partition, based on an aggregate identifier for eg. the message converter’s SequencingPolicy can be utilized.

Axon Consuming Events from Kafka

Axon application can be consumed through either Subscribing or a Tracking Event Processor. Both options are there when it comes to consuming events from a Kafka topic, which from a set-up perspective translates to a SubscribableMessageSource or a StreamableKafkaMessageSource respectively.

In summary

Axon serves as an event-driven microservice system that is based on the principles of Domain Driven Design, CQRS, and Event Sourcing and while Kafka as an Event Streaming platform thus ensures a continuous flow and interpretation of data so that the right information is at the right place, at the right time.To help integrate with Kafka, Axon provides a Kafka connector as part of the Axon Framework (https://docs.axoniq.io/reference-guide/extensions/kafka). The purpose of the connector is to help utilize the best of both services i.e. Axon for its Event Sourcing Capabilities and Command/Query Message processing while delegating the responsibility of event delivery to downstream utilizing Kafka.

Written by 

Abid Khan is a Lead Consultant at Knoldus Inc., postgraduate (MCA), and having 5+ years of experience in JavaSE, JavaEE, ORM framework, Spring, Spring-boot, RESTful Web Services, Kafka, MQTT, Rabbitmq, Docker, Redis, MySQL, Maven, GIT, etc. He is a well-developed professional with a prolific track record of designing, testing, and monitoring software as well as upgrading the existing programs.