0
0
Kafkadevops~10 mins

Exactly-once semantics (EOS) in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Exactly-once semantics (EOS)
Start Producer
Begin Transaction
Send Messages
Commit Transaction
Broker stores messages atomically
Consumer reads messages once
End
The producer starts a transaction, sends messages, commits atomically, ensuring messages are stored and consumed exactly once.
Execution Sample
Kafka
producer.initTransactions();
producer.beginTransaction();
producer.send(record1);
producer.send(record2);
producer.commitTransaction();
This code shows a Kafka producer sending two messages within a transaction to guarantee exactly-once delivery.
Process Table
StepActionState ChangeResult
1Initialize transactionsProducer ready for transactionsNo messages sent yet
2Begin transactionTransaction startedMessages will be sent atomically
3Send record1Record1 buffered in transactionNot visible to consumers yet
4Send record2Record2 buffered in transactionNot visible to consumers yet
5Commit transactionTransaction committed atomicallyBoth records become visible exactly once
6Consumer reads messagesReads record1 and record2 onceNo duplicates or missing messages
7EndTransaction completeExactly-once semantics ensured
💡 Transaction committed, messages stored atomically, ensuring exactly-once delivery
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
transactionStatenonestartedstartedstartedcommittedcommitted
bufferedMessages[][][record1][record1, record2][][]
messagesVisibleToConsumer[][][][][record1, record2][record1, record2]
Key Moments - 3 Insights
Why are messages not visible to consumers before commit?
Because messages are buffered in the transaction (see steps 3 and 4 in execution_table), they become visible only after commit (step 5), ensuring atomic visibility.
What happens if the transaction is aborted instead of committed?
If aborted, buffered messages are discarded and never become visible, preventing duplicates or partial writes (not shown in this trace but important for EOS).
How does exactly-once semantics prevent duplicates?
By committing messages atomically and tracking offsets, Kafka ensures consumers read each message once after commit (step 6), avoiding duplicates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of bufferedMessages after step 4?
A[]
B[record1]
C[record1, record2]
D[record2]
💡 Hint
Check the 'bufferedMessages' variable in variable_tracker after step 4
At which step do messages become visible to consumers?
AStep 3
BStep 5
CStep 2
DStep 4
💡 Hint
Look at 'messagesVisibleToConsumer' in variable_tracker and execution_table rows
If the transaction was aborted instead of committed at step 5, what would happen to bufferedMessages?
AThey would be discarded and not sent
BThey would be sent to consumers immediately
CThey would remain buffered indefinitely
DThey would be duplicated
💡 Hint
Recall the explanation in key_moments about aborting transactions
Concept Snapshot
Exactly-once semantics (EOS) in Kafka:
- Producer starts a transaction
- Sends messages buffered in transaction
- Commits transaction atomically
- Messages become visible once committed
- Consumers read messages exactly once
- Prevents duplicates and data loss
Full Transcript
Exactly-once semantics (EOS) in Kafka means messages are sent and stored so that consumers see each message only once. The producer starts a transaction, sends messages buffered inside it, and commits the transaction atomically. Before commit, messages are not visible to consumers. After commit, all messages become visible together. This prevents duplicates and partial writes. If the transaction aborts, messages are discarded. Consumers read committed messages exactly once, ensuring reliable processing.