// ProducerKafkaProducer<String, String> p = new KafkaProducer<>(props);p.send(new ProducerRecord<>("orders", order.id, json));// ConsumerKafkaConsumer<String, String> c = new KafkaConsumer<>(props);c.subscribe(List.of("orders"));while (true) { ConsumerRecords<String, String> recs = c.poll(Duration.ofMillis(500)); for (var r : recs) handle(r); c.commitSync();}
3. Implementing Message Acknowledgments
Mode
Semantics
Auto-ack
Acked on receive; risk of loss
Manual ack after process
At-least-once
Negative ack (nack)
Reject + requeue / DLQ
Cumulative ack
Single ack covers offset range (Kafka)
4. Implementing Exactly-Once Semantics
Mechanism
System
Idempotent producer + transactions
Kafka EOS (read-process-write)
Dedup at consumer (idempotency key)
App layer
Two-phase commit with sink
Flink, Beam
Outbox pattern
DB tx writes msg + state
Warning: Pure exactly-once across boundaries is impossible (Two Generals); achieve "effectively once" via idempotency + dedup.