Apache Flink

Also known as: Flink

On this page 7

Apache Flink is an open-source, distributed engine for stateful computations over unbounded and bounded data streams. In streaming data architectures, Flink, often called simply Flink, reads events from sources such as Kafka, transforms, joins and aggregates them continuously, and writes results to databases, data lakes or other streams with low latency and exactly-once state consistency.

Flink treats batch processing as a special case of streaming, in which the stream happens to be finite. Its distinguishing strengths are first-class event-time processing with watermarks, large managed state and a checkpointing mechanism that lets long-running jobs recover from failures without losing or double-counting data.

A Flink application is a dataflow graph of sources, operators and sinks. The JobManager turns the program into a parallel execution plan and distributes tasks across TaskManagers, each of which provides a fixed number of task slots. Every operator runs with a configurable parallelism, and records are partitioned between operator instances by key, round robin or broadcast.

Unlike micro-batch engines, Flink processes each record as it arrives in a pipelined fashion, which yields latencies in the range of milliseconds to sub-second for typical jobs. When a downstream operator slows down, network buffers fill and backpressure propagates upstream, throttling sources instead of dropping data.

The main runtime components are listed below.

ComponentRoleNotes
JobManagerSchedules tasks, coordinates checkpointsMade highly available with a standby and coordination store
TaskManagerExecutes operator tasksProvides task slots, memory and network buffers
State backendStores keyed and operator stateHeap memory or embedded RocksDB
Checkpoint storageHolds durable state snapshotsDistributed file system or object storage
ConnectorsRead from sources and write to sinksKafka, Kinesis, Pulsar, JDBC, files
Core components of a Flink deployment

State, checkpoints and exactly-once semantics

Flink operators can hold keyed state, such as a running count per user, which Flink partitions, stores and restores automatically. With the RocksDB state backend, state can grow far beyond available memory, reaching terabytes in large deployments, because it spills to local disk.

Fault tolerance relies on distributed snapshots based on the Chandy-Lamport algorithm. The JobManager injects checkpoint barriers into the streams; when an operator has received the barrier on all inputs, it snapshots its state. After a failure, Flink restores the latest complete checkpoint and rewinds replayable sources such as Kafka to the recorded offsets.

This gives exactly-once state consistency inside Flink. End-to-end exactly-once delivery additionally requires a transactional or idempotent sink; Flink coordinates this with a two-phase commit protocol for sinks such as Kafka. Savepoints, which are manually triggered snapshots, allow jobs to be upgraded, rescaled or migrated without losing state.

ParameterTypical rangeTrade-off
Checkpoint intervalSeconds to a few minutesShorter means faster recovery but more overhead
Checkpointing modeExactly-once or at-least-onceAt-least-once lowers latency, allows duplicates
Unaligned checkpointsEnabled under heavy backpressureFaster snapshots, larger checkpoint size
Incremental checkpointsEnabled with RocksDBUploads only changed state files
Key checkpoint parameters

Event time, watermarks and windows

Flink distinguishes event time, when an event actually occurred, from processing time, when the engine sees it. Event-time processing produces correct, reproducible results even when events arrive late or out of order, which is common with mobile devices and distributed producers.

A watermark is a special marker asserting that no events older than a given timestamp are expected. A typical strategy sets the watermark to the maximum observed timestamp minus a bounded out-of-orderness allowance, often a few seconds. Windows close when the watermark passes their end; allowed lateness and side outputs handle events that arrive afterwards.

Flink supports tumbling, sliding and session windows, plus interval joins, temporal joins and complex event processing for pattern detection across sequences of events.

Flink is often evaluated against other stream processing options. The differences lie mainly in processing model, state handling and operational footprint.

TechnologyProcessing modelTypical fit
Apache FlinkRecord-at-a-time, stateful, event timeComplex, large-state, low-latency pipelines
Spark Structured StreamingMicro-batch, optional continuous modeTeams already standardized on Spark
Kafka StreamsLibrary embedded in applicationsKafka-to-Kafka processing without a cluster
Apache KafkaDurable log and message brokerTransport and storage, not computation
Amazon KinesisManaged stream ingestion serviceTransport on one cloud, often paired with Flink
Flink and neighboring streaming technologies

A frequent misconception is that Flink competes with Kafka. In practice they are complementary: Kafka stores and transports event streams, while Flink computes over them.

APIs, use cases and operational pitfalls

Flink offers layered APIs: the low-level ProcessFunction for full control of state and timers, the DataStream API in Java and Python, and the declarative Table API and Flink SQL, which let analysts define continuous queries. Typical use cases include fraud detection, real-time analytics, streaming ETL, alerting and continuously updated feature stores for machine learning.

  • Unbounded state growth occurs when keys never expire; configure state time-to-live.
  • Data skew on a hot key overloads one subtask; pre-aggregate or salt keys.
  • Idle sources can stall watermarks; enable idleness detection.
  • Long checkpoint durations under backpressure signal undersized resources or slow sinks.
  • Incompatible state schema changes break savepoint restores; assign stable operator identifiers.

Operating Flink well requires attention to memory configuration, checkpoint health and backpressure metrics, which is why many organizations run it through a Kubernetes operator or a managed cloud service.

Stripped to the essentials

Apache Flink is software that works on data while it is still flowing. Instead of waiting until the end of the day to count sales, it updates the count the moment each sale happens.

Think of a cashier who keeps a running total in a notebook and makes a safe copy every few minutes. If the cashier is interrupted, the latest copy lets the work resume without counting anything twice. Flink does the same for millions of events per second, which is why it is trusted for fast and accurate results.

Where it is applied

A payment processor handles card transactions that arrive through Kafka at peaks of tens of thousands per second. A Flink job keyed by card number keeps, for each card, the transaction count and amount over sliding ten-minute windows, the last known country and the device fingerprint. It uses event time with a five-second out-of-orderness allowance, because terminals forward transactions with small delays.

When a card is used in two distant countries within minutes, or spending exceeds its usual pattern, the job emits an alert to a decision service in well under a second. Checkpoints every thirty seconds to object storage allow the job to recover from a node failure without losing state, and a savepoint lets engineers deploy new fraud rules without resetting the per-card history.