Kafka
30 interview questions and answers in the Kafka section.
Questions in this section
- What is topic in Kafka
- What is partition and why is it needed
- How is data distributed across partitions
- What is message key and how does it affect partitioning
- What is Consumer Group
- How does consumer balancing work in a group
- Can you have more consumers than partitions
- What happens when you add a new consumer to the group
- What message delivery guarantees does Kafka provide
- What is the difference between at-most-once, at-least-once and exactly-once
- How to configure exactly-once semantics
- What is offset in Kafka
- How does offset commit work
- What is the difference between auto commit and manual commit
- What is rebalancing and when does it happen
- What is replication in Kafka
- What are leader and follower replicas
- What is ISR (In-Sync Replicas)
- How does Kafka ensure fault tolerance
- What is producer acknowledgment and what modes exist (acks=0,1,all)?
- What is batch in Kafka producer
- How does message compression work
- What is idempotent producer
- How to handle errors when reading messages in Kafka
- What is DLQ (Dead Letter Queue) in Kafka
- How to monitor consumer lag in Kafka
- What is retention policy in Kafka
- How are old messages deleted from a Kafka topic
- Can you read messages from a specific partition in Kafka
- How to implement message filtering on consumer side in Kafka
Study navigator
30 interview preparation questions for Middle/Senior Java Developer.
π All Questions
πΊοΈ Topic Dependency Map
ββββββββββββββββββββββββββββββββββββββββββββ
β KAFKA BASICS (1-4) β
β 1. Topic β
β 2. Partition β
β 3. Data Distribution β
β 4. Message Key β
ββββββββββββββββββββ¬ββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β CONSUMER β β DELIVERY β β PRODUCER β
β GROUPS (5-8) β β GUARANTEES β β (20-23) β
β 5. Consumer β β (9-11) β β 20. acks=0,1,all β
β Group β β 9. Guarantees β β 21. Batch β
β 6. Balancing β β 10. At-most/ β β 22. Compression β
β 7. More cons. β β least/ β β 23. Idempotent β
β 8. Adding new β β exactly β β producer β
β consumer β β 11. Configure β β β
βββββββββ¬ββββββββ βββββββββ¬ββββββββ ββββββββββ¬ββββββββββββ
β β β
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββ
βΌ
ββββββββββββββββββββββββββββββββββββββββββββ
β OFFSET AND REBALANCING (12-15) β
β 12. Offset β
β 13. Commit offset β
β 14. Auto vs Manual commit β
β 15. Rebalancing β
ββββββββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β REPLICATION β β ERRORS AND β β RETENTION AND β
β AND FA (16-19) β β DLQ (24-26) β β FILTERING β
β 16. Replica. β β 24. Error β β (27-30) β
β 17. Leader/ β β handling β β 27. Retention β
β Follower β β 25. DLQ β β 28. Deletion β
β 18. ISR β β 26. Lag monit. β β 29. Partition read β
β 19. Fault β β β β 30. Filtering β
β tolerance β β β β β
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
π― Recommended Study Order
π’ Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Basics | Q1, Q2, Q3, Q4 | Topic, partition, key, distribution |
| 2 | Consumer Group | Q5, Q6 | Groups, balancing |
| 3 | Offset and commit | Q12, Q13, Q14 | Offset, commit types |
| 4 | Delivery guarantees | Q9, Q10 | At-most, at-least, exactly-once |
| 5 | Producer acks | Q20 | acks=0,1,all |
π‘ Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Rebalancing | Q7, Q8, Q15 | When it happens, eager vs cooperative |
| 2 | Replication | Q16, Q17, Q18 | ISR, leader/follower, fault tolerance |
| 3 | Batch & compression | Q21, Q22 | batch.size, linger.ms, lz4/gzip/snappy |
| 4 | Error handling | Q24, Q25 | Retry, DLQ, poison pill |
| 5 | Retention | Q27, Q28 | retention.ms, retention.bytes, cleanup.policy |
| 6 | Monitoring | Q26 | Consumer lag, tools |
π΄ Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Exactly-once | Q11 | EOS v1 vs v2, Producer ID/Epoch, Outbox |
| 2 | Idempotent producer | Q23 | PID, Epoch, max.in.flight, deduplication |
| 3 | Fault tolerance deep | Q19 | RF, min.insync.replicas, unclean election |
| 4 | Advanced consumer | Q29, Q30 | assign() vs subscribe(), filtering patterns |
| 5 | ISR deep dive | Q18 | ISR shrink/expand, LEO, HW, leader epoch |
| 6 | Chaos engineering | Q15 (Senior) | Cascading rebalancing, GC pauses, tuning |
π Key Topic Connections
Topic: Kafka Basics
Q1 (Topic) β Q2 (Partition) β Q3 (Distribution) β Q4 (Key)
Key connections:
- Q2 β Q16: Partitions are replicated for fault tolerance
- Q3 β Q20: acks=all affects when a message is considered delivered
- Q4 β Q3: Key determines partition via MurmurHash
Topic: Consumer Groups
Q5 (Consumer Group) β Q6 (Balancing) β Q7 (More consumers)
β
Q8 (Adding consumer) β Q15 (Rebalancing)
Key connections:
- Q5 β Q12: Each consumer commits offset for each partition
- Q6 β Q15: Balancing is the rebalance itself
- Q7 β Q8: Idle consumers waste resources, adding a new one triggers rebalance
Topic: Offset and Delivery
Q12 (Offset) β Q13 (Commit) β Q14 (Auto vs Manual)
β
Q9 (Guarantees) β Q10 (At-most/at-least/exactly) β Q11 (Configure exactly-once)
Key connections:
- Q13 β Q9: Commit BEFORE processing = at-most-once, AFTER = at-least-once
- Q14 β Q13: Auto commit = simpler but risk of loss; Manual = control
- Q10 β Q11: Exactly-once requires transactional producer
Topic: Producer
Q20 (acks) β Q21 (Batch) β Q22 (Compression) β Q23 (Idempotent)
Key connections:
- Q20 β Q23: Idempotent producer + acks=all = exactly-once delivery to topic
- Q21 β Q22: Batch + compression = maximum throughput
- Q23 β Q11: Idempotent producer β the foundation of exactly-once
Topic: Fault Tolerance
Q16 (Replication) β Q17 (Leader/Follower) β Q18 (ISR) β Q19 (Fault tolerance)
Key connections:
- Q17 β Q18: Leader is always in ISR, followers drop out if lagging
- Q18 β Q19: ISR shrink β reduced fault tolerance
- Q16 β Q20: acks=all waits for acknowledgment from all ISR replicas
Topic: Operations
Q24 (Error handling) β Q25 (DLQ) β Q26 (Lag monitoring)
β
Q27 (Retention) β Q28 (Deletion) β Q29 (Partition read) β Q30 (Filtering)
Key connections:
- Q24 β Q25: DLQ β the last resort after retries
- Q25 β Q26: Lag monitoring helps detect issues before DLQ
- Q27 β Q28: Retention policy determines when and how messages are deleted
- Q29 β Q30: assign() for reading specific partitions, filtering on consumer side
π Cheat Sheet: What to Know for Each Level
π’ Junior
- Topic = logical channel, partition = sub-channel for parallelism
- Consumer Group = mechanism for distributing partitions among consumers
- Offset = position in partition, commit = saving position
- acks=0 (fire-and-forget), acks=1 (leader only), acks=all (all ISR)
- At-most-once, at-least-once, exactly-once β three delivery semantics
π‘ Middle
- Rebalancing: eager (full stop) vs cooperative (partial)
- ISR: replicas that caught up with the leader. Leader is always in ISR
- Batch: batch.size, linger.ms, compression β three throughput levers
- DLQ: queue for messages that couldnβt be processed after N retries
- Retention: retention.ms (time) + retention.bytes (size per partition)
- Sticky partitioning: Kafka 2.4+ replaces round-robin
π΄ Senior
- Exactly-once: EOS v2 (Kafka 2.5+), Producer ID + Epoch, transaction coordinator
- Idempotent producer: deduplication at broker level via PID + sequence
- ISR shrink/expand: monitoring is critical for fault tolerance
- Cascading rebalancing: GC pause β exclude β rebalance β overload β another one falls
- Outbox pattern: for exactly-once Kafka-to-Database
- Leader Epoch: tracks data loss during failover
π File Format
Each file contains:
- π’ Junior Level β basic understanding, simple analogies, examples
- π‘ Middle Level β internals, common mistakes, practical examples
- π΄ Senior Level β deep dive, edge cases, production experience, monitoring
- π― Interview Cheat Sheet β key takeaways, common questions, red flags, related topics