Event Driven Microservice Spring Boot with Apache Kafka

Apache kafka logo

Apache Kafka in simple terms is a collection of logs of events when some data in a database changes, divided into topics. This is why it is called event driven architecture, when some service saves data in a database, you use Kafka to produce an event so that other services (wherever it is located) can listen on that event and do some computation whatever they want based on the event that is firing.

Kafka is usually used for microservices architecture, so instead of reading data from a database, each microservices communicate with Kafka events, which we call produce and consume.

Produce is Kafka term to emit a message to a specific topic, meanwhile consume is to listen to an event that contains data that is produced by some other microservices.

Monolithic application

Microservice architecture application

What is Event-driven Microservices

Microservice design is a way to develop an application and split it into small independently deployable services, each microservice has specific business capabilities that are separated to other microservice. Microservices make the development of some specific logic separated and be independent from each other. In a monolithic application if one fails it can impact the whole application, but with microservice if one microservice failed, it doesn’t affect other services. So, it prevents large-scale failure.

In request driven architectural style, communication between different microservices is not efficient. The request-driven style if you want some data, it then makes a single request to some service, and the client would wait until the service responds with the result.  

Event driven instead of waiting for an infinite period of time waiting for service response, the event driven messaging system will inform the other service when the response is ready via subscription to some specific topic.  Event-driven microservices code architecture provides real-time communication between microservices, so each microservice consumes the data in the form of events whenever other services produce the data somewhere out there.

Event Driven Microservice with Apache Kafka

Follow these steps to install Kafka on your local computer, so you can get a better understanding of how Kafka works. Using a simple spring boot microservices ping and pong example.

You need to have:

- Docker

- Java

- Git, to clone the example source code

1. Create docker-compose.yml to run Kafka server from docker

2. To run your Kafka server

docker-compose up -d

3. If you want to stop your Kafka server run this command, but don’t do this for now

docker-compose rm -fsv

4. Clone this full code

This is the full event driven microservice example using spring boot codes with Apache Kafka as messaging broker. You can clone this full code and test it yourself, understand better what microservice event driven software design.

Git repo: https://github.com/mudiadamz/event-driven-microservice-example.

Follow the Readme instruction on that repository to run and debug the application.

If you want to know what Apache Kafka is, there's a good explanation of what Apache Kafka is, and how it works for beginners from Confluent YouTube channel from here https://www.youtube.com/watch?v=06iRM1Ghr1k.

And what is event driven architecture is here the video explanation to help you understand better. Here from Coding tech watch the video here https://youtu.be/sSm2dRarhPo.


Popular posts from this blog

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Spring Kafka - How to use ReplyingKafkaTemplate send and reply synchronously