Scrivio Interview library
CodingIntermediate6 min read · updated 13 November 2026

A Flink job reads the Kafka topic orders, enriches each record and writes to orders-enriched, and must not produce duplicates on failure.

Show the sink configuration, explain how Flink achieves exactly-once with Kafka, and explain the startup error: The transaction timeout is larger than the maximum value allowed by the broker (as configured by transaction.max.timeout.ms).

Configure a KafkaSink with DeliveryGuarantee.EXACTLY_ONCE, a transactional id prefix unique to the job, and checkpointing enabled. The sink writes each checkpoint period's records in a Kafka transaction and commits it only after the checkpoint completes, a two-phase commit, so read_committed consumers see each record once. The startup error appears because the Flink sink defaults the producer's transaction.timeout.ms to one hour while brokers cap it at transaction.max.timeout.ms, 15 minutes by default.

Why this question is asked

Flink's exactly-once delivery to Kafka is one of its main selling points, and also one of the most frequently misconfigured: the job either fails at startup with this timeout error, or runs with settings that lose output after a long outage. Interviewers ask this coding question to see whether we can write the sink correctly and explain the mechanism well enough to choose its timeouts. In this answer, we will write the job's source and sink, follow a checkpoint through the two-phase commit, explain and fix the timeout error, and cover what consumers and operators must know.

1A Quick Recap of Checkpoints

Flink recovers from failure using checkpoints. Periodically the job manager injects a checkpoint barrier into the sources; as the barrier flows through the job, each operator saves its state, and the sources save their read positions, here the Kafka offsets of orders. When every operator has saved its state, the checkpoint is complete. After a failure, Flink restarts all operators from the last complete checkpoint, so the state and the read positions are consistent with each other.

That gives exactly-once state inside Flink. Output to Kafka is the remaining piece: records written after the last checkpoint would be written again after a restart. Kafka transactions close that gap.

2Writing the Source and the Sink

Here is the job, with checkpointing every 30 seconds and an exactly-once sink.

Java
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.enableCheckpointing(30_000, CheckpointingMode.EXACTLY_ONCE); // every 30 s
KafkaSource<Order> source = KafkaSource.<Order>builder()
.setBootstrapServers(servers)
.setTopics("orders")
.setGroupId("orders-enricher")
.setStartingOffsets(OffsetsInitializer.committedOffsets(OffsetResetStrategy.EARLIEST))
.setValueOnlyDeserializer(orderSchema)
.build();
KafkaSink<EnrichedOrder> sink = KafkaSink.<EnrichedOrder>builder()
.setBootstrapServers(servers)
.setRecordSerializer(KafkaRecordSerializationSchema.builder()
.setTopic("orders-enriched")
.setValueSerializationSchema(enrichedSchema)
.build())
.setDeliveryGuarantee(DeliveryGuarantee.EXACTLY_ONCE)
.setTransactionalIdPrefix("orders-enricher") // unique per job on the cluster
.setProperty("transaction.timeout.ms", "600000") // 10 min: below the broker cap
.build();
env.fromSource(source, WatermarkStrategy.noWatermarks(), "orders")
.map(this::enrich)
.sinkTo(sink);
env.execute("orders-enricher");

Without checkpointing, an EXACTLY_ONCE sink never commits, so nothing ever becomes visible to read_committed consumers; enabling checkpointing is part of the sink configuration, not an option. The transactional id prefix is how Flink names the producers' transactional ids; two jobs on the same cluster with the same prefix would fence each other's producers, so it must be unique per job.

3How the Two-Phase Commit Works

The sink opens a Kafka transaction and writes every record into it as records arrive. When the checkpoint barrier reaches the sink, it flushes the producer, which is the pre-commit or first phase, stores the transaction's identity in its checkpointed state, and starts a new transaction for the next records. Only when the job manager reports that the whole checkpoint is complete does the sink commit the earlier transaction, the second phase.

If the job fails before the checkpoint completes, Flink restores state and source offsets from the previous checkpoint and aborts the open transactions, so the records written since then are discarded for read_committed readers and are produced again by the replay. If it fails after the checkpoint completed but before the commit went through, the restored state still holds the transaction's identity and the sink commits it on recovery. Either way, each record is visible once.

Step throughOne checkpoint and its Kafka transaction
Job managerSourceKafkaSinkKafka
inject checkpoint barrier 42
save orders offsets in checkpoint
barrier 42 follows the records
flush transaction T42 (pre-commit)
state saved: T42 pending
begin T43 for the next records
checkpoint 42 complete
commit T42: records visible to read_committed

8 messages in order. Play them, or step through them one at a time.

4Why the Job Fails at Startup

The error says the transaction timeout is larger than the broker allows. The Flink Kafka sink sets the producer's transaction.timeout.ms to one hour by default, so that transactions survive long checkpoints and restarts. The broker refuses any transactional producer whose timeout exceeds transaction.max.timeout.ms, which defaults to 900,000 ms, 15 minutes. So the producer's initialisation is rejected and the job fails before processing anything.

There are two fixes. We can lower the sink's transaction.timeout.ms, as in the code above, to a value below the broker's cap but above the checkpoint interval plus the time a restart realistically takes; with 30-second checkpoints, 10 minutes leaves room. Or the platform team can raise the broker's cap if long timeouts are needed. The trade-off is real: an open transaction blocks read_committed consumers of that partition until it ends, so a very long timeout means a crashed producer can stall consumers for that long.

5What Happens During a Long Outage

There is a second, quieter consequence of the timeout. If the job is down for longer than transaction.timeout.ms, for example 40 minutes with a 10-minute timeout, the broker aborts the pending transaction, including records from a checkpoint that already completed but was not yet committed. When the job recovers, it tries to commit that transaction and cannot; those records are lost, because the source offsets in the checkpoint have already moved past them. So the timeout must be sized for realistic outages, not just for the happy path, and long maintenance windows need a savepoint taken while the job is healthy.

6What Consumers and Operators Should Know

Consumers of orders-enriched must use isolation.level=read_committed, or they will read records from transactions that are later aborted. They also see results roughly one checkpoint interval later than they were produced, because records become visible only at commit; shorter checkpoints mean lower latency and more overhead. Finally, the offsets the Kafka source commits to the consumer group are only for monitoring lag. Recovery always uses the offsets stored in Flink's checkpoint, so resetting the group's offsets with Kafka tools does not change where a restored job starts.

7Follow-up questions

Why does a Kafka source with idle partitions need watermark idleness?

In event time, an operator's watermark is the minimum of the watermarks of its inputs. A partition that receives no data never advances its watermark, so it holds the minimum back and windows downstream never close. Declaring idleness, for example WatermarkStrategy.forBoundedOutOfOrderness(...).withIdleness(Duration.ofMinutes(1)), lets Flink ignore a partition that has been silent for a minute until it receives data again.

Can two Flink jobs write to the same topic with exactly-once?

Yes, as long as each has its own transactional id prefix. Their transactions are independent, and read_committed consumers see both jobs' committed records.

Is AT_LEAST_ONCE a reasonable choice instead?

If consumers are idempotent, often yes. It avoids transactions, gives lower latency because records are visible immediately, and has no timeout pitfalls, at the cost of duplicates after a failure.

8Conclusion

In this answer, we wrote a Flink job with a KafkaSource and a KafkaSink set to EXACTLY_ONCE with a unique transactional id prefix and checkpointing enabled. The sink writes each checkpoint period's records in a Kafka transaction and commits it after the checkpoint completes, aborting open transactions on failure. The startup error comes from the sink's default one-hour transaction timeout exceeding the broker's 15-minute transaction.max.timeout.ms, so we set it between the checkpoint plus recovery time and the cap, sized for realistic outages. Consumers need read_committed, see results about one checkpoint later, and committed source offsets serve only monitoring.

What to say in the interviewAbout 90 seconds aloud

Open with

I would build a KafkaSink with setDeliveryGuarantee EXACTLY_ONCE, a record serializer that sets the topic orders-enriched and the value schema, and a transactional id prefix that is unique to this job on the cluster, and I would enable checkpointing with env.enableCheckpointing, because the sink only commits on checkpoints.

Then, if they want more

Exactly-once works as a two-phase commit. The sink writes records into a Kafka transaction; when the checkpoint barrier arrives, it flushes the transaction and records it in the checkpoint, then starts a new one. Once the job manager says the checkpoint is complete, it commits the transaction.

If the job fails, Flink restores state and source offsets from the last checkpoint and aborts the open transactions, so read_committed consumers see each record once. The startup error happens because the Flink sink defaults the producer's transaction.timeout.ms to one hour, while the broker caps it with transaction.max.timeout.ms, 15 minutes by default, so the producer is rejected.

I would set the sink's transaction.timeout.ms below the broker cap but above the checkpoint interval plus expected recovery time, or raise the broker cap. If the job is down longer than that timeout, the broker aborts pending transactions and that output is lost, so I size it for realistic outages.

Consumers see results about one checkpoint interval later, and the offsets the source commits to Kafka are only for monitoring, since recovery uses the checkpoint.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Show the sink configuration, explain how Flink achieves exactly-once with Kafka, and...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/383a65e63d/