Bird
Raised Fist0
Kafkadevops~10 mins

CQRS pattern in Kafka - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to produce a command message to Kafka.

Kafka
producer.send(new ProducerRecord<>("[1]", "CreateOrder", orderData));
Drag options to blanks, or click blank then click option'
Alogs
Bevents
Cqueries
Dcommands
Attempts:
3 left
💡 Hint
Common Mistakes
Using the events topic instead of commands.
Using queries topic for commands.
2fill in blank
medium

Complete the code to consume events from Kafka for updating read models.

Kafka
consumer.subscribe(Collections.singletonList("[1]"));
Drag options to blanks, or click blank then click option'
Aevents
Blogs
Cqueries
Dcommands
Attempts:
3 left
💡 Hint
Common Mistakes
Subscribing to commands topic instead of events.
Using queries topic for event consumption.
3fill in blank
hard

Fix the error in the Kafka consumer group id for event processing.

Kafka
props.put("group.id", "[1]");
Drag options to blanks, or click blank then click option'
Aevent-processors
Bquery-handlers
Ccommand-processors
Dlog-consumers
Attempts:
3 left
💡 Hint
Common Mistakes
Using command-processors group id for event consumers.
Using query-handlers group id for event consumers.
4fill in blank
hard

Fill both blanks to create a Kafka Streams topology that processes commands and produces events.

Kafka
KStream<String, String> commands = builder.stream("[1]");
KStream<String, String> events = commands.mapValues(command -> [2]);
events.to("events");
Drag options to blanks, or click blank then click option'
Acommands
Bevents
CprocessCommand(command)
DhandleEvent(command)
Attempts:
3 left
💡 Hint
Common Mistakes
Using events as input stream instead of commands.
Using handleEvent instead of processCommand for mapping.
5fill in blank
hard

Fill all three blanks to implement a Kafka Streams processor that filters commands, maps them to events, and writes to the events topic.

Kafka
KStream<String, String> commands = builder.stream("[1]");
KStream<String, String> filtered = commands.filter((key, value) -> value.contains("[2]"));
filtered.mapValues(value -> [3]).to("events");
Drag options to blanks, or click blank then click option'
Acommands
BCreateOrder
CprocessCommand(value)
Devents
Attempts:
3 left
💡 Hint
Common Mistakes
Filtering on wrong keyword.
Mapping with incorrect function.

Practice

(1/5)
1. What is the main idea behind the CQRS pattern in Kafka-based systems?
easy
A. Separating commands (writes) and queries (reads) into different parts
B. Using a single topic for both commands and queries
C. Storing all data in one database without separation
D. Ignoring events and focusing only on commands

Solution

  1. Step 1: Understand CQRS basics

    CQRS stands for Command Query Responsibility Segregation, which means separating write and read operations.
  2. Step 2: Apply to Kafka context

    In Kafka, commands and queries are handled separately, often using different topics for clarity and scalability.
  3. Final Answer:

    Separating commands (writes) and queries (reads) into different parts -> Option A
  4. Quick Check:

    CQRS = Separate commands and queries [OK]
Hint: Remember CQRS splits writes and reads separately [OK]
Common Mistakes:
  • Thinking commands and queries share the same topic
  • Confusing CQRS with event sourcing
  • Assuming CQRS means no events are used
2. Which Kafka topic setup correctly follows the CQRS pattern?
easy
A. One topic for all commands, events, and queries
B. One topic for commands and a separate topic for events
C. Multiple topics for queries only
D. No topics, just direct database writes

Solution

  1. Step 1: Recall CQRS topic separation

    CQRS uses separate topics for commands (writes) and events (results of commands) to keep communication clear.
  2. Step 2: Evaluate options

    One topic for commands and a separate topic for events correctly separates commands and events into different topics, matching CQRS principles.
  3. Final Answer:

    One topic for commands and a separate topic for events -> Option B
  4. Quick Check:

    Separate topics for commands and events = One topic for commands and a separate topic for events [OK]
Hint: Separate commands and events into different topics [OK]
Common Mistakes:
  • Mixing commands and queries in one topic
  • Ignoring event topics
  • Using no Kafka topics at all
3. Given this Kafka setup for CQRS:
commands_topic = ['CreateOrder', 'UpdateOrder']
events_topic = ['OrderCreated', 'OrderUpdated']

Which topic should a read service subscribe to for query data?
medium
A. commands_topic
B. Both commands_topic and events_topic
C. events_topic
D. Neither, it reads directly from the database

Solution

  1. Step 1: Identify read service role

    Read services in CQRS listen to events that represent state changes, not commands which are requests.
  2. Step 2: Match topic to read service

    Events_topic contains 'OrderCreated' and 'OrderUpdated' which reflect changes; read service subscribes here.
  3. Final Answer:

    events_topic -> Option C
  4. Quick Check:

    Read service subscribes to events_topic [OK]
Hint: Reads listen to events, not commands [OK]
Common Mistakes:
  • Subscribing read service to commands_topic
  • Subscribing to both topics unnecessarily
  • Assuming reads bypass Kafka
4. You have this Kafka CQRS code snippet:
producer.send('commands', 'CreateOrder')
consumer.subscribe(['commands', 'events'])

What is the main problem here?
medium
A. Consumer should not subscribe to both 'commands' and 'events' topics
B. Producer should send to 'events' topic, not 'commands'
C. Producer must send JSON, not strings
D. Consumer must subscribe only to 'commands' topic

Solution

  1. Step 1: Understand CQRS topic roles

    Commands topic is for writes; events topic is for read updates. Consumers usually subscribe to one role.
  2. Step 2: Analyze consumer subscription

    Subscribing to both commands and events mixes responsibilities and can cause confusion or errors.
  3. Final Answer:

    Consumer should not subscribe to both 'commands' and 'events' topics -> Option A
  4. Quick Check:

    Consumer subscribes to one topic type only [OK]
Hint: Consumers should subscribe to either commands or events, not both [OK]
Common Mistakes:
  • Thinking producer must send events on commands topic
  • Assuming consumer can handle both topics safely
  • Ignoring message format requirements
5. How can you design a Kafka-based CQRS system to handle high read traffic without slowing down writes?
hard
A. Use only one consumer group for both commands and events
B. Combine commands and events in one topic to reduce overhead
C. Write all data directly to the database and skip Kafka for reads
D. Use separate Kafka topics for commands and events, and scale read consumers independently

Solution

  1. Step 1: Apply CQRS scalability principle

    Separating commands and events allows independent scaling of write and read sides.
  2. Step 2: Design Kafka topics and consumers

    Using separate topics for commands and events lets read consumers scale without affecting write performance.
  3. Final Answer:

    Use separate Kafka topics for commands and events, and scale read consumers independently -> Option D
  4. Quick Check:

    Separate topics and scale reads independently [OK]
Hint: Separate topics and scale read consumers for high traffic [OK]
Common Mistakes:
  • Combining commands and events causing bottlenecks
  • Skipping Kafka for reads losing scalability
  • Using one consumer group for all messages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard