How do you guarantee that a payment event consumed from Kafka updates the ledger exactly once?

Short answer

Kafka delivers at least once to external systems, so make the consumer idempotent: store a processed-event key (or rely on a business unique constraint) in the same database transaction as the ledger update, commit offsets after the transaction, and route repeated failures to a dead-letter topic.

Detailed answer

@KafkaListener(topics = "payments.captured.v1", groupId = "ledger")
@Transactional
public void on(PaymentCaptured e) {
    int inserted = jdbc.update(
        "INSERT INTO processed_events(event_id) VALUES (?) ON CONFLICT DO NOTHING", e.eventId());
    if (inserted == 0) return;                       // duplicate: already applied
    ledger.post(e.paymentId(), e.amount());          // same DB transaction
}
// Offset committed after the listener method completes (AckMode RECORD/BATCH);
// a crash before commit redelivers, and the dedup table makes it harmless.

Kafka transactions (exactly_once_v2) only cover Kafka-to-Kafka read-process-write; database side effects need idempotency (inbox pattern).

What the interviewer expects

Understanding of where exactly-once holds and where idempotency is required.

Common mistakes to avoid

Believing enabling idempotent producers gives end-to-end exactly-once to a database.