Implementations and comparison
Comparing stream processing implementations
See how Kafka Streams, Flink, and Spark Structured Streaming maintain the same total and when to choose each one.
Learn with an AI assistant
Copy the prompt and paste it into a chat with an AI assistant.
You are a tutor for the course "Stream Processing".Topic: "Comparing stream processing implementations"In short: See how Kafka Streams, Flink, and Spark Structured Streaming maintain the same total and when to choose each one.Topic text in Markdown: https://stream-processing-systems.github.io/implementations/comparing-stream-processing-implementations.mdBase your explanations on the topic text. First ask how I want to study: a short summary, a step-by-step walkthrough, or a deep dive with questions that check my understanding. Use the text's example and terms. If you add anything the text does not say, point it out.The team behind our AI assistant needs to run the query that keeps account totals up to date. The gateway’s usage records already land in Kafka. After repeated deliveries are removed, each accepted request adds tokens to one account. The usage page needs the latest total for each account as the calculation catches up.
Kafka Streams is a Java library for processing data from Kafka. The team could put the calculation in a Java application or run it with Flink, the engine behind the earlier SQL query. Another option is Spark Structured Streaming, which can keep a query running as new data arrives. What would the team have to deploy and recover in each case, and what would reach the usage page?
One input contract and one result
Section titled “One input contract and one result”Start with empty totals. Keep the four familiar requests for acme and add
delta’s contribution from the partitioned input log.
Each row is a distinct accepted request. The three sums receive the account
and token count. An upstream step has already used usage_id to remove
repeated deliveries.
| Usage ID | Account | Tokens |
|---|---|---|
u-101 | acme | 1,000 |
u-102 | acme | 400 |
u-105 | delta | 300 |
u-103 | acme | 600 |
u-104 | acme | 500 |
Once all these records have been counted, the result is acme = 2,500 and
delta = 300. Another accepted row for acme with 400 tokens would make its
total 2,900. By that point the sum has only an account and a number, so it
cannot tell whether the 400 tokens came from a new request or another copy of
u-102. The total covers all accepted usage so far, without a time window.
If the four acme requests are processed in the order shown, the running total
goes through 1,000, 1,400, 2,000, and 2,500. A receiver can show 1,400 and then
2,500 and still have the correct current total. The skipped intermediate
value would matter to a receiver that needs to act on every change.
Where the calculation runs
Section titled “Where the calculation runs”In Kafka Streams, the Java application reads a topic keyed by account. The library runs the steps inside the application and publishes updated totals to another Kafka topic. Kafka stores the topics, including the changes used to restore task state.1
In Flink, the team submits a job to the Flink runtime. The Java DataStream API makes the grouping and the running sum explicit. The job’s source supplies the records, its keyed operator keeps the totals, and its sink receives the updates.2
In Spark Structured Streaming, Spark keeps a grouped query running. By default, it advances the query in micro-batches, processing available input in successive finite portions. With two file arrivals, it can process the first file, wait for the second, and continue the same sum.3
| Question | Kafka Streams | Flink DataStream | Spark Structured Streaming |
|---|---|---|---|
| What runs the calculation? | Instances of the Java application | A job submitted to the Flink runtime | A streaming query running on Spark |
| What must agree for recovery? | The task’s state and its consumed Kafka offsets | The job’s operator state and source positions at a completed checkpoint | The query’s aggregate state and micro-batch progress in its checkpoint |
| What does the example produce? | Records in a Kafka topic, each carrying a replacement total for its account | Updated account-total pairs for a downstream step | The full current result in an in-memory table for inspection |
What the usage page needs
Section titled “What the usage page needs”The page stores a row per account. If it reads a Kafka topic of updated totals, it can use the record key to find the row and replace 1,400 with 2,500. The Kafka Streams application can publish directly to that topic. With Flink the team would configure a sink that delivers those replacements. The Spark example makes it easy to inspect the full result locally. To serve the page, the team would select an output mode and a destination that can apply the query’s updates safely on retry.
When to choose each implementation
Section titled “When to choose each implementation”- Kafka Streams fits when input and output are Kafka topics and the team can deploy a Java application. The calculation runs inside that application, so there is no separate processing cluster to run. The trade-off is that the application still depends on Kafka for its input, output, and state recovery.1
- Flink fits when the team already runs Flink jobs or the calculation needs finer control over state and when to act. For example, DataStream can set a timer for each account and alert if an expected follow-up event fails to arrive. The team needs a Flink runtime and, for recovery, storage for checkpoints.24
- Spark Structured Streaming fits when the team already uses Spark for batch analytics and wants to express streaming aggregations with the same DataFrame API. In micro-batch mode, a record that arrives after a batch has picked up its input must wait for a later batch. Check whether the resulting delay is acceptable for the reader.3
What to remember
Section titled “What to remember”- Compare the same accepted input and the same result after it has been processed.
- The application or job runtime determines where state lives and how it is recovered.
- Start with the infrastructure the team already runs, then check that it supports the processing logic and the required freshness.
- Read the output contract before connecting a receiver. A replacement for one account and a full current table require different handling.
References
Section titled “References”© 2026 Grig Alex · CC BY-NC-SA 4.0
