Hello! Let's dive back into our study plan for the AWS Data Engineer - Associate certification.
In our last lesson, we established a strong security foundation for our MSK cluster by configuring encryption, network isolation with security groups, and client access control using IAM. With the "house" now secure, it's time to build the applications that will live in it and interact with our data streams.
Today, we will focus on designing the core components of any Kafka-based system: producer and consumer applications that interact with an MSK cluster. You'll learn how to create applications that can send data to and read data from your Kafka topics, applying the security principles we've already covered. We'll look at several common patterns, from simple command-line tools for testing to more robust, programmatic clients using Python with AWS Lambda—a pattern frequently seen in data engineering and on the certification exam.
1. The Fundamental Roles: Producers and Consumers
At its heart, Apache Kafka is a publish-subscribe messaging system. The applications that interact with it fall into two primary categories:
- Producers: Applications that write (or publish) streams of records to one or more Kafka topics.
- Consumers: Applications that read (or subscribe to) streams of records from one or more Kafka topics.
A single Kafka topic can have many producers writing to it and many consumers reading from it, which makes it a powerful tool for decoupling different parts of a complex data architecture.
To get a quick visual and conceptual refresher on these roles, let's start with a brief video segment.
AWS MSK Tutorial for Beginners | Kafka on AWS | Learn everything about AWS MSK | Hands- On Tutorial
The following clip from the 'AWS MSK Tutorial for Beginners' by AWS Made Easy offers a clear, high-level explanation of producers, consumers, and their relationship to Kafka topics.
Watch from 00:50 to 02:58. Pay attention to the e-commerce example, which illustrates how a single event (an order) can be published by one producer and consumed by multiple independent applications (for payment, for logistics, etc.).
2. A Practical Starting Point: Console Clients on EC2
The most straightforward way to test connectivity and interact with an MSK cluster is by using the standard command-line tools that come with Apache Kafka. This approach is invaluable for development and troubleshooting.
We will walk through the process of setting up a client machine (an EC2 instance) and using these tools to produce and consume messages. This exercise will directly apply the concepts from our previous lesson on security.
The following video provides a comprehensive, step-by-step demonstration of this entire process. As you watch, focus on how each step contributes to establishing a secure and functional connection between the client and the cluster.
AWS MSK Tutorial for Beginners | Kafka on AWS | Learn everything about AWS MSK | Hands- On Tutorial
This extended segment from 'AWS MSK Tutorial for Beginners' is a complete hands-on lab. It shows how to provision a client, configure it for IAM authentication, and use the console producer/consumer to interact with an MSK cluster.
Watch from 09:37 to 29:52. This is a longer segment, but it covers the complete end-to-end process. Follow along with the key phases: Client Provisioning (09:37 - 14:34): An EC2 instance is launched in the same VPC as the MSK cluster. This is the first requirement for connectivity. Client Software Setup (14:34 - 20:08): Java, Kafka, and the AWS MSK IAM Auth JAR are installed. The client.properties file is configured to use AWS_MSK_IAM, directly applying what we learned in the last lesson. Permissions and Networking (20:08 - 26:00): An IAM role with kafka-cluster:* permissions is created and attached to the EC2 instance. Critically, the security groups are configured to allow traffic between the EC2 client's security group and the MSK cluster's security group. Interaction (26:00 - 29:52): A topic is created using kafka-topics.sh. Then, you see the kafka-console-producer.sh and kafka-console-consumer.sh scripts running in two separate terminals, demonstrating real-time message flow.
This manual, hands-on process solidifies your understanding of all the components that must work together: VPC networking, security groups, IAM roles and policies, and client-side configuration. If any one of these is misconfigured, the connection will fail.
3. Architectural Patterns for Programmatic Clients
While console clients are great for testing, real-world data pipelines use programmatic clients. The way you architect these clients depends heavily on the use case. Here are two of the most common patterns you'll encounter as a data engineer.


For the certification, you should be familiar with both running clients on compute instances (EC2, Fargate) and using serverless services (Lambda, Glue). Let's focus on designing a serverless Python producer with AWS Lambda.
4. Designing a Serverless Producer with AWS Lambda
Using AWS Lambda as a Kafka producer is an efficient, scalable, and cost-effective pattern for event-driven data ingestion.
The video below demonstrates how to build a producer in Python and deploy it as a Lambda function. Note: The video uses unauthenticated access for simplicity. In our context, you would adapt this by including the IAM authentication parameters in your Python code, just as they were set in the client.properties file in the previous example.
Creating a Serverless Apache Kafka(MSK) publisher using AWS Lambda
The video 'Creating a Serverless Apache Kafka publisher using AWS Lambda' by Knowledge Amplifier provides an excellent walkthrough of building a Python-based producer and running it in Lambda.
Watch the following segments, focusing on the technical implementation: Programmatic Producer (13:34 - 16:35): The presenter writes a Python script using the kafka-python library. Observe how the KafkaProducer is instantiated with the bootstrap server addresses. To adapt this for IAM authentication, you would add parameters like security_protocol='SASL_SSL', sasl_mechanism='AWS_MSK_IAM'. Packaging for Lambda (16:35 - 21:17): Since the kafka-python library isn't included in the Lambda runtime by default, it must be packaged as a Lambda Layer. This is a crucial concept for deploying custom dependencies. Lambda Configuration (21:17 - 27:24): The Lambda function is configured to run inside the VPC, attached to the correct subnets and security group to allow it to communicate with the MSK brokers. The custom layer is attached. This demonstrates the critical networking and configuration steps for serverless resources.
This pattern is powerful because the producer code only runs (and incurs cost) when it's triggered, making it ideal for ingesting data from intermittent sources.
5. Best Practices for Production-Ready Clients
Simply connecting is not enough for production systems. Your producer and consumer applications must be designed for reliability and performance. The AWS documentation provides key best practices centered around client configuration parameters.
For the exam, you won't need to memorize every parameter, but you should understand the purpose of the most important ones for tuning availability and performance.
Best practices for Apache Kafka clients
The official AWS documentation on 'Best practices for Apache Kafka clients' is an essential resource for building robust applications. We will focus on the key parameters for availability and performance.
Read the sections 'Apache Kafka client availability' and 'Apache Kafka client performance'. As you read, focus on understanding the goal of these key parameters: For Producer Availability & Durability: acks=all: Ensures the message is written to all in-sync replica brokers before being considered 'committed'. This prevents data loss if a broker fails. retries: How many times the producer should re-send a message that failed. Set this high to handle transient network issues or broker failovers. For Producer Performance: linger.ms: The time the producer will wait to collect more records into a single batch before sending. A higher value (e.g., 25ms) increases latency slightly but improves throughput by sending fewer, larger requests. batch.size: The maximum size of a batch of records. Larger batches are more efficient. For Consumer Availability: session.timeout.ms: If a consumer doesn't send a heartbeat within this timeout, the cluster considers it dead and triggers a rebalance. This should be long enough to handle application restarts or temporary freezes. For Consumer Performance: fetch.min.bytes: The minimum amount of data the broker should return for a fetch request. This prevents the consumer from making many small, inefficient requests for low-volume topics.
Test your understanding!
You are designing a producer application that publishes critical financial transactions. The highest priority is to prevent any data loss, even if a broker temporarily fails. Which producer configuration parameter is most important to set, and what should its value be?
Show answer
The most important parameter is acks (acknowledgements). To ensure maximum durability and prevent data loss, you should set acks=all.
This configuration means the lead broker will wait until all in-sync replicas have successfully received the record before sending an acknowledgement back to the producer. This guarantees that the message is replicated and will not be lost even if the lead broker fails immediately after sending the acknowledgement.
Conclusion
In this lesson, you've moved from theory to practice, learning how to design the applications that form the backbone of a streaming data pipeline with Amazon MSK.
Key Takeaways:
- Producers write data to Kafka topics, and consumers read data from them.
- Client applications can be implemented in various ways: simple console tools for testing, long-running services in containers (Fargate) or on VMs (EC2), and event-driven serverless functions (Lambda).
- Regardless of the implementation, every client needs two things to connect to a secure MSK cluster: correct network configuration (VPC, Security Groups) and proper client properties for authentication (e.g., SASL/IAM).
- For production workloads, you must tune client parameters to optimize for availability (
acks,retries) and performance (linger.ms,batch.size).
You now know how to build your own custom applications to get data in and out of MSK. In our next lesson, we'll explore a powerful alternative: "Integrate external systems with MSK using MSK Connect." MSK Connect provides a way to stream data from common sources (like databases) into MSK and from MSK to common sinks (like S3 or OpenSearch) using pre-built connectors, often without writing any code.