Memory Atlas Β· Data processing

Apache Kafka

Build a durable mental model of how Spark plans, moves, and optimizes distributed workβ€”then retrieve it under interview pressure.

Chapters
06
Advanced
03
Mode
Recall

Read for structure. Pause at each memory map and answer before revealing the detail.

Foundation

Apache Kafka Overview and Interview Atlas

#

Apache Kafka Overview and Interview Atlas

Answer First: Kafka is a durable distributed log for event streaming: producers append records to topic partitions, brokers replicate those partitions, and consumer groups read offsets independently.

Memory Map: event -> topic -> partition -> offset -> consumer group -> replay.

Kafka in one picture

Answer First: Remember TOPIC-PARTITION-OFFSET: topic is the named stream, partition is the ordered shard, and offset is the position inside that shard.

Memory Map: TOPIC-PARTITION-OFFSET -> logical stream -> parallel log -> durable position -> replayable read -> interview whiteboard.

Use Kafka when you need high-throughput event transport, fan-out to multiple independent consumers, replay, and backpressure. Do not use Kafka as a key-value database; it is optimized for append and sequential read.

Kafka vs queue and managed stream choices

Answer First: Kafka is strongest for replayable logs and high-throughput pub/sub; RabbitMQ is better for routing-heavy task queues, Kinesis is AWS-managed streaming, and Pulsar combines log and queue patterns with multi-tenancy.

Memory Map: tool choice -> replay need -> routing need -> cloud ownership -> tenancy model -> operational cost.

Say this in interviews: "If consumers must independently replay an ordered event history, I start with Kafka. If I need simple task dispatch with priorities and routing, I consider RabbitMQ."

Foundation

Kafka Topics, Partitions, Brokers, and Replicas

#

Kafka Topics, Partitions, Brokers, and Replicas

Answer First: Kafka scales by splitting a topic into partitions; each partition has one leader broker for reads/writes and follower replicas for fault tolerance.

Memory Map: topic -> partition -> leader -> follower -> ISR -> failover.

Topic vs partition vs replica

Answer First: A topic is the logical stream, a partition is the physical ordered log, and a replica is a broker-side copy used for availability.

Memory Map: logical topic -> physical log shard -> per-partition ordering -> replica copies -> leader election.

Ordering is guaranteed inside one partition only. If global ordering is required, you either use one partition or redesign the requirement around per-key ordering.

Partition key, producer partitioner, and sticky batching

Answer First: With a key, the producer hashes the key to keep the same key on the same partition; without a key, modern clients batch using a sticky partitioner for throughput.

Memory Map: record key -> hash partition -> same-key order -> null-key sticky batch -> throughput gain.

Use customer ID, account ID, or order ID when the business needs per-entity ordering. Avoid increasing partitions casually because key hashing changes.

Leader, follower, and ISR

Answer First: The leader handles partition reads/writes, followers fetch from the leader, and ISR contains replicas caught up enough to be eligible for safe leadership.

Memory Map: leader handles traffic -> followers fetch -> ISR health -> acks all -> min.insync guard -> durable write.

Intermediate

Kafka Producers, Consumers, and Consumer Groups

#

Kafka Producers, Consumers, and Consumer Groups

Answer First: Producers decide where records land; consumers read assigned partitions; consumer groups divide work while separate groups replay the same topic independently.

Memory Map: producer send -> partition assignment -> consumer poll -> group ownership -> offset commit -> replay.

Consumer group vs consumer

Answer First: Inside one group, each partition is assigned to exactly one consumer; different groups can read the same topic independently with their own offsets.

Memory Map: group id -> partition assignment -> idle extra consumers -> independent group offsets -> fan-out pattern.

If a topic has four partitions, at most four consumers in the same group can actively read it. More consumers sit idle until partitions increase or another topic is assigned.

Rebalancing without outages

Answer First: Rebalancing redistributes partitions when group membership changes; cooperative sticky assignment and static membership reduce stop-the-world pauses.

Memory Map: membership change -> group coordinator -> partition movement -> cooperative assignor -> static member -> pause reduction.

If 30-second pauses appear, check assignment strategy, heartbeat/session settings, long processing between polls, and frequent autoscaling.

Consumer lag diagnosis

Answer First: Growing lag means consumers are not keeping up with partition production rate; add consumers up to partition count, speed up processing, or repartition after planning ordering impact.

Memory Map: lag metric -> partition hotspot -> processing time -> consumer count limit -> partition redesign -> SLA recovery.

Always diagnose lag by partition, not just topic total. One stuck partition can hide behind healthy aggregate throughput.

Advanced

Kafka Delivery, Ordering, Retention, and Reliability

#

Kafka Delivery, Ordering, Retention, and Reliability

Answer First: Kafka reliability is a contract between producer acknowledgements, replication health, offset commits, retention policy, and idempotent downstream writes.

Memory Map: acks -> ISR -> offset commit -> retention -> compaction -> idempotent sink.

At-most-once, at-least-once, and exactly-once

Answer First: At-most-once commits before work and can lose messages; at-least-once commits after work and can duplicate; exactly-once requires idempotent producers, transactions, read-committed consumers, and compatible sinks.

Memory Map: commit timing -> loss risk -> duplicate risk -> producer idempotence -> transaction boundary -> sink idempotency.

Retention and log compaction

Answer First: Time and size retention delete old segments; compaction keeps the latest record per key so Kafka can represent current state while preserving log semantics.

Memory Map: time retention -> size cap -> compacted key -> tombstone delete -> offset gaps -> restore state.

Use compaction for user profiles, configuration, and CDC topics where the latest value per key matters more than every historical event.

ZooKeeper vs KRaft

Answer First: Modern Kafka uses KRaft for built-in metadata quorum; ZooKeeper belongs to older deployments and is retired from the modern operating model.

Memory Map: legacy ZooKeeper -> metadata quorum -> controller role -> simpler ops -> migration check.

In interviews, phrase it as: "I expect KRaft unless the company is operating an older cluster that still needs a migration plan."

Why Kafka is fast

Answer First: Kafka gets throughput from append-only sequential I/O, batching, compression, OS page cache, partition parallelism, and zero-copy transfer.

Memory Map: append-only log -> batch compression -> page cache -> sendfile zero-copy -> partition parallelism -> broker throughput.

Do not hand-wave "Kafka is fast." Name the mechanics, then connect them to producer batching and broker disk/network limits.

Advanced

Kafka Scenarios, Labs, Gotchas, and Production Traps

#

Kafka Scenarios, Labs, Gotchas, and Production Traps

Answer First: Kafka interview scenarios are solved by first naming the partitioning contract, then the delivery contract, then the operational failure mode.

Memory Map: scenario -> partition key -> delivery semantic -> lag/rebalance risk -> durability config -> recovery test.

Single-node Kafka lab

Answer First: The fastest hands-on proof is: start a local broker, create a topic, produce records, consume from the beginning, and describe the consumer group.

Memory Map: docker broker -> create topic -> produce records -> consume replay -> describe group -> prove offsets.

bash
kafka-topics.sh --create --topic orders --partitions 3 --replication-factor 1
kafka-console-producer.sh --topic orders
kafka-console-consumer.sh --topic orders --from-beginning

Observe partitioning by key

Answer First: Produce several records with the same key and print partitions while consuming; the repeated key should land on the same partition.

Memory Map: keyed produce -> partition print -> same key grouping -> per-key order -> rescale warning.

Use this lab to make ordering concrete: Kafka orders records by partition, not by whole topic.

Visual flows to draw in interviews

Answer First: Draw producer-to-broker-to-consumer flow, ISR shrink and re-election, and compaction; these three pictures cover most Kafka whiteboard prompts.

Memory Map: producer flow -> broker leaders -> consumer offsets -> ISR shrink -> compaction cleaner -> failure tradeoff.

Production gotchas

Answer First: The common Kafka failures are partition rehashing, weak acknowledgements, lag hotspots, wrong offset reset, heartbeat starvation, and oversized messages.

Memory Map: partition increase -> key reorder risk -> acks one loss -> lag hotspot -> poll timeout -> message-size envelope.

Interview-safe fixes: plan partitions early, use acks=all, monitor lag by partition, set auto.offset.reset=earliest for replay, keep processing under max.poll.interval.ms, and align broker/topic/consumer size limits.

Event-driven order system scenario

Answer First: Partition orders by customer, let inventory/payment/shipping read as independent groups, emit service-specific event topics, and use idempotency plus DLQs for safe retries.

Memory Map: order event -> customer partition -> independent service groups -> outcome topics -> idempotent write -> DLQ.

This is the default Kafka design prompt: separate command processing from event observation, then make duplicates harmless.

Partition sizing scenario

Answer First: More partitions increase parallelism but also increase metadata, file handles, elections, and rebalance work; choose partitions from throughput and ordering needs, not vibes.

Memory Map: target throughput -> per-partition budget -> consumer parallelism -> metadata overhead -> rebalance time -> future growth.

Keep a sizing story ready: estimate MB/s, divide by safe throughput per partition, then validate with a load test.

Advanced

Kafka Interview Questions

#

Kafka Interview Questions

These questions are link-only so there is one canonical answer owner.

Q-KAF-001: Design an event-driven order system: user places order β†’ inventory reserves β†’ payment charges β†’ shipping dispatches

Answer owner: Event-driven order system scenario

Alternate source wording: content/kafka/Kafka_01_Confusions_Labs_MockInterview.md#L411.

Q-KAF-002: How do you guarantee exactly-once from producer to downstream system?

Answer owner: At-most-once, at-least-once, and exactly-once

Alternate source wording: content/kafka/Kafka_01_Confusions_Labs_MockInterview.md#L423.

Q-KAF-003: What's the tradeoff between many small partitions vs few large ones?

Answer owner: Partition sizing scenario

Alternate source wording: content/kafka/Kafka_01_Confusions_Labs_MockInterview.md#L433.

Q-KAF-004: How does Kafka achieve high throughput?

Answer owner: Why Kafka is fast

Alternate source wording: content/kafka/Kafka_01_Confusions_Labs_MockInterview.md#L441.

Q-KAF-005: Consumer rebalance is causing 30-second pauses. How to fix?

Answer owner: Rebalancing without outages

Alternate source wording: content/kafka/Kafka_01_Confusions_Labs_MockInterview.md#L451.

90 seconds

Practice sprint

Close the atlas. Rebuild the map.

Name the path from API to files, then explain where shuffle, skew, and serialization enter the system.

Open interview prompts