← Back to list

Messaging Queue (Kafka)

Messaging queue kafka earlier one microservice try to contact with another then it will be a synchronous communication- it will wait for…

omprakash · 2026-04-24 06:26 · 0 claps · 4.4 min read
#messaging-queue #kafka
Open on Medium ↗

Messaging Queue (Kafka)

Messaging queue kafka

earlier one microservice try to contact with another then it will be a synchronous communication- it will wait for reply wehn we send http till then it will be idle after i came to asynch communication but when request is huge and high throughput then normal service cant handle much throughput m because its less throughtput in microservice2

throughtput — OPS, how many req it can handle lets say ola/uber/rapido

when GPS send req to ola server, the frequency for sending the gps singla is 1 signal/sec and if we do Back of the envelope and lets say if in delhi there is huge rain fall or any environmental climate going on and from delhi — req — 1lakh driver in ola per sec — 1lakh req/sec

so in this case any db can crash because of huge request either sql/nosql So we introduce message queue- kafka now gps send 1lakh/sec send to producer then producer send this req to kafka then it will send to worker then it will send to DB.

Producer will push so many message at a time to kafka and worker is consumer interacting with DB again and again would increase the latency of verall application.

Now i dug deep into kafka architecture cluster, broker, topic, partition, offset,, zookeeper, consumer, consumer group

So ola can have different kind of message -> rider, notification, fare price notification, gps location

SO inside kafka we have broker (kafka Server) and inside it we can have many topic(topic 0, topic 1, topic 2 ….) and inside the each topic we can have many partition (partition0, partition1, ……) and inside each partition we can have any many offset ( offset0, offset1….)

now if we can imagine we have producer, it will send req to offset0 and then it will send req to consumer. This is whole architecture Now we will introduce cinsumer group now when offset send req to any consumer1 or consumer2 it will be decided by auto balancing- redistributing partition among consumers when system changes kafka runs in distributed env multiple consumers and partitions so work mucst evenly distributed broker wont give one partition access to 2 consumer If no.of partition are less than the no.of consumers , some cinsumers will remain idle 1 consumer can consumer multiple partition but 1 partition can be consumed by only 1 consumers

Now we will be introducing 2 consumer group inside the consumer group we can have many consumer (consumer1,….) Now here 1 partition can be consumed by many consumer group and inside consumer group one cunsumer can consumer 1 partition.

You’ve understood the core motivation for Kafka and async pub‑sub very well 👍 Now let me cleanly re‑frame, correct a few misconceptions, and connect this to real Ola/Uber‑scale design thinking, so your mental model becomes interview‑ready + production‑ready.

I’ll go step by step and map to what you already wrote.

1. Problem recap (you are right ✅)

Synchronous microservice call (HTTP)

Service A  --> HTTP --> Service B --> DB

Problems:

  • Service A waits for Service B’s response
  • If Service B is slow → cascading failure
  • Throughput limited by:
  • Service B’s TPS
  • DB TPS

Ola GPS example (excellent intuition ✅)

  • GPS signal frequency ≈ 1/sec/driver
  • 1 lakh active drivers in Delhi → 100,000 writes/sec

No traditional system can safely:

  • Accept
  • Process
  • Persist all at the same speed

Even NoSQL will choke if you naïvely write per request.

2. Why Kafka actually helps (important clarity)

❌ Kafka does NOT magically increase DB capacity ✅ Kafka decouples ingestion from processing

Think of Kafka as a shock absorber.

Without Kafka

GPS --> API --> DB (DB rate limited) ❌

With Kafka

GPS --> Producer --> Kafka  ||buffer||  --> Consumers --> DB ✅

Key benefits:

  1. Absorb spikes (rain, surge, events)
  2. Smooth processing
  3. Protect DB
  4. Scale consumers independently

Kafka handles ingestion scale, not storage scale.

3. Correct Kafka mental model (important corrections)

Kafka is NOT

  • Request/response
  • Push-based to consumers

Kafka IS

  • Distributed append-only log
  • Consumers pull data

4. Kafka core components (refined)

4.1 Cluster & Broker

  • Cluster = multiple Kafka servers
  • Broker = one Kafka server
Kafka Cluster
 ├── Broker 1
 ├── Broker 2
 └── Broker 3

4.2 Topic

A logical stream of messages

Examples for Ola:

  • gps-location
  • ride-events
  • fare-updates
  • notifications

4.3 Partition (this is the scaling unit)

Each topic → split into partitions

Topic: gps-location
 ├── Partition 0
 ├── Partition 1
 ├── Partition 2

Why partitions?

  • Parallelism
  • Ordering guarantee only inside a partition

4.4 Offset (small but important correction)

❌ Producer does NOT send to offset ✅ Offset is assigned after append

Partition 0:
 offset 0
 offset 1
 offset 2
 offset 3
  • Offset = position in the log
  • Kafka assigns it
  • Consumer keeps track of “last read offset”

5. Producer flow (corrected)

GPS Service
   |
   |--> Producer
            |
            |--> Topic: gps-location
                      |
                      |--> Partition chosen by:
                            - key (driverId)
                            - or round-robin

✅ Producer only writes ✅ No knowledge of consumers ✅ Extremely fast (sequential disk + batching)

6. Consumer basics (VERY IMPORTANT)

Consumers poll, Kafka does NOT push

Consumer ---> poll() ---> Kafka

Consumers:

  • Read messages
  • Process
  • Commit offsets

7. Consumer Group (you explained this nearly perfectly ✅)

Let’s restate cleanly.

Core rules (memorize these)

  1. One partition → one consumer (inside a consumer group)
  2. One consumer → many partitions (possible)
  3. One partition → MANY consumer groups (allowed)

Example

Topic gps-location with 6 partitions

Consumer Group: gps-db-writer

Consumer A → P0, P1
Consumer B → P2, P3
Consumer C → P4, P5

Consumer Group: gps-analytics

Consumer X → P0, P1, P2
Consumer Y → P3, P4, P5

✅ Same partition ✅ Multiple consumer groups ✅ Independent offsets

8. Auto rebalancing (what you said is correct ✅)

When:

  • Consumer joins
  • Consumer dies
  • Partition count changes

Kafka:

  • Reassigns partitions
  • Only one consumer per partition per group

If:

Partitions = 4
Consumers = 6

→ 2 consumers idle ✅ (you were right)

9. Latency concern (very important design insight)

You said:

worker interacting with DB again and again would increase latency

Correct, but that’s expected

Kafka trades latency for reliability and throughput

Typical GPS pipeline:

Kafka → Consumer → Batch → DB

Why batching matters

Instead of:

1 Kafka msg → 1 DB write ❌

Do:

1000 Kafka msgs → 1 DB write ✅

This:

  • Saves DB
  • Lowers cost
  • Improves throughput massively

10. Real Ola/Uber GPS architecture (simplified)

Drivers
  |
  |--> Ingestion API (stateless)
            |
            |--> Kafka (gps-location)
                      |
                      |--> Stream Processor (Flink/KStreams)
                                |
                                |--> Cache (Redis)
                                |--> Analytics
                                |--> Cold storage

Not every GPS signal goes to DB

  • Most recent location → Redis
  • Aggregated paths → DB
  • Raw logs → S3 / HDFS

11. Zookeeper (short note)

  • Older Kafka: used for metadata & leader election
  • New Kafka (KRaft mode): no Zookeeper

You don’t need to emphasize Zookeeper in interviews anymore.

12. Final mental model (one‑line summary)

Kafka is a distributed, durable, high‑throughput commit log that decouples fast producers from slow consumers using partitions, offsets, and consumer groups.


메타데이터
post_id
fb2b129342ef
slug
messaging-queue-kafka-fb2b129342ef
url
https://medium.com/@omprakash524/messaging-queue-kafka-fb2b129342ef
canonical_url
https://medium.com/@omprakash524/messaging-queue-kafka-fb2b129342ef
author_url
https://medium.com/@omprakash524
status
ok
fetched_at
2026-06-28 14:26:31