Lesson 7 of 7 Amazon Kinesis and Amazon MSK

Designing Fan-In, Fan-Out and Stateful Stream Processing

This lesson explains how streaming architectures are described by how data converges and spreads, fan-in and fan-out, and by whether processing must remember earlier records, and how both choices decide which services and settings fit.

Learning outcomes

After completing this lesson, you will be able to:

  • Design fan-in from many producers into one stream.
  • Design fan-out to many independent consumers.
  • Distinguish stateless from stateful processing and choose a service for each.

Fan-in

Fan-in means many producers sending data into one destination, such as thousands of devices writing to a single Kinesis data stream, or log groups in many accounts sending to one central stream through CloudWatch Logs subscription filters and a cross-account destination. Fan-in simplifies downstream processing, because consumers read one stream, but the destination must absorb the combined throughput.

The design concerns are capacity and distribution. The total write rate decides how many shards or partitions are needed, or whether on-demand mode should scale them automatically. The partition key must spread records evenly, because a key shared by many producers creates a hot shard. Producers can batch records, for example with the Kinesis Producer Library's aggregation, to use each shard's 1,000-records-per-second limit efficiently. EventBridge provides fan-in for events, with rules on a central event bus receiving events from other accounts.

Scenario questions

Fan-in design

Exam skills 1.1.10

Choose the design for each fan-in requirement.

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

Fan-out

Fan-out means one stream of data delivered to many consumers, each processing it independently. With Kinesis Data Streams, standard consumers share each shard's 2 MB per second of reads, so enhanced fan-out is used when several consumers each need full throughput and low latency. With Amazon MSK, each consumer group reads the whole topic independently. For messages and events, SNS fans one message out to many SQS queues, Lambda functions and endpoints, and an EventBridge rule can send one event to up to five targets. The following table compares the mechanisms.

PatternMechanismNotes
Fan-in of recordsMany producers into one Kinesis stream or MSK topicSize for combined throughput; spread partition keys
Fan-in of logsCloudWatch Logs subscription filters to a cross-account destinationCentralises logs from many accounts
Fan-out of recordsKinesis enhanced fan-out; MSK consumer groupsEach consumer reads at full throughput
Fan-out of messagesSNS topic to many SQS queues (SNS-to-SQS fan-out)Each subscriber gets its own durable copy
Fan-out of eventsEventBridge rule with several targetsContent-based routing to up to five targets per rule

The following diagram shows a retail stream that combines fan-in from 2,000 stores with fan-out to three consumers, one of which is stateful.

The retail stream. Two thousand stores write to one on-demand Kinesis data stream, keyed by store and register so writes spread evenly (fan-in). Fraud detection and the dashboard each use enhanced fan-out (fan-out); fraud detection is stateful and runs on Managed Service for Apache Flink, the dashboard is stateless and runs in Lambda, and Firehose archives every record to S3.
EFO is enhanced fan-out.
Match the pairs

Fan-out mechanisms

Exam skills 1.1.10

Match each fan-out need with its mechanism.

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

Stateless and stateful processing

Stateless processing handles each record on its own, using only the data in that record: filtering out invalid records, masking a field, converting a format or enriching a record from a lookup. Because no record depends on another, stateless work scales simply by adding parallel workers, and a failed record can be retried in isolation. A Lambda function triggered by a stream, or a Firehose transformation, is typically stateless.

Stateful processing keeps information across records: counting events per user in five-minute windows, joining two streams, deduplicating by event ID or detecting a sequence of events. The state must survive failures, so stateful engines store it in checkpoints. Amazon Managed Service for Apache Flink keeps keyed state with exactly-once checkpoints, Spark Structured Streaming in Glue or EMR checkpoints to S3, and Lambda offers limited state through tumbling windows of up to 15 minutes. The same terms apply to transactions: a stateless transaction carries everything it needs, while a stateful one depends on context held from earlier steps, such as a session. The following table summarises the difference.

Stateless
UsesOnly the current record
Scaling and recoveryAdd workers; retry single records
AWS examplesLambda per record, Firehose transformation
Stateful
UsesInformation from earlier records (windows, joins, deduplication)
Scaling and recoveryState must be checkpointed and restored
AWS examplesManaged Service for Apache Flink, Spark Structured Streaming, Lambda tumbling windows
Sort into groups

Stateless or stateful?

Exam skills 1.1.12

Decide whether each processing step is stateless or stateful.

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

One stream, three teams

Two thousand retail stores send sales events to one Kinesis data stream in on-demand mode, using the store ID and register as the partition key so writes spread evenly (fan-in). Three teams consume the stream: fraud detection, a real-time dashboard and an archive. Fraud detection and the dashboard register for enhanced fan-out so each has its own throughput (fan-out), while Firehose archives to S3. Fraud detection must compare each card's transactions over the last ten minutes, which is stateful, so it runs on Managed Service for Apache Flink with keyed state; the dashboard's per-record formatting is stateless and runs in Lambda.

Check yourself

How is one message delivered to many independent subscribers, each with its own retries?

Show answer

With SNS-to-SQS fan-out: an SNS topic publishes to one SQS queue per subscriber.

Exam Tips

Remember for the exam

  • Fan-in: many producers into one stream; size for combined throughput and spread partition keys.
  • Several consumers each needing full Kinesis throughput: enhanced fan-out.
  • One message to many independent subscribers: SNS-to-SQS fan-out.
  • Windows, joins and deduplication are stateful; use Managed Service for Apache Flink or Spark Structured Streaming.
  • Per-record filtering, masking and format conversion are stateless and scale by adding workers.