Lesson 6 of 7 Amazon Kinesis and Amazon MSK

Handling Ordering and Delivery Guarantees in Streams

This lesson explains the promises streaming systems make about read order and delivery, since pipelines that assume stronger guarantees than the system provides produce duplicates and wrong results.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the ordering guarantee within and across shards or partitions.
  • Distinguish at-most-once, at-least-once and exactly-once delivery.
  • Identify why duplicates occur at the producer and consumer.
  • Achieve effectively-once results with idempotent processing.

Ordering

Records with the same partition key go to the same Kinesis shard, or the same Kafka partition, and are read in the order they were written. There is no ordering across shards or partitions. Pipelines that need per-entity order, such as all events for one account, therefore use the entity ID as the partition key; pipelines that need a global order must use a single shard, which limits throughput.

True or false

Ordering

Exam skills 1.1.1 · 1.1.12

Decide whether each statement about stream ordering is true.

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

Delivery guarantees

A delivery guarantee states what can happen to a record when failures and retries occur. With at most once, a record is never duplicated but may be lost, as when a producer sends without retrying. With at least once, a record is never lost but may be delivered more than once, because a retry can follow a success that was not acknowledged. Kinesis, Kafka, SQS standard queues and Lambda event sources all work this way, so consumers must tolerate duplicates.

With exactly once, each record affects the result once. It requires extra machinery, such as Flink checkpoints combined with transactional sinks, or SQS FIFO queues' deduplication. In practice, most pipelines achieve the same effect with at-least-once delivery and idempotent processing. The following table summarises the guarantees.

GuaranteeMeaningTypical source
At most onceRecords may be lost but are never duplicatedFire-and-forget producers without retries
At least onceRecords are never lost but may be duplicatedKinesis, Kafka, SQS standard, Lambda event sources
Exactly onceEach record affects the result onceFlink checkpoints with transactional sinks; SQS FIFO deduplication
Match the pairs

Delivery guarantees

Exam skills 1.1.11 · 1.1.12

Match each delivery guarantee with its behaviour.

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

Why duplicates occur

Most streaming systems guarantee at-least-once delivery, and duplicates arise at both ends of a stream. On the producer side, a producer that times out waiting for an acknowledgement retries the write, even though the first write may have succeeded, so the record is stored twice.

On the consumer side, a consumer that processes a batch and then fails before recording its checkpoint reads the same batch again after it restarts. Similarly, when a Lambda function fails part of a batch, the whole batch is retried unless the function reports partial batch failures, so successfully processed records are processed again.

The following diagram shows how duplicates arise at the producer and at the consumer.

Two ways at-least-once delivery creates duplicates. A producer whose acknowledgement is lost retries a write that had succeeded, so the record is stored twice. A consumer that fails after processing a batch but before checkpointing reads the same batch again after it restarts.
Record and batch numbers are illustrative.
Select all that apply

Where duplicates come from

Exam skills 1.1.11

Identify the situations that produce duplicate records.

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

Achieving effectively-once results

Because duplicates cannot be prevented entirely, pipelines achieve correct results through idempotence: processing a record twice has the same effect as processing it once. The foundation is a unique event ID in every record, assigned by the producer, which consumers use to recognise and discard duplicates.

Writes are made idempotent with upserts, such as MERGE keyed on the event ID, or with conditional writes that succeed only if the event has not been seen. For stateful stream processing, Amazon Managed Service for Apache Flink provides exactly-once state consistency through checkpoints, and combined with transactional or idempotent sinks, exactly-once results end to end.

The following diagram shows a Lambda consumer that adds payments to account balances, before and after it is made idempotent.

The payment consumer before and after the fix. Before, a retried batch added payments that had already been applied, so balances were too high. After, each payment ID is recorded with a conditional write in the same transaction as the balance update, so a repeated payment is rejected, and ReportBatchItemFailures retries only the records that failed.
The conditional write and the balance update run in one DynamoDB transaction.
Worked example

A Lambda consumer adds each payment event's amount to a running balance in DynamoDB. After a deployment that briefly caused errors, several balances are too high.

The failed batches were retried, and payments that had already been added were added again: delivery is at least once. Make the update idempotent by recording each payment ID with a conditional write in the same transaction as the balance update, so a repeated payment is rejected, and use ReportBatchItemFailures so only failed records are retried.

Check yourself

Kinesis delivers records at least once. What must consumers do?

Show answer

Be idempotent, or deduplicate records by a unique event ID, so a record processed twice has no extra effect.

Exam Tips

Remember for the exam

  • Ordering holds only within a shard or partition.
  • Kinesis, Kafka and Lambda event sources deliver at least once.
  • Make consumers idempotent, or deduplicate by a unique event ID.
  • Flink checkpoints with transactional sinks give exactly-once results.
  • Use the entity ID as the partition key when per-entity order matters.