0
0
Kafkadevops~7 mins

Transactional producer in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to send multiple messages to Kafka as a single group that either all succeed or all fail. A transactional producer helps you do this by making sure messages are sent together or not at all, avoiding partial updates.
When you need to update multiple Kafka topics atomically to keep data consistent.
When you want to ensure that a batch of messages is fully processed or fully discarded.
When your application requires exactly-once message delivery to avoid duplicates.
When you are implementing financial transactions or inventory updates that must not be partial.
When you want to combine producing messages with consuming and processing them in one atomic step.
Config File - producer.properties
producer.properties
bootstrap.servers=localhost:9092
key.serializer=org.apache.kafka.common.serialization.StringSerializer
value.serializer=org.apache.kafka.common.serialization.StringSerializer
transactional.id=my-transactional-producer-1
enable.idempotence=true
acks=all

This configuration file sets up the Kafka producer for transactions.

  • bootstrap.servers: Kafka server address.
  • key.serializer and value.serializer: How keys and values are converted to bytes.
  • transactional.id: Unique ID to enable transactions for this producer.
  • enable.idempotence: Ensures messages are not duplicated.
  • acks=all: Waits for all replicas to confirm message receipt for safety.
Commands
Starts a Kafka producer with transactions enabled using the transactional ID. This allows sending messages as part of a transaction.
Terminal
kafka-console-producer --broker-list localhost:9092 --topic example-topic --producer-property transactional.id=my-transactional-producer-1
Expected OutputExpected
No output (command runs silently)
--broker-list - Specifies Kafka server addresses to connect.
--topic - Specifies the topic to send messages to.
--producer-property transactional.id=my-transactional-producer-1 - Enables transactional support with a unique ID.
Sends 5 messages of 10 bytes each to the topic using a transactional producer to test transactional message sending.
Terminal
kafka-producer-perf-test --topic example-topic --num-records 5 --record-size 10 --throughput 1 --producer-props bootstrap.servers=localhost:9092 transactional.id=my-transactional-producer-1 enable.idempotence=true acks=all
Expected OutputExpected
5 records sent, 5 records acknowledged, 0 records failed 1 records/sec (approximate throughput)
--topic - Topic to send messages to.
--num-records - Number of messages to send.
--producer-props - Properties to configure the producer including transactional settings.
Checks the consumer group status to verify if messages sent transactionally are available for consumption.
Terminal
kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group example-group
Expected OutputExpected
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID example-topic 0 5 5 0 consumer-1 /127.0.0.1 consumer-1
--bootstrap-server - Kafka server address.
--describe - Shows detailed info about the consumer group.
--group - Specifies the consumer group to check.
Key Concept

If you remember nothing else from this pattern, remember: transactional producers let you send multiple messages as one all-or-nothing group to keep data consistent.

Common Mistakes
Not setting a unique transactional.id in the producer configuration.
Without a transactional ID, Kafka cannot track transactions, so messages won't be sent atomically.
Always set a unique transactional.id property for each transactional producer instance.
Not enabling idempotence (enable.idempotence=true) when using transactions.
Idempotence prevents duplicate messages; without it, exactly-once delivery is not guaranteed.
Set enable.idempotence=true in the producer configuration to ensure safe retries.
Starting to send messages without initializing transactions in the producer code.
Messages sent outside a transaction are not part of the atomic group, defeating the purpose.
Always call initTransactions() and beginTransaction() before sending messages in transactional mode.
Summary
Configure the Kafka producer with a unique transactional.id and enable idempotence.
Start the producer with transactional support to send messages as a single atomic group.
Verify message delivery and consumer group status to ensure transactional messages are processed.