Kinesis creates multiple records with the same sequence number

Based on Kinesis documentation, sequence number is supposed to be unique, however we see the same value being reused across multiple records. Our event producer is Spring Boot application that uses KPL internally, consumers are AWS lambdas. We have performed a re-sharding a couple times during the test. Below you can see sample sequence number reused more than once. How that’s even possible?

{
“Records”: [{
“kinesis”: {
“kinesisSchemaVersion”: “1.0”,
“partitionKey”: “00000000000000002”,
“sequenceNumber”: “49596124085897508159438713510240079964989152308217511954”,
“data”: “",
“approximateArrivalTimestamp”: 1558991793.009
},
“eventSource”: “aws:kinesis”,
“eventVersion”: “1.0”,
“eventID”: “shardId-000000000001:49596124085897508159438713510240079964989152308217511954”,
“eventName”: “aws:kinesis:record”,
“invokeIdentityArn”: “-----------------”,
“awsRegion”: “us-east-1”,
“eventSourceARN”: “-----------------”
}, {
“kinesis”: {
“kinesisSchemaVersion”: “1.0”,
“partitionKey”: “00000000000000003”,
“sequenceNumber”: “49596124085897508159438713510240079964989152308217511954”,
“data”: “”
”,“,
“approximateArrivalTimestamp”: 1558991793.009
},
“eventSource”: “aws:kinesis”,
“eventVersion”: “1.0”,
“eventID”: “shardId-000000000001:49596124085897508159438713510240079964989152308217511954”,
“eventName”: “aws:kinesis:record”,
“invokeIdentityArn”: “-----------------”,
“awsRegion”: “us-east-1”,
“eventSourceARN”: “-----------------”
}, {
“kinesis”: {
“kinesisSchemaVersion”: “1.0”,
“partitionKey”: “00000000000000004”,
“sequenceNumber”: “49596124085897508159438713510240079964989152308217511954”,
“data”: “”************************”,",
“approximateArrivalTimestamp”: 1558991793.009
},
“eventSource”: “aws:kinesis”,
“eventVersion”: “1.0”,
“eventID”: “shardId-000000000001:49596124085897508159438713510240079964989152308217511954”,
“eventName”: “aws:kinesis:record”,
“invokeIdentityArn”: “-----------------”,
“awsRegion”: “us-east-1”,
“eventSourceARN”: “-----------------”
}]
}


Related discussions:

Check out https://docs.aws.amazon.com/streams/latest/dev/key-concepts.html#partition-key. The sequenceNumber is only unique within its shard, identified by the partitionKey.

Was the data the same or different for the duplicate records? Also, did you get these duplicates from a single invocation of your consumer lambda?

I see they all have different partition keys, which are used to shard data. There might have been data agreggation happening. On a side note: these partiton keys seem unusual and not well spread. Partition keys ONLY need to be different from each other in a shard.

Did you do any resharding? That would explain this behaviour and would be perfectly fine.

Sequence number ordering is specific to a shard within a stream and not across all shards within a stream. The sequence number is non-decreasing within a shard. You can guarantee ordering by writing serially to a shard and using the exclusive minimum sequence number parameter. Amazon Kinesis always guarantee stream and shard read order.

Haven’t used kpl for ages but if memory serves me right multiple logical records get packed into a single kinesis record, sharing the same seq no.

In such case AWS documentation is misleading:

A sequence number is a unique identifier for each record. Sequence number is assigned by Amazon Kinesis when a data producer calls PutRecord or PutRecords operation to add data to an Amazon Kinesis data stream.

Also Kinesis sends those records separately to consumers.

Data was different. Yes, three records above were sent inside the same record batch.

Did you do any resharding? That would explain this behaviour and would be perfectly fine.

Why that’s fine. All of those records belong to the same shard, hence according to AWS docs each of the must be assigned a unique sequence number.

I am not sure if records were retried, we just wanted to collect partition key distribution stats and processed all records (TRIM_HORIZON) and then noticed many duplicates.

How that’s even possible? Sequence number is not set by Kinesis, but by KPL?

I was thinking about scaling in (merging) shards. Ok i see, i do not have the whole underlying whitepaper in detail in my head, will out of curiosity read it again. But i was experiencing the same thing when using KPL aggregation where the record CONTENT is holding information about subsequencenumbers.

Edit: " However, if KPL aggregation is being used on the producer side, there is a subtlety involving checkpointing: all subrecords within an aggregated record have the same sequence number, so additional data has to be stored with the checkpoint if you need to distinguish between subrecords. This additional data is referred to as the subsequence number. "

https://docs.aws.amazon.com/streams/latest/dev/kinesis-kpl-consumer-deaggregation.html

see my comment below with the source.

Sounds plausible. Thank you

Thanks a lot.

I could reproduce this problem. When KPL uses aggregation User Records are delivered batched together to regular Kinesis consumers and their sequence numbers are unique, but we need to implement de-aggregation. If enhanced fan-out is used User Records are delivered as separate Kinesis records and they share the same sequence number.

So the Kinesis sequence number is not always unique.