This project demonstrates the implementation of the CQRS (Command Query Responsibility Segregation) design pattern in a Products Microservice. It leverages Apache Kafka for event-driven communication and Spring Boot for building a robust and scalable microservices.
- Introduction
- Features
- Technologies Used
- Architecture Overview
- Getting Started
- Usage
- Contributing
- License
This repository aims to provide a practical implementation of the CQRS design pattern in a microservices architecture. The CQRS pattern separates the write operations (commands) from the read operations (queries), allowing for improved scalability, flexibility, and maintainability.
By integrating Apache Kafka, this project ensures reliable and asynchronous communication between different components of the system using event streams.
- Command and Query Separation: Ensures that write and read operations are handled independently.
- Event-Driven Architecture: Utilizes Apache Kafka (with KRaft) for event publishing and subscribing.
- Microservice Design: The application is structured as separate product command and query microservices.
- Scalable and Resilient: Designed to handle a large number of concurrent requests with ease.
- Spring Boot Integration: Simplifies development with pre-built components and configurations.
- Java 21: 100% of the codebase is written in Java.
- Spring Boot: For building the microservice.
- Apache Kafka (with KRaft): For event-driven communication.
- Spring Data JPA: For database interactions.
- MySQL Separate databases for each microservice
- Maven: For dependency management and build automation.
- Apache Avro: Schema-based serialization for lightweight and efficient data encoding.
- Confluent Schema Registry: Centralized repository for managing and evolving Avro schemas.
The architecture is based on the CQRS pattern and consists of the following components:
-
Command Side:
- Handles write operations (Create, Update, Delete).
- Publishes events to Apache Kafka after executing commands.
-
Query Side:
- Handles read operations.
- Listens to Kafka events and updates the read model for efficient querying.
-
Event Bus:
- Apache Kafka acts as the message bus for publishing and consuming events.
- Ensures decoupled communication between the command and query sides.
To run this project, you'll need:
- Java 17 or higher
- Apache Kafka installed and running
- Maven 3.9+ installed
-
Clone the repository:
git clone https://github.com/bruce-mig/cqrs.git cd cqrs
-
Install dependencies:
cd product-command-service
mvn clean install
----
cd product-query-service
mvn clean install
-
Configure Apache Kafka:
- Run the following command
docker compose up -d
- Ensure Kafka is running locally on port
9092
. - Update Kafka configurations in
application.yml
if necessary.
- Run the following command
-
Run the application:
cd product-command-service mvn spring-boot:run ---- cd product-query-service mvn spring-boot:run
-
Create a Product:
- In Command service, Send a POST request to
/api/v1/products
with product details.- Example:
{ "name": "Book", "description": "Gödel, Escher, Bach: an Eternal Golden Braid", "price": 150 }
- Example:
- In Command service, Send a POST request to
-
Query Products:
- In query service, Send a GET request to
/api/v1/products
to fetch all products.
- In query service, Send a GET request to
-
Update/Delete Products:
- Use respective endpoints under
/api/v1/products
for update and delete operations.
- Use respective endpoints under
-
Monitor Kafka Events:
- Use Kafka tools to monitor the
product-event-topic
topic for published events.
- Use Kafka tools to monitor the
Contributions are welcome! If you'd like to contribute, please follow these steps:
- Fork 5A87 the repository.
- Create a new feature branch:
git checkout -b feature-name
- Commit your changes:
git commit -m "Description of changes"
- Push to your branch:
git push origin feature-name
- Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.
Happy coding! If you have any questions or suggestions, feel free to open an issue or reach out. 🚀