Lesson 4 of 4 Amazon Kinesis and Streaming Services

Processing Streams in Real Time with Managed Service for Apache Flink

This lesson explains how Amazon Managed Service for Apache Flink runs stateful, real-time stream processing on Kinesis or Kafka data without managing infrastructure.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe what Flink applications do.
  • Identify typical uses such as windowed aggregation.
  • Choose a stream processor.

What it does

Amazon Managed Service for Apache Flink runs stream-processing applications written in Java, Scala, Python or SQL. They read from Kinesis Data Streams and Amazon MSK, and write to streams, Amazon S3, databases and other destinations.

Flink applications keep state across events, such as running totals or recent events per user, which allows windowed aggregations, joins between streams and detection of patterns over time. State is saved in automatic checkpoints, so applications recover from failures without losing it. The service scales automatically and is billed per Kinesis Processing Unit.

Typical uses

Managed Service for Apache Flink runs Apache Flink applications, which process streams continuously and keep state between records, something a simple per-record function cannot do. Windowed aggregations compute results over time windows, such as orders per minute for each product, updated as each order arrives. Anomaly detection compares each event with recent history, for example flagging an unusual card transaction as it happens.

Stream enrichment and joins combine streams with each other or with reference data, such as adding customer details to click events. Real-time dashboards are fed with the computed metrics, written continuously to OpenSearch or a database. Applications can be written in Java, Python or SQL. The following table summarises these uses.

UseExample
Windowed aggregationsOrders per minute per product, updated continuously
Anomaly detectionFlagging unusual transactions as they happen
Stream enrichment and joinsAdding customer details to click events
Real-time dashboardsFeeding metrics to OpenSearch or a database

Choosing a stream processor

The right stream processor depends on how much each record's processing depends on other records. Lambda, through an event source mapping, suits simple processing of each record on its own, such as validating and storing it. Managed Service for Apache Flink suits stateful processing, such as aggregations over time windows, joins and SQL analytics across many records. Amazon Data Firehose suits delivering the stream to storage, such as S3 or Redshift, with light transformation and no code to manage. The following table summarises the choice.

NeedChoose
Simple per-record processingLambda
Stateful, windowed or SQL analytics on streamsManaged Service for Apache Flink
Delivery to storage with light transformationData Firehose
Sort into groups

Which stream processor?

Exam skills 3.5.2 · 3.5.5

Choose the stream processor for each requirement.

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

Detecting fraud in real time

A payments company must flag cards used in more than three countries within ten minutes, as transactions arrive on a Kinesis data stream.

RequirementFlink feature
Remember recent transactions per cardKeyed state
Evaluate a ten-minute window continuouslySliding windows
Survive failures without losing stateAutomatic checkpoints
Send alertsA sink to another stream or to SNS through Lambda

Lambda alone processes records one batch at a time and keeps no state between invocations, which makes windowed, stateful analysis like this hard; Flink is designed for it.

Check yourself

What does Managed Service for Apache Flink do with a stream?

Show answer

It runs continuous processing, such as aggregations over time windows and anomaly detection, on data from Kinesis Data Streams or MSK as it arrives.

Exam Tips

Remember for the exam

  • For real-time SQL or stateful analytics on streams, use Managed Service for Apache Flink.
  • For windowed aggregations over streaming data, use Managed Service for Apache Flink.
  • For simple per-record processing of a stream, Lambda is usually enough.