Pages

Saturday, January 17, 2026

Kafka Explained


1. The Problem: Tightly Coupled Architecture

In a traditional food delivery app (like Zomato or Uber Eats), a single Order Processing Service might need to communicate with several other microservices:

  • Restaurant Service: Send the order to the kitchen.

  • Delivery Service: Schedule a driver.

  • Analytics Service: Track user behavior.

  • Notification Service: Send an SMS to the user.

Key Issues:

  1. Blocking (Synchronous) Calls: The system waits for Service A to finish before calling Service B. If one service is slow, the entire order process stalls—much like a traffic jam at a single intersection.

  2. Single Point of Failure: If the Analytics service is down, the entire order might fail because the code is waiting for a response that never comes.

  3. Lack of Flexibility: Adding a new service (e.g., a "Loyalty Points" service) requires changing and redeploying the core order processing code.


2. The Solution: Event-Driven Architecture

Instead of calling every service directly, the Order Service simply writes an Event (e.g., "Order #123 Placed") to a Message Buffer.

  • The Producer: The Order Service "produces" a message and moves on immediately. It doesn't wait for anyone.

  • The Consumers: Services (Restaurant, Delivery, etc.) "consume" the message whenever they are ready.

The Post Box Analogy: It is like a postman delivering mail. The postman doesn't wait for you to open the door and read the letter; they drop it in the box (buffer) and leave. You read it when you are free.


3. What is Apache Kafka?

Kafka is an open-source distributed event streaming platform. It acts as the "buffer" mentioned above, but with high-performance capabilities.

Core Concepts:

  • Topics: Categories used to organize messages (e.g., an "Orders" topic vs. a "Payments" topic).

  • Persistence: Unlike traditional systems, Kafka saves messages to a disk. You can configure it to keep data for days or even years.

  • Replayability: Because data is saved, if a service fails, you can "replay" the events from the last 30 minutes to fix the data.

  • Offsets: Every consumer has a "marker" (offset) showing where they left off in the stream, allowing them to process at their own speed.


4. Kafka vs. Traditional Message Queues (RabbitMQ)

While they look similar, their fundamental designs differ:

FeatureMessage Queue (RabbitMQ)Event Streaming (Kafka)
RetentionMessages are deleted once consumed.Messages are retained (durable log).
ConsumptionUsually "Push-based" (Broker sends to user)."Pull-based" (User asks for data).
ReplayNot possible (Samosa analogy: once eaten, it's gone).Possible (YouTube analogy: watch and rewind).
ScaleThousands of messages.Trillions of messages.

5. Real-World Use Cases

  • Netflix: Uses Kafka to track your clicks and "thumbs up" to update recommendation engines in real-time.

  • Tesla: Streams billions of data points from car sensors (GPS, battery, speed) for predictive maintenance and monitoring.

  • FinTech: Credit card companies use it for instant fraud detection. If a transaction occurs, a fraud model consumes that event and alerts you within milliseconds.


6. When to Use (and Not Use) Kafka

Use Kafka when:

  • You have humongous data volumes (millions/trillions of events).

  • You need real-time data pipelines.

  • You need to decouple services so they don't depend on each other's uptime.

Avoid Kafka when:

  • You have small data volumes (less than 1,000 messages/day).

  • You need to perform complex SQL queries (Kafka is not a database).

  • A simple Request-Response pattern is sufficient for your task.


Apache Kafka is a distributed event streaming platform where applications publish events, other applications consume them independently, and events are stored durably and in order for real-time and replayable processing.

No comments:

Post a Comment