Java Microservices: CQRS & Event Sourcing with Kafka

Most, if not all, engineers at Sourcelabs have been working on systems that have been designed from ground up with a Microservices architecture in mind or monolithic systems that have been decomposed into several smaller (micro)services.

One of the most interesting parts of Microservices is dealing with communication between different services. During our latest meetup we dived into the subject of CQRS and Event Sourcing as a means of doing inter Microservice communication, we selected a Udemy course created by Sean Campbell (not related to our very own Duncan Campbell) as our primary source of knowledge for the meetup: https://www.udemy.com/course/java-microservices-cqrs-event-sourcing-with-kafka/.

The following part from the course description was one of the main reasons why we selected this course: You will not use any CQRS framework, but you will write every line of code that is required to effectively create your own CQRS and Event Sourcing framework using Java and Apache Kafka. While this might sound a little daunting, you will be carefully guided step by step, and gain all the know-how and confidence to become an expert in CQRS and Event Sourcing. At Sourcelabs we prefer to get a deeper understanding of how things really work under the hood. Most frameworks make it easier for developers to implement certain concepts, but they also hide the real complexity. When learning a new concept or technology, implementing it yourself from scratch gives you a deeper understanding of all the complexity or boilerplate code a framework has already implemented for you.

One thing to note here, CQRS does not need Event Sourcing and vice versa. It’s just that this course covers both topics. If you have no clue what CQRS is, then this might be a good read for you: https://martinfowler.com/bliki/CQRS.html

#LFG

The first part of the course explains the basic concepts of Microservices, CQRS and Event Sourcing. Next to that it introduces Apache Kafka which is used in this course as the underlying technology for sourcing events.

This was the first time we used a Udemy course as a primary source for the meetup content. We wanted be able to complete the entire course by the end of the day. Since it ‘only’ contains 4.5 hours of on-demand video content this seemed doable. This does mean: sit back, watch video, drink coffee, repeat 🙂 From time to time we paused the videos for some interesting discussions, lunch, ice-cream breaks (see image below), etc.

After the introduction the setup and structure are explained. The instructor explains step-by-step how to build an application that demonstrates all the aspects of a Microservice using CQRS and Event Sourcing without using a framework like Axon. By not using a framework like Axon you experience first-hand the amount of infrastructure code you need to write in order to get the system up and running.

As a course student you are guided through the steps of setting up the project structure using Spring Initializer, IntelliJ and Maven. There are a few different modules that have to be developed, one of them is a command part, which is a Spring Boot application that uses Spring Data MongoDB for storing events and Spring Kafka for publishing data (aggregates). Next to that there is also a query part which is a Spring Boot application that consumes data (aggregates) from Kafka using Spring Kafka and stores them locally in a MySQL database using Spring Data JPA. It provides endpoints for reading the ‘latest’ version of the data from the MySQL database. The whole idea of separating the command and query part is that the two applications can work independently from each other. Separating the read (query) and write (events) model of the application enables us to support some specific use-cases, like being able to optimize the data being read or to be able get a snapshot of the data at a specific moment in time. After completing all the lectures in this course the result should be a working CQRS based Microservices application that uses Event Sourcing.

Conclusion

Flexibility almost always comes at the cost of complexity. I strongly feel that it very much depends on the context and use-case if you really want (need) CQRS, Event Sourcing or even a Microservices architecture. If you don’t really know yet, or have little to no experience implementing CQRS or event sourcing, then please, please, keep it simple. That does not mean we don’t encourage you to experiment with it, please give it a try, get familiar with it, but don’t go all-in because you successfully completed a Udemy course. Microservices have also been around for quite some time now, but not necessarily has it made the businesses we support more agile, on the contrary.

Nom nom nom

A Sourcelabs meetup wouldn’t be a proper meetup without finishing the day with proper meal. This time we visited restaurant Balijepark in Utrecht which is highly recommended! Small impression of the food check out the images below:

Did you enjoy this content? We have got more on the way! Sign up here for regular updates!

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *