DEV Community

Vidhyadhar Reddy
Vidhyadhar Reddy

Posted on

Understanding Kafka..

What is Kafka?

Imagine you are in a classroom:
One student (the producer) makes announcements.
The teacher (the consumer) listens and notes them down.
Now, what if there are many students making announcements and many teachers listening? Chaos!

Kafka comes in as the loudspeaker system in the classroom:
All messages go to Kafka first.
Anyone who wants to listen can tune in whenever they want.
Even if you miss the live announcement, you can replay it later.
(from where you want to listen (offset))

Core Components of Kafka

Producer → Sends messages (data) into Kafka.
Broker → Kafka server that stores the messages.
Topic → Like a folder where messages are stored (e.g., “chat-messages”).
Partition → Each topic is split into partitions to handle more data. Order is guaranteed within a partition.
Consumer → Reads messages from Kafka.
Zookeeper → The manager that keeps track of brokers and decides leaders (though in new Kafka versions, Zookeeper is being replaced by KRaft).

Why Use Kafka?

Scalable → Can handle millions of messages per second.
Reliable → Messages are not lost; they are stored for a configurable time.
Ordered → Within one partition, messages are always in order.
Decoupled → Producers and consumers don’t need to know each other.

Delivery Guarantees

At least once → No message is lost, but duplicates may happen.
At most once → No duplicates, but some messages may be lost.
Exactly once → Message delivered once and only once (hardest, but Kafka supports it).

Real-World Use Cases

Chat Apps (like WhatsApp) → Store and deliver messages in order.
E-commerce → Track orders, payments, and inventory updates.
IoT Devices → Handle continuous data from sensors.
Banking → Real-time fraud detection.
Analytics → Collect logs and show dashboards in real-time

Kafka vs Sockets (Quick Example)

Socket = Phone call (both must be online, message not stored).
Kafka = WhatsApp message (you can be offline, but messages are stored and delivered later)

Read about whatsapp why they used (Sockets + XMPP) instead of kafka?

Final Words

Think of Kafka as a giant post office for data:
Producers = people posting letters.
Kafka = the post office (stores and forwards reliably).
Consumers = people who receive the letters.
Zookeeper = the postmaster who ensures everything runs smoothly.

Kafka shines when you need to process a huge amount of real-time data reliably and in order

Top comments (1)

Collapse
 
sakethram_reddyseelam_6c profile image
Sakethram Reddy Seelam

Good One.