0
0
Kafkadevops~10 mins

Amazon MSK in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Amazon MSK
Create MSK Cluster
Configure Topics
Produce Messages
MSK Brokers Receive
Store Messages in Partitions
Consume Messages
Process Data
End
This flow shows how Amazon MSK manages Kafka clusters: create cluster, configure topics, produce messages, store them, and consume for processing.
Execution Sample
Kafka
from kafka import KafkaProducer, KafkaConsumer

producer = KafkaProducer(bootstrap_servers='b-1.msk-cluster:9092')
producer.send('test-topic', b'Hello MSK')
producer.flush()

consumer = KafkaConsumer('test-topic', bootstrap_servers='b-1.msk-cluster:9092', value_deserializer=lambda m: m.decode('utf-8'))
for msg in consumer:
    print(msg.value)
This code sends a message to an MSK topic and then reads messages from it.
Process Table
StepActionEvaluationResult
1Create KafkaProducer with MSK bootstrap serverConnect to MSK brokersProducer ready to send messages
2Send message 'Hello MSK' to 'test-topic'Message serialized and sentMessage queued in MSK topic partition
3Create KafkaConsumer for 'test-topic'Connect to MSK brokersConsumer ready to receive messages
4Consumer polls messagesReceives 'Hello MSK' messageMessage available for processing
5Print message valueDecode bytes to stringOutput: Hello MSK
6No more messagesConsumer waits or exitsEnd of execution
💡 No more messages to consume, consumer stops polling
Status Tracker
VariableStartAfter Step 2After Step 4Final
producerNoneKafkaProducer instance connectedKafkaProducer instance connectedKafkaProducer instance connected
consumerNoneNoneKafkaConsumer instance connectedKafkaConsumer instance connected
message_sentNoneb'Hello MSK'b'Hello MSK'b'Hello MSK'
message_receivedNoneNoneHello MSKHello MSK
Key Moments - 3 Insights
Why does the consumer receive the message after the producer sends it?
Because the producer sends the message to the MSK topic partition, which stores it until the consumer polls and receives it, as shown in steps 2 and 4 of the execution_table.
What happens if the consumer tries to read before the producer sends a message?
The consumer will wait or poll repeatedly but receive no messages until the producer sends one, so no output appears until step 4 in the execution_table.
Why do we need to specify bootstrap_servers in both producer and consumer?
Because both need to connect to the MSK cluster brokers to send or receive messages, as shown in steps 1 and 3 where connections are established.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output printed at step 5?
Ab'Hello MSK'
BHello MSK
Ctest-topic
DNo output
💡 Hint
Check the 'Result' column at step 5 in the execution_table.
At which step does the consumer connect to the MSK brokers?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Evaluation' columns for consumer creation in the execution_table.
If the producer did not send any message, what would the consumer receive at step 4?
AThe last message sent
BAn error
CNo messages
DA default message
💡 Hint
Refer to the key_moments explanation about consumer behavior before messages are sent.
Concept Snapshot
Amazon MSK manages Kafka clusters in the cloud.
Create clusters, configure topics, produce and consume messages.
Producer sends messages to topic partitions.
Consumer reads messages from topics.
Both connect via bootstrap servers.
MSK handles storage and delivery reliably.
Full Transcript
Amazon MSK is a managed Kafka service that lets you create Kafka clusters easily. You configure topics where messages are sent and received. A producer connects to the MSK brokers and sends messages to a topic. These messages are stored in partitions inside the cluster. A consumer connects to the same brokers and polls messages from the topic partitions. When the consumer receives a message, it can process or print it. This flow ensures reliable message delivery and easy scaling. Both producer and consumer need to know the bootstrap servers to connect to the cluster. If no messages are sent, the consumer waits without output. This example shows sending 'Hello MSK' and receiving it back.