Kafka Consumer
CommitSync
CommitAsync
Data Processing
Stream Processing

Kafka consumer commitSync vs commitAsync

Codemia Knowledge Hub ·

commitSync() blocks until the broker acknowledges the offset commit or an unrecoverable error occurs, so it is reliable but adds latency. commitAsync() sends the commit in the background and keeps polling, so it gives higher throughput, but failures only surface through a callback. Many applications commit asynchronously most of the time and synchronously at key points.

What committing an offset means

Every message in a Kafka partition has a sequential ID, the offset, that uniquely identifies it. Consumers track which messages they have processed by offset and should periodically save the offsets they have consumed, so that after a restart they resume where they left off. Saving the offset is called committing it. Both methods below exist so the consumer does not lose its place and handles restarts gracefully.

commitSync()

commitSync() commits the last offset of the messages returned by poll() and blocks until the commit succeeds or hits an unrecoverable error. Because it waits for the broker's acknowledgment, a failure throws an exception you can handle, for example by retrying. The consumer's position is safely recorded before it consumes anything further. The cost is latency: no new messages are processed while the commit is in progress.

java
1try {
2    while (true) {
3        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
4        for (ConsumerRecord<String, String> record : records) {
5            processRecord(record);
6        }
7        consumer.commitSync();
8    }
9} catch (Exception e) {
10    log.error("Commit failed", e);
11} finally {
12    consumer.close();
13}

commitAsync()

commitAsync() commits offsets like commitSync() but does not block. The consumer keeps polling while the commit is handled in the background, which improves throughput, especially under high load. Since it does not wait for Kafka's response, failed commits are harder to handle. One strategy is to pass a callback that logs commit errors.

java
1consumer.poll(Duration.ofMillis(100)).forEach(record -> processRecord(record));
2consumer.commitAsync((offsets, exception) -> {
3    if (exception != null) {
4        log.error("Commit failed for offsets {}", offsets, exception);
5    }
6});

Comparing the two

FeaturecommitSync()commitAsync()
BlockingYes, it blocks until the commit completes.No, it allows continued processing.
Error HandlingErrors are thrown directly.Errors need to be handled in a callback.
ReliabilityHigher, due to synchronous commit.Lower, commit might fail silently.
ThroughputLower, due to blocking nature.Higher, as it does not block processing.
Use CaseSafety-critical applications.High-throughput applications where occasional loss is acceptable.

Which one to use

If you need high reliability and can give up some throughput, use commitSync. If the application needs higher throughput and can tolerate some failure or loss, commitAsync may fit. In practice, a common pattern combines them: asynchronous commits normally, with periodic synchronous commits to reduce the risk of data loss.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design