Kafka & Streaming
Apache Kafka: The Backbone of Real-Time Data Streaming
In today’s data-driven world, massive amounts of data are generated every second and need to be processed efficiently. This is where Apache…
In today’s data-driven world, massive amounts of data are generated every second and need to be processed efficiently. This is where Apache Kafka comes in. It acts as a distributed event/message queue system, ensuring that data is not lost and that it is processed in the exact order it was received. Kafka is a vital tool for handling streams of real-time data.
What is Kafka?
Apache Kafka is an open-source distributed streaming system used for stream processing, real-time data pipelines, and data integration at scale. Originally created to handle real-time data feeds at LinkedIn in 2011, Kafka quickly evolved from a messaging queue to a full-fledged event streaming platform, capable of handling over one million messages per second, or trillions of messages per day.
The main purpose of Kafka is to handle real-time data by providing a reliable, scalable, and fault-tolerant system for transmitting and storing streams of events.
Key Idea behind working of Kafka : it stores streams of records (messages) in topics, which can be consumed by multiple consumers.
Architecture of Kafka:
Kafka is built as a distributed, fault-tolerant, high-throughput messaging system. Its architecture revolves around a few core components:
1. Producer
- Applications that send data/messages to Kafka topics.
- Can choose which partition to send a message to.
2. Consumer
- Applications that read data/messages from Kafka topics.
- Can work individually or as part of a consumer group to share workload.
3. Topic
- Logical channel where messages are published.
- Each topic is divided into partitions for parallel processing.
4. Partition
- A topic is split into partitions to allow scalability and parallelism.
- Messages within a partition are strictly ordered.
5. Broker
- A Kafka server that stores messages and serves producers and consumers.
- Kafka clusters have multiple brokers for fault tolerance and load balancing.
6. Cluster
- A collection of brokers working together to store data and handle requests.
- Data is replicated across brokers to prevent loss.
7. ZooKeeper / KRaft Mode
- ZooKeeper (older versions) manages cluster metadata and leader election.
- Newer Kafka versions use KRaft mode, eliminating the need for ZooKeeper.
Data Flow in Kafka
- Producers send messages to a topic.
- The topic stores messages in partitions across brokers.
- Consumers read messages from partitions, either individually or via a consumer group.
- Messages are retained based on time or size, allowing reprocessing if needed.

Why Kafka is Popular
- High throughput — millions of messages per second.
- Scalable — add brokers to handle more data.
- Durable and fault-tolerant — replicates data across brokers.
- Real-time processing — integrates with Spark, Flink, and Storm.
Kafka Use Cases
- Log aggregation — centralizing logs from multiple services.
- Event sourcing — storing state changes as an ordered stream of events.
- Real-time analytics — tracking user activity in real-time.
- Data pipelines — moving data between systems reliably.
Installation of Kafka
Here’s a clear guide for installing Apache Kafka on both Mac and Windows, suitable for your Medium post. I’ll cover the latest stable version setup using Kafka binaries and Homebrew (Mac).
Installing Kafka on Mac
1. Install Homebrew (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"2. Install Kafka using Homebrew
brew install kafka3. Start ZooKeeper (if using Kafka < 3.3)
zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties✅ Optional tip: For modern versions (3.3+), you can run Kafka in KRaft mode:
kafka-server-start /usr/local/etc/kafka/kraft/server.propertiesNo ZooKeeper needed.
4. Start Kafka Server
kafka-server-start /usr/local/etc/kafka/server.properties5. Create a Topic
kafka-topics --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 16. Test Producer and Consumer
# Start Producer
kafka-console-producer --topic test-topic --bootstrap-server localhost:9092# Start Consumer
kafka-console-consumer --topic test-topic --from-beginning --bootstrap-server localhost:9092
Installing Kafka on Windows
1. Download Kafka
- Go to Kafka Downloads on google.
- Choose binary download for the latest version.
- Extract the zip file to
C:\kafka
2. Start ZooKeeper
Open Command Prompt in C:\kafka\bin\windows and run:
zookeeper-server-start.bat ..\..\config\zookeeper.properties3. Start Kafka Server
In a new Command Prompt window:
kafka-server-start.bat ..\..\config\server.properties4. Create a Topic
kafka-topics.bat --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 15. Test Producer and Consumer
# Producer
kafka-console-producer.bat --topic test-topic --bootstrap-server localhost:9092# Consumer
kafka-console-consumer.bat --topic test-topic --from-beginning --bootstrap-server localhost:9092
Using a Managed Kafka Service (like Aiven)
If you don’t want to install Kafka locally, you can use Aiven or other managed Kafka services (Confluent Cloud, AWS MSK) to host Kafka in the cloud.
Steps:
- Sign up on Aiven and create a Kafka service.
- Note the connection details:
bootstrap servers, username, password, and security settings. - Connect to Kafka from your IDE (Python, Java, or other languages) using Kafka client libraries.
Example (Python with kafka-python):
from kafka import KafkaProducer
producer = KafkaProducer(
bootstrap_servers='your_bootstrap_servers_here',
security_protocol='SSL',
ssl_cafile='path_to_ca.pem',
ssl_certfile='path_to_cert.pem',
ssl_keyfile='path_to_key.pem'
)
producer.send('test-topic', b'Hello from Aiven Kafka!')
producer.flush()Benefits of using Aiven or managed Kafka:
- No need to install Kafka/ZooKeeper locally.
- Secure, scalable, and production-ready setup.
- Ideal for experimenting or learning Kafka in a real cloud environment.
Optimizing Apache Kafka for Performance
Apache Kafka is designed for high throughput and real-time data processing, but proper configuration is key to getting the best performance. Here are the main areas to focus on:
1. Topic and Partition Tuning
- Partitions: Increase the number of partitions to improve parallelism and throughput. However, too many partitions can add overhead, so balance based on cluster size and workload.
- Replication Factor: Use at least 2–3 replicas per topic for fault tolerance and high availability.
2. Producer Optimization
- Batching: Enable batching (
batch.size) to send multiple messages together, reducing network overhead. - Compression: Use compression (
snappy,gzip,lz4) to save bandwidth and improve throughput. - Acknowledgments: Set
acks=allto ensure data durability; this slightly impacts latency but guarantees reliability.
3. Consumer Optimization
- Consumer Groups: Use multiple consumers in a group to parallelize message processing.
- Fetch Size: Tune
fetch.min.bytesandfetch.max.wait.msto balance latency and throughput. - Offset Management: Use
enable.auto.commit=falsefor precise control over message acknowledgment.
4. Broker Optimization
- Memory & Disk: Allocate sufficient memory and use SSDs for log storage.
- Log Segments: Adjust
log.segment.bytesandlog.retention.msfor efficient storage and faster reads. - Replication Traffic: Ensure replication traffic does not saturate the network.
5. Network and OS Tuning
- File Descriptors: Increase OS limits for open files (
ulimit -n). - Socket Buffers: Tune network socket buffers for high-throughput traffic.
- Thread Pools: Configure I/O and background threads based on workload and cluster size.
6. Monitoring & Scaling
- Keep track of critical metrics:
- Broker CPU, memory, and disk usage
- Under-replicated partitions
- Consumer lag
- Scale horizontally by adding more brokers to handle higher throughput and ensure fault tolerance.
7. Managed Kafka Services
For beginners or production workloads, using managed services like Aiven Kafka, Confluent Cloud, or AWS MSK can simplify optimization. They handle scaling, replication, monitoring, and network tuning automatically, letting you focus on building applications.
Real-World Example
“For instance, Netflix uses Kafka to track user activity in real-time. Every click, search, or watch action is sent as an event to Kafka topics. These streams of data are processed immediately to generate personalized recommendations, monitor service health, and power analytics dashboards — all without losing a single event.”
Conclusion
“Apache Kafka has become the backbone of modern real-time data streaming. Whether you’re building real-time analytics, event-driven applications, or scalable data pipelines, Kafka provides a reliable, fault-tolerant, and high-throughput platform. Start experimenting locally or use managed services like Aiven to experience Kafka in action.”