Bird
Raised Fist0
Kafkadevops~5 mins

Message broker architecture in Kafka - Commands & Configuration

Choose your learning style10 modes available
Introduction
A message broker helps different parts of a system talk to each other by sending messages. It makes sure messages get delivered safely and in order, even if parts of the system are busy or offline.
When you want to connect different apps or services that run separately and need to share data.
When you want to handle lots of messages quickly without losing any.
When you want to keep messages safe even if one part of your system crashes.
When you want to process messages in order or group them by topic.
When you want to scale your system easily by adding more message handlers.
Config File - server.properties
server.properties
broker.id=1
listeners=PLAINTEXT://:9092
log.dirs=/tmp/kafka-logs
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=localhost:2181
zookeeper.connection.timeout.ms=6000

broker.id: Unique ID for this Kafka broker.

listeners: Network address where Kafka listens for connections.

log.dirs: Folder where Kafka stores message logs.

num.network.threads and num.io.threads: Threads for handling network and disk I/O.

log.retention.hours: How long Kafka keeps messages before deleting.

zookeeper.connect: Address of ZooKeeper which manages Kafka cluster metadata.

Commands
Start ZooKeeper, which Kafka uses to keep track of brokers and topics.
Terminal
bin/zookeeper-server-start.sh config/zookeeper.properties
Expected OutputExpected
[2024-06-01 12:00:00,000] INFO Starting zookeeper server ... [2024-06-01 12:00:01,000] INFO Binding to port 2181 [2024-06-01 12:00:02,000] INFO Started serving clients
Start the Kafka broker using the configuration file to accept and store messages.
Terminal
bin/kafka-server-start.sh config/server.properties
Expected OutputExpected
[2024-06-01 12:00:05,000] INFO Kafka server started (kafka.server.KafkaServer) [2024-06-01 12:00:06,000] INFO Awaiting client connections on port 9092
Create a topic named 'example-topic' with 3 partitions to organize messages and allow parallel processing.
Terminal
bin/kafka-topics.sh --create --topic example-topic --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1
Expected OutputExpected
Created topic example-topic.
→
--partitions - Number of partitions to split the topic into for parallelism
→
--replication-factor - Number of copies of data for fault tolerance
List all topics available on the Kafka broker to verify the topic creation.
Terminal
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
Expected OutputExpected
example-topic
Key Concept

If you remember nothing else from this pattern, remember: a message broker safely stores and forwards messages between parts of a system, organizing them by topics and partitions for reliability and speed.

Common Mistakes
Starting Kafka broker before starting ZooKeeper
Kafka depends on ZooKeeper to manage cluster metadata, so it won't start properly without ZooKeeper running.
Always start ZooKeeper first, then start the Kafka broker.
Creating a topic without specifying partitions
Without partitions, the topic cannot handle parallel processing or scale well.
Specify the number of partitions when creating a topic to allow better performance.
Using replication-factor higher than the number of brokers
Kafka cannot create more copies than brokers available, causing topic creation to fail.
Set replication-factor equal or less than the number of running brokers.
Summary
Start ZooKeeper first to manage Kafka cluster metadata.
Start Kafka broker with a proper configuration file to accept messages.
Create topics with partitions and replication for organizing and protecting messages.
List topics to verify they exist and are ready for use.

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