title: Idempotent and Transactions
body: <p>I am exploring Transactions in Kafka, and I want to understand all the details.</p>
  
  <p>I noticed in Spring-Kafka that idempotent is enabled when you provide a transactionsalId.</p>
  
  <pre><code>public void setTransactionIdPrefix(String transactionIdPrefix) {
      Assert.notNull(transactionIdPrefix, "'transactionIdPrefix' cannot be null");
      this.transactionIdPrefix = transactionIdPrefix;
      enableIdempotentBehaviour();
  }
  </code></pre>
  
  <p>At first glance, I assume Spring-Kafka enabled idempotent in transactions because it is "good-to-have". I assumed it was to ensure to ensure exactly-once semantics in transactions.</p>
  
  <p>I did a bit more digging and discovered that idempotent is required for transactions to work. This is mentioned in <a href="https://cwiki.apache.org/confluence/display/KAFKA/KIP-98+-+Exactly+Once+Delivery+and+Transactional+Messaging" rel="nofollow noreferrer">KIP-98</a></p>
  
  <blockquote>
    <p>Note that enable.idempotence must be enabled if a TransactionalId is
    configured.</p>
  </blockquote>
  
  <p>Kafka idempotent is a feature to avoid duplicated messages, such as network errors after the message has been sent. </p>
  
  <p>My understanding is that, Kafka transactions basically writes to an internal topic and idempotent has to be enabled to avoid duplicates.</p>
  
  <p>Idempotent enables exactly-once semantics for producers.</p>
  
  <p>Transactions enables exactly-once semantics for transitivity; consume -> produce.</p>
  
  <p>Is my understanding correct?</p>
  
  <p>What enables exactly-once for only consumers? Committing offset, idempotent, or transactions.</p>