Exactly-Once Semantics: Transactions, Idempotent Producer
Difficulty: Staff | Asked at: Google, Confluent, Uber, Stripe
âšī¸Interview Context
Exactly-once semantics (EOS) is a cornerstone of modern data systems. This question tests your understanding of Kafka's transactional model and how to achieve true exactly-once delivery across producers and consumers.
The Question
How does Apache Kafka achieve exactly-once semantics? Explain the role of idempotent producers, the transactional API, and how end-to-end exactly-once delivery works. What are the limitations?
Exactly-Once Delivery Semantics
Exactly-once means the effect of processing each record appears exactly once, which is not the same as the record being delivered exactly once. Delivery guarantees describe the channel: at-most-once drops records when something fails, at-least-once repeats them when something fails, and at-least-once plus deduplication on the receiving side is what most production systems actually run. Kafka's claim is narrower and more useful than a delivery guarantee: inside the cluster, writes can be made idempotent and atomic, so a record lands in its partition once and a batch of records lands across several partitions together. What the cluster cannot do is reach into your database, your HTTP client or your object store and make those atomic with the offset commit. Keep that boundary visible and the rest of this lesson becomes mechanics rather than magic.
Definition
- At-Least-Once: Message delivered at least once (retry-safe)
- At-Most-Once: Message delivered at most once (no duplicates)
- Exactly-Once: Message delivered exactly once (no duplicates, no loss)
Kafka's Three Levels of Delivery Guarantees
# Level 1: At-Least-Once (default)
producer = KafkaProducer(
acks='all',
retries=3
# No idempotence â duplicates possible on retry
)
# Level 2: At-Most-Once
producer = KafkaProducer(
acks='0',
retries=0
# No retry â messages may be lost
)
# Level 3: Exactly-Once (requires idempotence)
producer = KafkaProducer(
enable_idempotence=True,
acks='all',
retries=2147483647
# Duplicates prevented by sequence numbers
)
â ī¸Key Distinction
Kafka's exactly-once is NOT about exactly-once delivery to the consumer. It's about exactly-once processing within the Kafka ecosystem. The consumer still sees messages at-least-once unless using transactions with read_committed isolation.
Idempotent Producer
Idempotence is the cheap half of exactly-once, and it covers the most common duplicate source: a producer retry after the broker already appended the record. Each producer instance is assigned a producer id by the broker on startup and stamps every record with a per-partition sequence number. The broker tracks the last accepted sequence for each pair of producer id and partition, and rejects anything older or already seen while returning success instead of a second copy. Because that bookkeeping is per partition, ordering is preserved only inside a partition, and because the producer id belongs to one instance, two application instances writing identical data are not deduplicated against each other.
Sequence Number Protocol
Each producer gets a unique Producer ID (PID) and assigns monotonically increasing sequence numbers to each message:
from kafka import KafkaProducer
from kafka.errors import DuplicateSequenceError
class IdempotentProducer:
def __init__(self):
self.producer = KafkaProducer(
bootstrap_servers='localhost:9092',
enable_idempotence=True,
acks='all',
retries=2147483647,
max_in_flight_requests_per_connection=5,
delivery_timeout_ms=120000
)
self.sequence_numbers = {} # partition -> seq_num
def send(self, topic, key, value):
"""
Send message with automatic deduplication.
If network error occurs:
1. Producer retries the message
2. Broker receives duplicate (same PID + Partition + SeqNum)
3. Broker detects duplicate, returns success without appending
4. Consumer sees message exactly once
"""
future = self.producer.send(topic, key=key, value=value)
try:
metadata = future.get(timeout=30)
return metadata
except DuplicateSequenceError:
# Message was already committed
# This is safe - idempotency working as expected
return None
def send_batch(self, topic, messages):
"""
Send multiple messages with idempotency.
Sequence numbers are assigned per partition automatically.
"""
futures = []
for key, value in messages:
future = self.producer.send(topic, key=key, value=value)
futures.append(future)
# Wait for all acknowledgments
results = []
for future in futures:
try:
metadata = future.get(timeout=30)
results.append(metadata)
except DuplicateSequenceError:
results.append(None)
return results
Idempotent Producer Limitations
Limitations:
1. Per-partition ordering only
- Sequences are per (PID, Partition) pair
- Cross-partition ordering NOT guaranteed
2. Max 5 in-flight requests (with idempotence)
- Without idempotence: unlimited
- With idempotence: max 5 for sequence ordering
3. Producer restart resets sequences
- New PID assigned on restart
- Old PID's sequences expire after retention
4. No cross-producer deduplication
- Each producer has unique PID
- Two producers can send same data (different PIDs)
5. Single transactional.id required for transactions
- Multiple producers cannot share transactional ID
from kafka import KafkaProducer, KafkaConsumer
from kafka.errors import ProducerFenced
class TransactionalProducer:
def __init__(self, transactional_id):
self.producer = KafkaProducer(
bootstrap_servers='localhost:9092',
# Transactional configuration
transactional_id=transactional_id,
enable_idempotence=True,
acks='all',
retries=2147483647,
# Performance
batch_size=32768,
linger_ms=10,
compression_type='lz4'
)
# Initialize transaction
self.producer.init_transactions()
def produce_atomically(self, topic, messages):
"""
Produce messages atomically.
Either ALL messages commit or NONE do.
"""
try:
self.producer.begin_transaction()
for key, value in messages:
self.producer.send(topic, key=key, value=value)
self.producer.commit_transaction()
return True
except ProducerFenced as e:
# Another producer with same transactional.id started
# This producer is now fenced (invalidated)
self.producer.close()
raise
except Exception as e:
# Any error â abort transaction
self.producer.abort_transaction()
raise
# Usage
txn_producer = TransactionalProducer('my-app-txn-1')
txn_producer.produce_atomically('events', [
(b'user1', b'login'),
(b'user2', b'logout'),
(b'user3', b'purchase')
])
Transaction Coordinator Protocol
Transaction Coordinator (runs on a broker):
1. Transaction Registration:
- Producer calls initTransactions()
- Coordinator maps transactional.id â producer PID
- If existing PID exists, it's fenced (invalidated)
2. Transaction Begin:
- Producer calls beginTransaction()
- Coordinator creates transaction metadata
- State: ONGOING
3. Message Production:
- Producer sends messages to partition leaders
- Leaders forward to coordinator for transaction logging
- Messages are buffered, not visible to consumers
4. Transaction Commit:
- Producer calls commitTransaction()
- Coordinator writes COMMIT marker to transaction log
- Partition leaders apply COMMIT to their logs
- Messages become visible to read_committed consumers
5. Transaction Abort:
- Producer calls abortTransaction()
- Coordinator writes ABORT marker
- Messages are discarded (never visible)
â ī¸Transaction Timeout
Transactions have a default timeout of 60 seconds (transaction.timeout.ms). If a producer crashes during a transaction, the coordinator will abort it after the timeout. Ensure your transaction processing completes within this window.
Exactly-Once Consumer (read_committed)
from kafka import KafkaConsumer
# Consumer with read_committed isolation
consumer = KafkaConsumer(
'events',
group_id='exactly-once-consumer',
bootstrap_servers='localhost:9092',
# Critical for exactly-once
isolation_level='read_committed',
# Manual offset commit for control
enable_auto_commit=False,
# Read from beginning for replay
auto_offset_reset='earliest'
)
def consume_exactly_once():
"""
Consume messages with exactly-once semantics.
Messages are only visible after transaction commit.
Aborted messages are never seen by consumer.
"""
while True:
messages = consumer.poll(timeout_ms=1000)
for topic_partition, records in messages.items():
for record in records:
# record.value is the actual message
# Messages from aborted transactions are filtered out
process_message(record)
# Commit offset after processing
consumer.commit()
Transactional Consumer-Producer (Exactly-Once Pipeline)
class ExactlyOncePipeline:
"""
Consumer-Producer pipeline with exactly-once semantics.
Reads from input topic, processes, writes to output topic.
"""
def __init__(self, input_topic, output_topic, transactional_id):
# Consumer reads from input
self.consumer = KafkaConsumer(
input_topic,
group_id='exactly-once-pipeline',
bootstrap_servers='localhost:9092',
isolation_level='read_committed',
enable_auto_commit=False,
auto_offset_reset='earliest'
)
# Producer writes to output with transactions
self.producer = KafkaProducer(
bootstrap_servers='localhost:9092',
transactional_id=transactional_id,
enable_idempotence=True,
acks='all'
)
self.producer.init_transactions()
def process_batch(self, max_messages=100):
"""
Process a batch of messages atomically.
Either:
- All messages processed and offsets committed
- Nothing processed, offsets not committed
"""
try:
self.producer.begin_transaction()
messages_processed = 0
offsets_to_commit = {}
# Read messages
messages = self.consumer.poll(timeout_ms=1000)
for topic_partition, records in messages.items():
for record in records:
# Transform message
transformed = self.transform(record.value)
# Send to output topic
self.producer.send(
'output-topic',
key=record.key,
value=transformed
)
messages_processed += 1
# Track offset
offsets_to_commit[topic_partition] = (
record.offset + 1 # Next offset to read
)
if messages_processed == 0:
self.producer.abort_transaction()
return 0
# Commit consumer offsets as part of transaction
self.producer.send_offsets_to_transaction(
offsets_to_commit,
self.consumer.group_metadata()
)
# Commit everything atomically
self.producer.commit_transaction()
return messages_processed
except Exception as e:
self.producer.abort_transaction()
raise
def transform(self, message):
"""Transform message (your business logic)"""
return message.upper() # Example transformation
âšī¸Critical Pattern
send_offsets_to_transaction() is the key to end-to-end exactly-once. It atomically commits both the produced messages AND the consumer offsets. If either fails, both are rolled back.
Transaction Isolation Levels
# Three isolation levels for consumers
# 1. read_uncommitted (default)
# Sees ALL messages, including uncommitted and aborted
consumer = KafkaConsumer(
'topic',
isolation_level='read_uncommitted'
)
# 2. read_committed
# Only sees committed messages
# Aborted messages filtered out
consumer = KafkaConsumer(
'topic',
isolation_level='read_committed'
)
# 3. read_committed with consumer
# Sees messages in transaction order
# Guarantees: committed messages appear in order
# Aborted messages are skipped (not visible)
consumer = KafkaConsumer(
'topic',
isolation_level='read_committed'
)
Producer Fencing
from kafka.errors import ProducerFenced
class FencedProducerHandler:
"""
Handle producer fencing gracefully.
Fencing occurs when:
1. Producer A starts transaction with transactional.id="app-1"
2. Producer A crashes
3. Producer B starts with same transactional.id="app-1"
4. Producer A recovers and tries to commit
5. Coordinator fences Producer A (invalidates it)
6. Producer A gets ProducerFenced exception
"""
def __init__(self, transactional_id):
self.transactional_id = transactional_id
self.producer = None
self._initialize_producer()
def _initialize_producer(self):
self.producer = KafkaProducer(
bootstrap_servers='localhost:9092',
transactional_id=self.transactional_id,
enable_idempotence=True,
acks='all',
retries=2147483647
)
self.producer.init_transactions()
def produce_with_fencing_handling(self, topic, messages):
"""
Produce messages with fencing detection.
On ProducerFenced:
1. Close old producer
2. Create new producer (gets new PID)
3. Retry operation
"""
max_retries = 3
for attempt in range(max_retries):
try:
self.producer.begin_transaction()
for key, value in messages:
self.producer.send(topic, key=key, value=value)
self.producer.commit_transaction()
return True
except ProducerFenced:
log.warning("Producer fenced, creating new producer")
self.producer.close()
self._initialize_producer()
except Exception as e:
self.producer.abort_transaction()
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt)
return False
Transaction Timeout Handling
import time
from kafka import KafkaProducer
class TransactionWithTimeout:
"""
Handle long-running transactions with timeout management.
"""
def __init__(self, transactional_id, timeout_ms=60000):
self.producer = KafkaProducer(
bootstrap_servers='localhost:9092',
transactional_id=transactional_id,
transaction_timeout_ms=timeout_ms,
enable_idempotence=True,
acks='all'
)
self.producer.init_transactions()
self.transaction_timeout = timeout_ms
def produce_with_chunking(self, topic, large_dataset):
"""
Chunk large datasets to fit within transaction timeout.
If processing takes longer than transaction timeout:
1. Commit partial batch
2. Start new transaction
3. Continue processing
"""
chunk_size = 1000
total_committed = 0
start_time = time.time()
for i in range(0, len(large_dataset), chunk_size):
# Check if we're approaching timeout
elapsed = (time.time() - start_time) * 1000
if elapsed > self.transaction_timeout * 0.8:
# Commit current batch
self.producer.commit_transaction()
total_committed += i
# Start new transaction
self.producer.begin_transaction()
start_time = time.time()
# Process chunk
chunk = large_dataset[i:i + chunk_size]
for record in chunk:
self.producer.send(topic, value=record)
# Final commit
self.producer.commit_transaction()
return total_committed + len(large_dataset)
â ī¸Transaction Timeout Warning
Set transaction.timeout.ms high enough for your processing but low enough to detect dead producers. Default 60s works for most cases. For batch processing, increase to 300s or use chunking.
Exactly-Once Formula
def calculate_exactly_once_overhead(
message_rate_per_sec: int,
avg_message_size: int,
batch_size: int,
coordinator_rtt_ms: float
) -> dict:
"""
Calculate overhead of exactly-once semantics.
"""
# Sequence number overhead (8 bytes per message)
seq_overhead_per_msg = 8 # bytes
seq_overhead_per_sec = seq_overhead_per_msg * message_rate_per_sec
# Batch overhead
batches_per_sec = message_rate_per_sec / (batch_size / avg_message_size)
# Transaction overhead (1 RTT per transaction)
transaction_overhead_per_sec = batches_per_sec * coordinator_rtt_ms / 1000
# Total overhead
total_overhead_bytes = seq_overhead_per_sec + transaction_overhead_per_sec * avg_message_size
return {
'sequence_overhead_mbps': seq_overhead_per_sec / (1024 * 1024),
'transaction_overhead_mbps': transaction_overhead_per_sec * avg_message_size / (1024 * 1024),
'total_overhead_percentage': (total_overhead_bytes / (message_rate_per_sec * avg_message_size)) * 100
}
# Example
overhead = calculate_exactly_once_overhead(
message_rate_per_sec=100000,
avg_message_size=500,
batch_size=32768,
coordinator_rtt_ms=10
)
print(f"Sequence overhead: {overhead['sequence_overhead_mbps']:.2f} MB/s")
print(f"Transaction overhead: {overhead['transaction_overhead_mbps']:.2f} MB/s")
print(f"Total overhead: {overhead['total_overhead_percentage']:.2f}%")
Common Exactly-Once Patterns
Pattern 1: Kafka â Kafka Exactly-Once
Producer: Transactional with send_offsets_to_transaction()
Consumer: read_committed isolation
Result: Messages processed exactly once
Pattern 2: Kafka â Database Exactly-Once
Producer: Transactional to Kafka
Consumer: Process and write to DB
Offset commit: In same DB transaction (dual write problem)
Solution: Use database transaction to atomically write + commit offset
Pattern 3: Database â Kafka Exactly-Once
Source: CDC (Change Data Capture) with transactions
Kafka: Transactional producer
Result: Each DB change appears exactly once in Kafka
Pattern 4: Exactly-Once Aggregation
Input: Kafka topic with events
Processing: Kafka Streams with EOS
Output: Kafka topic with aggregated results
Result: Exactly-once aggregation
âšī¸Production Tip
For most use cases, at-least-once with idempotent consumer processing is sufficient. Use exactly-once transactions only when:
- Duplicate processing is expensive (e.g., financial transactions)
- You need atomic multi-topic writes
- You need consumer-producer pipeline atomicity
Exactly-Once vs At-Least-Once Comparison
| Aspect | At-Least-Once | Exactly-Once |
|---|---|---|
| Duplicates | Possible | Eliminated |
| Throughput | Higher | Lower (5-15%) |
| Latency | Lower | Higher |
| Complexity | Simple | Complex |
| Consumer Logic | Simple retry | Transaction-aware |
| Use Cases | Most applications | Financial, critical |
â ī¸Key Insight
Kafka's exactly-once is a system-level guarantee, not an end-to-end guarantee across external systems. If your consumer writes to a database and crashes after processing but before committing offsets, you'll process duplicates. True end-to-end exactly-once requires careful system design.
What the transaction coordinator actually commits
Transactions add atomicity across partitions and across topics, coordinated by the broker that owns the transaction coordinator for a given transactional.id. On initTransactions() the coordinator assigns a producer id and fences any previous producer holding that same transactional id, which is exactly how a zombie left over from a previous deploy is stopped from committing. During beginTransaction() the producer appends records normally, but each leader also writes control markers into the partition, so the records already exist in the log before anybody can observe them. commitTransaction() asks the coordinator to write commit markers, after which every partition leader flips its markers and read_committed consumers are allowed to advance past those offsets. abortTransaction() writes abort markers instead, consumers skip the range, and the records never become visible even though they occupy log space until retention clears them.
Where exactly-once stops
Three limits are worth naming before an interviewer has to ask. First, the guarantee is scoped to the Kafka cluster: a consumer that reads a record, writes two rows to Postgres, and crashes before committing the offset will read that record again, and the duplicate write belongs to you. Second, transactions carry a deadline, transaction.timeout.ms at 60 seconds by default, after which the coordinator aborts an orphaned transaction, so any workflow longer than that has to be chunked into several commits. Third, throughput pays for the guarantee: expect single-digit to low double-digit percentage losses from sequence bookkeeping, extra round trips for commit markers, and lost pipelining while a transaction is still open. Anything a consumer does outside Kafka must be made idempotent or recorded in the same transaction as the offset.
Choosing a guarantee for a real system
A practical decision starts from the cost of a duplicate rather than from a preference for purity. If a duplicate would merely increment a counter or add a second audit row, at-least-once with an idempotent sink is the right answer, because it is simpler to build and cheaper to run. If a duplicate would move money, oversell inventory, or corrupt an aggregate that cannot be recomputed, use transactions and set the consumer to read_committed, because retries then become invisible. For Kafka-to-Kafka pipelines the wiring is transactional.id plus send_offsets_to_transaction(); for Kafka-to-database the more reliable pattern is often to write the row and the offset inside one database transaction while leaving Kafka at at-least-once. State the rule you are choosing and the failure it protects against, and it is clear you have made this call before.
Common Mistakes
Assuming enable_idempotence alone makes the whole pipeline exactly-once
Fix: Idempotence only stops producer retries; use transactions with send_offsets_to_transaction and read_committed consumers for atomic pipelines
Leaving consumers on read_uncommitted while producing transactionally
Fix: Set isolation_level to read_committed so aborted records are filtered and uncommitted ones stay invisible
Running a transaction longer than transaction.timeout.ms so the coordinator aborts committed work
Fix: Chunk long jobs into several transactions and commit each one before the timeout, or raise the timeout deliberately
Key Takeaways
- Kafka's exactly-once applies inside the cluster: idempotent writes remove producer-retry duplicates and transactions make multi-partition writes atomic
- The idempotent producer stamps each record with a producer id and per-partition sequence, and the broker drops anything it has already accepted
- A transactional.id fences out stale producer instances, and commit markers are what make records visible to read_committed consumers
- External sinks still need idempotency or an offset commit inside the same transaction, because Kafka cannot make a database write atomic with a commit