DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on

KAFKA IN SPRING BOOT

Great Bhupendra πŸ‘
Since you like real understanding (not ratta), I’ll make Kafka super simple + interview ready + practical mindset.


πŸš€ Apache Kafka – Short & Easy Way to Understand

🧠 First Understand in 1 Line

πŸ‘‰ Kafka = Real-time data post office
It delivers messages between applications very fast & reliably.


🍽️ Simple Example (Udupi Hotel)

Imagine this flow:

Customer App  β†’  Kafka  β†’  Udupi Hotel App
Enter fullscreen mode Exit fullscreen mode
  • Customer = Producer
  • Udupi Hotel = Consumer
  • Kafka = Message Broker
  • Topic = Message box

πŸ” Real Flow

1️⃣ Customer Places Order

Customer App ---> [order_topic] ---> Hotel App
Enter fullscreen mode Exit fullscreen mode

Customer = Producer
Hotel = Consumer


2️⃣ Hotel Accepts Order

Hotel App ---> [order_status_topic] ---> Customer App
Enter fullscreen mode Exit fullscreen mode

Now:
Hotel = Producer
Customer = Consumer

πŸ‘‰ Roles can change!


πŸ—οΈ Kafka Architecture (Very Easy)

Producer β†’ Topic β†’ Consumer
Enter fullscreen mode Exit fullscreen mode

Important Components:

Component Meaning (Simple Language)
Producer Who sends message
Consumer Who receives message
Topic Message container
Broker Kafka server
Zookeeper Manages Kafka (older versions)

πŸ”₯ Real Interview-Level Understanding

Why Not Just Use Database?

Use Case DB Kafka
Store order βœ… Yes ❌ No
Real-time notification ❌ No βœ… Yes
Heavy traffic ❌ Slow βœ… Very Fast
Event streaming ❌ Not ideal βœ… Best

πŸ‘‰ Kafka is NOT database replacement
πŸ‘‰ Kafka is event streaming system


🎯 Where Kafka Is Used in Industry

  • Swiggy / Zomato β†’ Orders
  • Amazon β†’ Order tracking
  • Banking β†’ Transactions
  • Uber β†’ Location tracking
  • Microservices communication

Since you're learning microservices πŸ”₯
πŸ‘‰ Kafka is used for asynchronous communication


⚑ Important Kafka Concepts (Must Know)

1️⃣ Topic

Logical container for messages.

Example:

  • order_topic
  • payment_topic

2️⃣ Partition (Very Important 🚨)

Topic is divided into partitions.

Why?
πŸ‘‰ Parallel processing
πŸ‘‰ High scalability

order_topic
   β”œβ”€β”€ Partition 1
   β”œβ”€β”€ Partition 2
   └── Partition 3
Enter fullscreen mode Exit fullscreen mode

More partitions = More performance


3️⃣ Offset

Each message has unique number inside partition.

Message 1 β†’ Offset 0
Message 2 β†’ Offset 1
Message 3 β†’ Offset 2
Enter fullscreen mode Exit fullscreen mode

Consumer remembers offset
So no duplicate processing.


4️⃣ Consumer Group

Multiple consumers can read same topic.

order_topic
   ↓
Consumer Group A
   β”œβ”€β”€ Consumer 1
   └── Consumer 2
Enter fullscreen mode Exit fullscreen mode

Kafka distributes partitions among them.


🧩 Modern Kafka Architecture

⚠️ Important:

πŸ‘‰ New Kafka versions (3.x+) do not require Zookeeper
They use KRaft mode

So in interviews:
If someone says Zookeeper mandatory β†’ ❌ Old knowledge.


πŸ› οΈ Setup (Simplified for Understanding)

You basically:

  1. Start Zookeeper (old versions)
  2. Start Kafka
  3. Create Topic
  4. Start Producer
  5. Start Consumer

πŸ’‘ Very Important Interview Questions

Q1: What is Kafka?

Distributed event streaming platform.


Q2: Why Kafka is fast?

  • Sequential disk write
  • Partitioning
  • Zero copy transfer

Q3: Difference Between Kafka and RabbitMQ?

Kafka RabbitMQ
Event streaming Message queue
High throughput Low latency
Log-based Queue-based
Scalable Less scalable

Q4: What is Exactly Once?

Kafka ensures:

  • No duplicate
  • No message loss

(using idempotent producer + transactions)


🧠 Real Microservices Example (Important for You)

Suppose:

Order Service
Payment Service
Notification Service

Instead of direct REST calls:

Order Service β†’ Kafka β†’ Payment Service
Payment Service β†’ Kafka β†’ Notification Service
Enter fullscreen mode Exit fullscreen mode

Benefits:

  • Loose coupling
  • Scalable
  • Fault tolerant

🎯 When To Use Kafka?

Use Kafka When:

βœ” High traffic
βœ” Real-time updates
βœ” Microservices
βœ” Event driven architecture
βœ” Log streaming

Do NOT use Kafka when:

❌ Small CRUD app
❌ Simple project


🧾 Final 30-Second Revision

Kafka = Distributed event streaming platform

Main parts:

  • Producer
  • Consumer
  • Topic
  • Partition
  • Offset
  • Broker

Use for:

  • Real-time
  • High traffic
  • Microservices

==========================

================================
Perfect Bhupendra πŸ”₯
Since you’re learning Microservices, this is VERY important for interviews.

πŸš€ Kafka vs REST in Microservices (Easy + Practical)


🧠 First Understand Core Difference

REST Kafka
Synchronous Asynchronous
Direct call Message-based
Tight coupling Loose coupling
Request/Response Event-driven

πŸ“ž 1️⃣ REST in Microservices

Flow:

Order Service  β†’  Payment Service  β†’  Notification Service
Enter fullscreen mode Exit fullscreen mode
  • Order calls Payment via HTTP
  • Payment returns response
  • Then Order calls Notification

πŸ”΄ Problem:

  • If Payment service is DOWN ❌ β†’ Order fails
  • Services tightly connected
  • Hard to scale under heavy load

πŸ”₯ 2️⃣ Kafka in Microservices

Flow:

Order Service β†’ [Kafka: order_created] β†’ Payment Service
Payment Service β†’ [Kafka: payment_done] β†’ Notification Service
Enter fullscreen mode Exit fullscreen mode
  • Order publishes event
  • Payment consumes event
  • No direct dependency

🟒 Benefit:

  • If Payment is down β†’ Kafka stores message
  • When Payment comes back β†’ It processes messages
  • No data loss

πŸ—οΈ Real Architecture Comparison

πŸ”΄ REST Based Architecture

Client
  ↓
Order Service
  ↓ HTTP
Payment Service
  ↓ HTTP
Notification Service
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Chain dependency
πŸ‘‰ Slow under heavy traffic


🟒 Kafka Based Architecture

Client
  ↓
Order Service
  ↓
[Kafka]
  ↓
Payment Service
  ↓
[Kafka]
  ↓
Notification Service
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Event Driven
πŸ‘‰ Scalable
πŸ‘‰ Fault Tolerant


🎯 Real Interview Question

Q: When should we use REST instead of Kafka?

Use REST when:

βœ” Immediate response needed
βœ” Simple CRUD
βœ” Less traffic
βœ” Small system

Example:

  • Login API
  • Fetch user profile

Q: When should we use Kafka?

Use Kafka when:

βœ” High traffic
βœ” Event driven
βœ” Decoupled microservices
βœ” Retry mechanism required
βœ” Real-time analytics

Example:

  • Order system
  • Payment processing
  • Ride booking
  • Banking transactions

⚑ Important Microservices Concept

πŸ”Ή Synchronous vs Asynchronous

REST = Synchronous

Client waits for response.

Kafka = Asynchronous

Producer doesn’t wait.
Message goes to topic.


🧠 Real Industry Example

Amazon Order Flow

  1. Order Created β†’ Kafka
  2. Inventory Updated β†’ Kafka
  3. Payment Processed β†’ Kafka
  4. Email Sent β†’ Kafka

Each service works independently.


πŸ’₯ Performance Difference

Feature REST Kafka
Scalability Medium Very High
Fault tolerance Low High
Retry Manual Built-in
Throughput Limited Millions/sec
Coupling Tight Loose

🧩 What Big Companies Use?

  • Netflix β†’ Kafka
  • Uber β†’ Kafka
  • LinkedIn β†’ Kafka (created by LinkedIn)
  • Amazon β†’ Event-driven architecture

🎯 Final Easy Rule (Remember This)

If service must WAIT β†’ Use REST
If service must NOT WAIT β†’ Use Kafka


πŸ”₯ For Your Interview (Spring Boot Example Answer)

If interviewer asks:

"How would you design order microservice?"

You say:

I would use REST for synchronous calls like login or fetching user data.
For order processing, payment, and notification, I would use Kafka for asynchronous communication to ensure loose coupling and scalability.

πŸ”₯ That’s senior-level answer.


==================

==========================

Top comments (0)