Lesson 2 of 3 Streaming Data for ML

Using Apache Kafka and Apache Flink on AWS for ML

This lesson explains Amazon MSK for Apache Kafka and Amazon Managed Service for Apache Flink, the managed versions of the streaming technologies many organisations already run and the exam guide names.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe Kafka topics, partitions and consumer groups on Amazon MSK.
  • Explain how Flink computes stateful streaming features.
  • Choose between Kinesis, MSK and Flink.

Apache Kafka with Amazon MSK

Apache Kafka organises events into topics split into ordered partitions; consumer groups read partitions and track their position with offsets. Amazon Managed Streaming for Apache Kafka (Amazon MSK) runs Kafka clusters with managed brokers, patching and storage, and MSK Serverless removes broker sizing. Because the Kafka APIs are unchanged, existing producers, consumers and Kafka Connect connectors move to AWS without rewriting.

Choose Kafka on MSK when an organisation already uses Kafka, needs Kafka-specific tools or wants long retention with tiered storage. Choose Kinesis for a simpler AWS-native stream with deep integration with Lambda and Firehose.

Match the pairs

Kafka terms

Exam skills 1.1.4

Match each Kafka term with its Kinesis Data Streams counterpart.

Log in to try this activity Interactive activities are free with an account.

Apache Flink for stateful processing

Apache Flink processes unbounded streams with state: it can count events per user in sliding windows, join two streams, detect patterns and deduplicate, while checkpointing state so it recovers exactly where it stopped after a failure. It handles event time with watermarks, so late records are assigned to the correct window.

Amazon Managed Service for Apache Flink runs Flink applications written in Java, Scala, Python or SQL without managing clusters, reading from Kinesis or Kafka and writing to S3, Kinesis, Kafka, OpenSearch or custom sinks. For ML, Flink is the usual choice for computing streaming features, such as the number of failed logins in the last five minutes, and writing them to a feature store.

The following diagram shows the path from a card transaction to a fraud score after a company moves its real-time fraud features from Kafka to AWS.

Real-time fraud features after the move to AWS. Producers keep publishing to Kafka, now on Amazon MSK with only new broker addresses. A Flink application keyed by card computes 10-minute sliding counts and sums by event time and writes each update to the Feature Store online store, which the scoring service reads before calling the model endpoint.
The scenario is illustrative.

Choosing between the options

Several streaming technologies can feed machine learning pipelines, and the choice usually depends on what the team already uses and how much processing the stream needs. Teams with existing Kafka producers and consumers, or that need Kafka's ecosystem of connectors, use Amazon MSK. Teams that want the simplest AWS-native stream use Kinesis Data Streams. Stateful processing over time, such as computing a user's rolling activity features for a fraud model, calls for Amazon Managed Service for Apache Flink reading from either. The following table summarises when each streaming technology is the best fit.

RequirementBest fit
Existing Kafka producers and consumersAmazon MSK
Kafka without sizing brokersMSK Serverless
Simple AWS-native stream with Lambda consumersKinesis Data Streams
Windowed aggregations, stream joins, exactly-once stateManaged Service for Apache Flink
Deliver stream to S3 as filesAmazon Data Firehose
Scenario questions

Pick the streaming service

Exam skills 1.1.4 · 1.2.3

Choose the streaming service that fits each team's requirements.

Log in to try this activity Interactive activities are free with an account.
Worked example

Real-time fraud features

A bank already publishes card transactions to Kafka on premises and plans to move to AWS. The fraud model needs, for each card, the number and total amount of transactions in the last 10 minutes, updated within seconds.

  • Move the Kafka topics to Amazon MSK so existing producers only change broker addresses.
  • A Managed Service for Apache Flink application reads the topic, keys by card number and computes 10-minute sliding-window counts and sums using event time.
  • The application writes each updated feature record to the SageMaker Feature Store online store with PutRecord.
  • The scoring service reads the features with GetRecord and calls the model endpoint.

A Lambda function could keep counts in DynamoDB, but it would need custom code for windows, late events and exactly-once updates, which Flink provides through its state and checkpoints.

Check yourself

Which service suits stateful windows, joins and pattern detection on streams?

Show answer

Amazon Managed Service for Apache Flink.

Exam Tips

Remember for the exam

  • Existing Kafka workloads: Amazon MSK keeps the same APIs.
  • Stateful windows, joins and pattern detection: Managed Service for Apache Flink.
  • Flink checkpoints give exactly-once state after failures.
  • Event-time windows with watermarks handle late records correctly.
  • Streaming features often flow from Flink into the Feature Store online store.