Bird
Raised Fist0
Kafkadevops~10 mins

Message broker architecture 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 create a Kafka producer.

Kafka
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers=[1])
Drag options to blanks, or click blank then click option'
A'localhost:9092'
B'127.0.0.1:2181'
C'kafka:2181'
D'localhost:2181'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Zookeeper port 2181 instead of Kafka broker port 9092.
2fill in blank
medium

Complete the code to send a message to a Kafka topic.

Kafka
producer.send([1], b'Hello Kafka')
Drag options to blanks, or click blank then click option'
A'default'
B'topic1'
C'kafka_topic'
D'my_topic'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an incorrect or non-existent topic name.
3fill in blank
hard

Fix the error in the consumer code to subscribe to a topic.

Kafka
from kafka import KafkaConsumer
consumer = KafkaConsumer([1], bootstrap_servers='localhost:9092')
Drag options to blanks, or click blank then click option'
A'topic1'
B['my_topic']
C'my_topic'
D['topic1']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a list of strings for topics.
4fill in blank
hard

Complete the code to create a dictionary comprehension that maps partition numbers to their offsets.

Kafka
offsets = {partition: consumer.position(partition) [1] partition in consumer.assignment()}
Drag options to blanks, or click blank then click option'
A:
Bfor
Cin
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' for key-value separation.
Using 'in' instead of 'for' in comprehension.
5fill in blank
hard

Fill both blanks to create a dictionary comprehension filtering partitions with offsets greater than 100.

Kafka
filtered_offsets = {partition: offset [1] partition, offset [2] offsets.items() if offset > 100}
Drag options to blanks, or click blank then click option'
A:
Bfor
Cin
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' for key-value.
Using 'in' instead of 'for' or vice versa.

Practice

(1/5)
1. What is the main role of a message broker like Kafka in an application?
easy
A. To create user interfaces for applications
B. To send messages between different parts of an application reliably
C. To run complex calculations on data
D. To store large files permanently

Solution

  1. Step 1: Understand the purpose of a message broker

    A message broker helps different parts of an app communicate by sending messages.
  2. Step 2: Identify Kafka's role

    Kafka acts as a reliable middleman to send and store messages in order.
  3. Final Answer:

    To send messages between different parts of an application reliably -> Option B
  4. Quick Check:

    Message broker = reliable message passing [OK]
Hint: Message brokers connect apps by passing messages [OK]
Common Mistakes:
  • Thinking Kafka stores files permanently
  • Confusing message brokers with UI tools
  • Assuming Kafka does data calculations
2. Which of the following is the correct way to describe a Kafka topic?
easy
A. A user who sends messages to Kafka
B. A tool to monitor Kafka servers
C. A place where messages are stored and organized in order
D. A database table for storing user data

Solution

  1. Step 1: Define what a Kafka topic is

    A Kafka topic is a category or feed name where messages are stored in order.
  2. Step 2: Match the correct description

    Only A place where messages are stored and organized in order correctly describes a topic as a storage place for ordered messages.
  3. Final Answer:

    A place where messages are stored and organized in order -> Option C
  4. Quick Check:

    Kafka topic = ordered message storage [OK]
Hint: Topic = ordered message storage in Kafka [OK]
Common Mistakes:
  • Confusing topic with user or tool
  • Thinking topic is a database table
  • Mixing topic with monitoring tools
3. Given this Kafka setup: Producer sends messages to topic orders. Consumer reads from orders. What will the consumer receive if the producer sends messages ["order1", "order2"] in order?
medium
A. ["order1", "order2"]
B. No messages because consumer reads from a different topic
C. ["order2", "order1"]
D. An error because messages are unordered

Solution

  1. Step 1: Understand message order in Kafka topics

    Kafka keeps messages in the order they are sent within a topic.
  2. Step 2: Trace the messages sent and received

    Producer sends ["order1", "order2"] to topic orders. Consumer reads from the same topic, so it receives messages in the same order.
  3. Final Answer:

    ["order1", "order2"] -> Option A
  4. Quick Check:

    Kafka preserves message order = ["order1", "order2"] [OK]
Hint: Kafka keeps message order per topic [OK]
Common Mistakes:
  • Assuming messages arrive reversed
  • Thinking consumer reads from different topic
  • Believing Kafka does not guarantee order
4. You wrote code where a Kafka consumer tries to read from a topic but gets no messages. What is a likely cause?
medium
A. The consumer is subscribed to the wrong topic name
B. Kafka does not support consumers
C. Messages are deleted immediately after sending
D. Producer sends messages in the wrong format

Solution

  1. Step 1: Check consumer subscription

    If the consumer subscribes to a wrong or misspelled topic, it won't receive messages.
  2. Step 2: Evaluate other options

    Kafka supports consumers, messages are not deleted immediately, and format issues usually cause errors, not empty reads.
  3. Final Answer:

    The consumer is subscribed to the wrong topic name -> Option A
  4. Quick Check:

    Wrong topic subscription = no messages [OK]
Hint: Check topic name spelling for consumer subscription [OK]
Common Mistakes:
  • Assuming Kafka lacks consumer support
  • Thinking messages vanish instantly
  • Blaming message format without error
5. You want to ensure messages sent to Kafka are not lost even if a server crashes. Which Kafka feature helps achieve this?
hard
A. Sending messages without keys
B. Using a single broker to simplify management
C. Deleting messages immediately after consumption
D. Replication of topic partitions across multiple brokers

Solution

  1. Step 1: Understand message durability in Kafka

    Kafka replicates partitions of topics across multiple brokers to avoid data loss if one fails.
  2. Step 2: Evaluate options for reliability

    Single broker risks data loss, deleting messages early loses data, and keys don't affect durability.
  3. Final Answer:

    Replication of topic partitions across multiple brokers -> Option D
  4. Quick Check:

    Replication = message safety on server crash [OK]
Hint: Use replication to keep messages safe [OK]
Common Mistakes:
  • Thinking single broker is safer
  • Deleting messages early to save space
  • Ignoring replication importance
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