0
0
Kafkadevops~30 mins

CQRS pattern in Kafka - Mini Project: Build & Apply

Choose your learning style9 modes available
Implementing CQRS Pattern with Kafka
📖 Scenario: You are building a simple order management system. You want to separate the commands (actions that change data) from queries (actions that read data) using the CQRS pattern. Kafka will be used to handle the communication between the command side and the query side.
🎯 Goal: Build a basic CQRS setup where commands to create orders are sent to a Kafka topic, and the query side consumes these commands to update a read model.
📋 What You'll Learn
Create a Kafka topic named orders-commands to receive order creation commands.
Create a Kafka topic named orders-queries to send updated order read models.
Produce a command message to orders-commands with order details.
Consume the command message from orders-commands and update the read model.
Produce the updated read model message to orders-queries.
Print the final read model message.
💡 Why This Matters
🌍 Real World
CQRS pattern is used in systems where separating write and read operations improves scalability and performance. Kafka helps by providing a reliable messaging backbone.
💼 Career
Understanding CQRS with Kafka is valuable for roles in backend development, system architecture, and DevOps where event-driven and scalable systems are built.
Progress0 / 4 steps
1
Create Kafka topics for commands and queries
Create two Kafka topics named orders-commands and orders-queries using the Kafka CLI commands exactly as shown.
Kafka
Need a hint?

Use kafka-topics --create command with --topic, --bootstrap-server, --partitions, and --replication-factor options.

2
Produce an order creation command message
Use the Kafka console producer to send a JSON message with order details {"orderId": "123", "product": "Book", "quantity": 2} to the orders-commands topic.
Kafka
Need a hint?

Use kafka-console-producer with --topic orders-commands and send the JSON message.

3
Consume command and produce updated read model
Consume the message from orders-commands topic using Kafka console consumer, then produce an updated read model JSON message {"orderId": "123", "status": "Created", "product": "Book", "quantity": 2} to the orders-queries topic using Kafka console producer.
Kafka
Need a hint?

Use kafka-console-consumer to read one message from orders-commands, then use kafka-console-producer to send the updated read model JSON to orders-queries.

4
Display the updated read model message
Consume the updated read model message from the orders-queries topic and print it to the console.
Kafka
Need a hint?

Use kafka-console-consumer with --topic orders-queries and --max-messages 1 to print the updated read model.