This project serves as a practical demonstration of Command Query Responsibility Segregation (CQRS) principles using Spring Boot. It comprises two distinct services: product-query-service and product-command-service. These services communicate via Apache Kafka, enabling an event-driven architecture for seamless synchronization between their respective databases.
This service is responsible for handling read operations, specifically supporting GET requests. It serves as the query side of the application.
The command service supports POST and PUT operations, functioning as the command side. It processes commands to create and update products.
The project structure is organized as follows:
├── product-command-service
│   ├── src
│   ├── ...
├── product-query-service
│   ├── src
│   ├── ...
- 
Command Side - product-command-service:- When a client sends a POST request to create a new product or a PUT request to update an existing product, the product-command-serviceprocesses the request and generates an event, such asCreateProductorUpdateProduct, and publishes it to the Kafka topic dedicated to these events.
 
- When a client sends a POST request to create a new product or a PUT request to update an existing product, the 
- 
Kafka Integration: - Kafka acts as the central messaging service for this CQRS application.
- It maintains topics for events like CreateProductandUpdateProduct.
- The product-query-servicelistens to these Kafka topics to consume the events.
 
- 
Query Side - product-query-service:- The product-query-servicesubscribes to the Kafka topics where events are published.
- When it receives an event, it updates its own database to ensure data consistency between the query and command sides.
 
- The 
- 
Client Requests: - Clients can send GET requests to the product-query-serviceto retrieve product information. These requests are read-only operations.
- The product-query-serviceresponds to these requests with data from its synchronized database.
 
- Clients can send GET requests to the 
To run the project locally, follow these steps:
- 
Prerequisites: - Ensure you have Java and Apache Kafka installed on your system.
 
- 
Clone the Repository: git clone https://github.com/Ninjavin/CQRS-Design-Pattern.git 
- 
Build and Run Services: - Navigate to the product-query-serviceandproduct-command-servicedirectories.
- Build and run each service using Maven or your preferred IDE.
 
- Navigate to the 
- 
Configuration: - Configure Kafka properties in the application.propertiesfiles of both services to specify the Kafka broker address.
 
- Configure Kafka properties in the 
- 
Test the Application: - Send GET requests to product-query-servicefor read operations.
- Use POST and PUT requests to product-command-serviceto create and update products.
- Observe how the services interact via Kafka to maintain database consistency.
 
- Send GET requests to 
This project heavily relies on Kafka for event-driven communication. Ensure you have Kafka set up with the following components:
- 
Kafka Topics: Create Kafka topics for events like CreateProductandUpdateProduct. Configure these topics in both services.
- 
Kafka Brokers: Update Kafka broker properties in the application.propertiesfiles of both services to connect to your Kafka cluster.