Fault Tolerance in Stream Processing

How a stream processor survives crashes without losing or double-counting events: microbatches, checkpoints with barriers, transactional and idempotent outputs, and what exactly-once really promises.

Overview

A batch job that fails can simply run again: its input is fixed and its output is written only at the end. A stream job never ends. It holds state (counts, windows, join tables) and keeps sending results to other systems while it runs. When a machine crashes halfway, the processor must answer two questions: which events had already been counted, and which results had already gone out?

The goal is usually called exactly-once. It does not mean each event is processed only once; after a crash some events are processed again. It means the visible effect is as if each event had been processed exactly once: nothing lost, nothing counted twice. Some people say effectively-once for that reason.

There are three guarantees, named by what can happen after a crash.

  • At-most-once: some events are lost. You get it by acknowledging input before processing, with no replay.
  • At-least-once: some events affect the output twice. You get it by replaying input from the last saved position.
  • Exactly-once (effect): neither happens. You get it by replaying, plus making state and output atomic or idempotent.

The rest of this chapter is about the third: how engines replay safely, and how the output keeps its effect single.

Microbatching: reuse batch recovery

One approach cuts the stream into small batches and runs each like a tiny batch job. Spark Structured Streaming works this way by default. Every source must have offsets, like Kafka offsets, so the engine can re-read any range. Before each batch it records the offset range it is about to process in a checkpoint and write-ahead log. If a batch fails, the engine re-runs exactly that range. A sink that is idempotent absorbs the repeat, and Spark's guide says replayable sources plus idempotent sinks give end-to-end exactly-once. Not every sink qualifies: Spark's Kafka sink, for example, is documented as at-least-once, so readers of that topic must deduplicate.

Take batch 12, covering Kafka offsets 1000 to 1999, and follow it through a failure.

  • The engine writes 'batch 12 = 1000 to 1999' to the checkpoint and write-ahead log before it runs anything.
  • It starts batch 12, which writes results to the idempotent sink as it goes.
  • The batch fails partway through.
  • The engine re-runs batch 12 over the same offsets, read from the log, and writes the results again.
  • The idempotent sink absorbs the repeat, so the output counts each event once.

The cost is latency: results appear once per batch, not per event.

Key concepts

  • Exactly-once (effect)
  • Microbatching
  • Checkpoint
  • Checkpoint barrier
  • Unaligned checkpoint
  • Two-phase commit sink
  • Idempotent write
  • read_committed

Interview questions

  • How does Flink achieve exactly-once end to end with Kafka as both source and sink?
  • Would you use at-least-once or exactly-once for a pipeline that charges customers for usage?
  • How do you choose a checkpoint interval?
  • Your job has exactly-once enabled, yet customers got duplicate emails after a restart. Why?

Sign up free to read the full topic

More DDIA topics

Start free on Cariara · Coding problems · System design studio