Apache Kafka Streaming (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How does Consumer Offset Management work, and what are the trade-offs of Auto-Commit vs. Manual Commit?
senior LevelSystem Design
In Simple Words:
Kafka consumers store their current progress as offsets in an internal compacted topic named __consumer_offsets.
- Auto-Commit (
enable.auto.commit=true):- Commits offsets periodically (default every 5s via
auto.commit.interval.ms). - Risk: If your application crashes after auto-committing but before finishing database writes, records are lost. If it crashes before auto-commit, records are re-processed.
- Commits offsets periodically (default every 5s via
- Manual Commit (
enable.auto.commit=false):commitSync(): Synchronously commits offsets. Blocks until the broker confirms. Safe, but degrades throughput.commitAsync(): Asynchronously commits offsets without blocking. High throughput, but does not retry on commit failure to prevent committing an older offset over a newer one.- Best Practice: Use
commitAsync()during the processing loop, and callcommitSync()in afinallyblock during consumer shutdown.