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.
Ordering
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.
| Guarantee | Meaning | Typical source |
|---|---|---|
| At most once | Records may be lost but are never duplicated | Fire-and-forget producers without retries |
| At least once | Records are never lost but may be duplicated | Kinesis, Kafka, SQS standard, Lambda event sources |
| Exactly once | Each record affects the result once | Flink checkpoints with transactional sinks; SQS FIFO deduplication |
Delivery guarantees
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.

Where duplicates come from
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.

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.
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.