0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · Lesson

Handling Producer Send Callbacks and Acknowledgments

Learn how to react to the outcome of asynchronous Kafka sends using callbacks, CompletableFuture, and acks configuration to guarantee delivery reliability.

Handling Producer Send Callbacks and Acknowledgments is a free Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Send Results Matter

When you call KafkaTemplate.send(), the message is dispatched asynchronously. The call returns immediately, but the broker may not have stored the record yet.

To build reliable producers you must inspect the result of each send so you can log, retry, or alert on failures.

The CompletableFuture Result

In Spring Kafka, send() returns a CompletableFuture<SendResult<K,V>>.

  • SendResult holds the RecordMetadata (partition, offset, timestamp).
  • You attach a continuation to handle success or failure.
CompletableFuture<SendResult<String,String>> future =
    kafkaTemplate.send("orders", order.getId(), payload);

Attaching whenComplete

Use whenComplete to handle both outcomes in one place. The first argument is the result, the second is the exception (null on success).

future.whenComplete((result, ex) -> {
    if (ex == null) {
        var md = result.getRecordMetadata();
        log.info("Sent to partition {} offset {}", md.partition(), md.offset());
    } else {
        log.error("Send failed", ex);
    }
});

Reading RecordMetadata

On success, RecordMetadata tells you exactly where the record landed.

  • partition() — which partition received it.
  • offset() — its position in that partition.
  • timestamp() — broker-assigned timestamp.
RecordMetadata md = result.getRecordMetadata();
String location = md.topic() + "-" + md.partition() + "@" + md.offset();

Blocking for the Result

Sometimes you need a synchronous guarantee. Call get() with a timeout to block until the broker acknowledges.

Use this sparingly — it kills throughput, but it is useful in tests or critical writes.

SendResult<String,String> result =
    kafkaTemplate.send("orders", payload).get(10, TimeUnit.SECONDS);

The acks Configuration

Acknowledgment durability is controlled by the producer acks setting:

  • acks=0 — fire and forget, no guarantee.
  • acks=1 — leader writes, then acknowledges.
  • acks=all — leader plus all in-sync replicas acknowledge.

Configuring acks in Spring Boot

Set the strongest durability with acks=all in your application.yml.

spring:
  kafka:
    producer:
      acks: all
      properties:
        min.insync.replicas: 2

Trade-off: Latency vs Durability

Stronger acks mean higher latency.

  • acks=0 is fastest but loses data on broker failure.
  • acks=all is safest but waits for replica confirmation.

For financial events choose all; for high-volume metrics 1 may suffice.

Centralizing Callback Logic

Avoid duplicating callback code. Wrap sends in a helper method that always logs metadata and failures consistently.

public void sendTracked(String topic, String key, String value) {
    kafkaTemplate.send(topic, key, value)
        .whenComplete((res, ex) -> {
            if (ex != null) metrics.incrementFailures();
            else metrics.incrementSuccess();
        });
}

Handling Failures Gracefully

In the failure branch you can:

  • Persist the failed payload to a fallback store.
  • Increment a failure metric for alerting.
  • Schedule a retry on a separate executor.

Never swallow the exception silently.

Putting It Together

A robust producer combines acks=all, callback inspection, and failure handling. This gives you observability and delivery guarantees without blocking your main flow.

future.whenComplete((res, ex) -> {
    if (ex != null) deadLetterStore.save(payload);
});

Quick Check

Test your understanding of producer acknowledgments.

Recap

You learned to handle the asynchronous result of Kafka sends.

  • send() returns a CompletableFuture<SendResult>.
  • Use whenComplete to inspect metadata or failures.
  • acks controls the durability vs latency trade-off.
  • Centralize callbacks for consistent reliability handling.

Frequently asked questions

Is the “Handling Producer Send Callbacks and Acknowledgments” lesson free?

Yes — the full text of “Handling Producer Send Callbacks and Acknowledgments” is free to read here on the web, and the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) course, upgrade to CoddyKit PRO.

What will I learn in “Handling Producer Send Callbacks and Acknowledgments”?

Learn how to react to the outcome of asynchronous Kafka sends using callbacks, CompletableFuture, and acks configuration to guarantee delivery reliability. You practise Advanced Spring Boot 4: Event-Driven Architecture (Kafka) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?

No prior experience is required. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Handling Producer Send Callbacks and Acknowledgments” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson?

Yes. Every Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Integrating Spring Kafka Starter
  2. Sending Messages with KafkaTemplate
  3. Customizing Producer Configurations
  4. Handling Producer Send Callbacks and Acknowledgments
← Back to Advanced Spring Boot 4: Event-Driven Architecture (Kafka)