Kafka Streams vs Flink for Pre-Aggregation

Pick by how your events arrive, not by raw throughput.

· 8 min read
Cover illustration for “Kafka Streams vs Flink for Pre-Aggregation”
Stream Processing Engines · November 15, 2026 · 8 min read · 1,729 words

Pre-aggregation with Kafka Streams or Flink is chiefly a choice about time. Kafka Streams is sufficient when events arrive in near-order and windows are simple. Flink earns its operational complexity when correct results depend on handling out-of-order events and routing late data. Teams must also decide which aggregations need a streaming job and which belong in the downstream OLAP engine.

Why pre-aggregation is not primarily a throughput problem

Pre-aggregation does some of the work of analysis before records reach the query engine. For windowed results, that work includes deciding which events belong together and when a result is ready. Speed alone cannot settle either question. Both Kafka Streams and Flink support exactly-once semantics and stateful windowed aggregation, and they differ in how they treat events that arrive after processing has moved ahead.

Kafka Streams is a library inside the application JVM, with no separate cluster or independent scheduler. Flink is a framework whose JobManager and TaskManagers own the processing lifecycle. This gives Flink independent control over watermarks, late-data routing, and operator-level parallelism, while leaving Kafka Streams simpler to operate. The extra runtime has to justify its upkeep.

How Kafka Streams handles time and what that means for aggregation windows

Kafka Streams uses stream time. Stream time is the maximum record timestamp seen so far, tracked locally per task or subtopology. Processing in near-event-time order works reliably when records reaching Kafka partitions are themselves near-ordered. The library has no separate watermark model like Flink’s to keep event-time progress in step.

A record can arrive too late for its aggregation window and be dropped. Kafka Streams has no built-in side-output mechanism to send these records to a separate late-data stream or dead-letter topic. That routing requires custom application code. For pre-aggregation, a team must decide whether it can lose a small amount of late data or needs a further path for every record left out.

This makes tumbling and hopping windows a good fit when arrival order is predictable. Session windows are harder to get right when keeping event-time progress in step takes complex coordination. The library’s simplicity is useful, but it does not remove those requirements. A team already running JVM microservices, with Kafka as both source and sink, can keep straightforward aggregation inside its existing application model. That fit weakens when late-data handling becomes a separate processing obligation.

State lives locally in RocksDB, with Kafka changelog topics supporting recovery, giving low-latency access while tying state capacity to each instance’s disk. Useful stream-thread parallelism is limited by the input topic’s partition count. Repartitioning for an aggregation means writing to and reading from an intermediate Kafka topic, adding latency and storage cost. These limits matter when a simple windowed job grows beyond what it was built to do.

Flink distinguishes event time, when an event occurred, from processing time, when an operator processes it. Each operator advances its watermark using the minimum of its input watermarks. Unlike Kafka Streams’ task-scoped maximum timestamp, the watermark provides a basis for coordinating aggregation over out-of-order streams. That distinction becomes useful when arrival order is not a reliable guide to which window an event belongs in.

A watermark does not mean late events disappear. Flink windows can use allowed lateness to keep accepting events for a defined period after the watermark passes. Within that period, an arrival can update the aggregate and cause a corrected result to be emitted. The window can therefore distinguish between producing a result and refusing any further correction.

But how long should correction remain possible? Allowed lateness defines a finite correction period. Flink also provides side outputs, which are named streams that can preserve late records. Those streams enable downstream dead-letter handling, audit, or reprocessing. Together, these mechanisms give a job an explicit policy for both revising an aggregate and handling records outside its correction period.

The framework also separates aggregation parallelism from source partition count. A job reading a 32-partition Kafka topic can run aggregation operators at a higher degree of parallelism through internal repartitioning. Flink can also join a Kafka stream with a second stream, a changelog, or a slowly-changing dimension table in one job. Those capabilities broaden what can happen before the sink.

A deployment includes a JobManager and TaskManagers, checkpoint storage, and job lifecycle management. Backpressure monitoring and TaskManager memory tuning become part of running the pipeline. The team that ships product features now also has to run distributed infrastructure. That is a reasonable trade when late-data correctness is a requirement, but a harder one to justify for a predictable count over well-ordered records.

The pre-aggregation trap: high-cardinality COUNT DISTINCT in both systems

Exact COUNT DISTINCT over millions of user IDs, device fingerprints, or session tokens has to keep the full set of distinct values. That state can take up many gigabytes per window in either system. This is a different kind of problem. Watermarks determine how time advances, but they do not shrink the distinct set.

Problems that stay hidden in smaller workloads can become visible at production volumes. A Kafka Streams instance can fill its local RocksDB storage, while a Flink job can face TaskManager heap pressure and backpressure. If approximation is acceptable, probabilistic structures such as HyperLogLog reduce the state needed at the cost of exactness. If exactness is required, moving COUNT DISTINCT to the OLAP layer avoids maintaining that set in the streaming tier. The choice is therefore not only which processor to use, but where the calculation belongs.

Flink 2.0, released on March 24, 2025, changed how large-state jobs store their state. Its Disaggregated State Storage and Management uses the ForSt backend, with Distributed File Systems as primary storage and local disk as an optional secondary cache. State no longer has to live primarily with compute. The VLDB 2025 paper Disaggregated State Management in Apache Flink® 2.0 describes this separation and an asynchronous runtime that addresses the latency implications of remote storage. This changes the operational case for large-state pre-aggregation.

Under this architecture, object storage capacity bounds state size. Compute nodes can be added or removed without redistributing their locally held state, because the primary state lives externally. Scaling during a traffic spike can therefore happen without a full state migration. Recovery changes for the same reason. New nodes can read state from object storage rather than first downloading it from a checkpoint archive, reducing the recovery burden of large jobs.

The distinction is between accommodating state and eliminating it. Disaggregated storage makes it easier to run large-state workloads, but exact distinct counting still has to keep its distinct set. It also leaves the work of operating a Flink cluster in place. The 2.2.x line arrived in December 2025 with deeper AI and real-time integration, extending the architecture further. For pre-aggregation decisions, the relevant change remains the separation of state capacity from individual compute nodes.

Where the OLAP engine absorbs what used to belong to the streaming layer

Being able to keep more state upstream is not in itself a reason to do more aggregation there. Purpose-built OLAP engines have absorbed a meaningful share of this work, including exact aggregations, materialized views, and high-cardinality counts. The destination is part of the design. Before choosing a streaming processor, a team should ask which calculations need to happen before ingestion. Otherwise, an aggregation job may do work the query engine already does well.

There is still a clear case for preprocessing. Heavy deduplication, strict event-time windowing, and complex stateful joins upstream can substantially reduce the compute and storage the OLAP engine needs to serve queries. Here, the streaming layer earns its place by delivering a cleaner, already-processed stream. It also meets requirements for time and late data that can only be left to a later stage if the pipeline’s requirements change.

The reverse applies to simple counts and sums. Over well-ordered data, these can be cheaper to leave to the OLAP engine than to maintain through an additional streaming job. The team should also weigh where to run exact distinct counts because of the state they need upstream. Where near-real-time freshness is sufficient and late records do not need side outputs, the query engine can absorb more of the work. Removing an unnecessary streaming job also removes the operational work attached to it.

OLAP engines built for sub-second analytical queries are natural terminal sinks for real-time dashboards and APIs. The streaming layer sends clean, correctly timed records, and the OLAP layer answers analytical queries. Neither role needs to replace the other.

For a team making this choice, the first practical question is what must happen when a record arrives late. If a small amount of dropped late data is acceptable, Kafka Streams may be sufficient. That case is strongest with Kafka-only sources and sinks, near-partition-order arrivals, and tumbling or hopping windows with predictable ordering. An existing JVM microservice deployment also avoids the need for a separate cluster. That simpler setup works when state fits comfortably in tens of gigabytes per instance.

Flink earns its overhead when the answer is stricter. Events may arrive far out of order, results may need to be corrected through allowed lateness, or records left out may need to go to a side output or dead-letter stream. Those are concrete processing requirements. Multi-source joins and aggregation parallelism beyond the input partition count can strengthen the case. So can state that outgrows local RocksDB capacity, when Flink 2.0+ disaggregated storage fits the workload.

Before moving such work upstream, the team should still check how much of it the OLAP engine can do. A large exact distinct count may belong downstream even when other parts of the pipeline require Flink. At the same time, strict event-time windows may need to stay upstream even when their results are small. Size alone does not decide the placement. Bug reports about late-data correctness provide the clearest signal to graduate from Kafka Streams.

In practice, the architecture can use both processors. Kafka handles transport and durability, while simple aggregations run in Kafka Streams applications beside JVM microservices. Flink jobs take on multi-stream joins, session analytics, and strict event-time windowing, reading from Kafka and writing results back to Kafka or directly to the OLAP sink. Complexity goes where the time-handling requirements justify it.

More in Stream Processing Engines