Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are Sequence Numbers unique per partition key? Is is possible that two separate clients running on separate threads write a record to the same partition key and end up having the same seq. number? #643

Closed
thiagoh opened this issue Nov 4, 2019 · 2 comments

Comments

@thiagoh
Copy link

thiagoh commented Nov 4, 2019

No description provided.

@MihirLimbachia
Copy link

The sequence number is a unique identifier of the record in a shard of the kinesis stream.

If two concurrent clients make a request using the same partition key and if the hash value calculated based on the partition key lies with the hash key range of the same shard in the kinesis stream.. they both will try to put to the same shard and will get a unique sequence number in return.

However, if hash value for the two clients using the same partition key are different causing the record to go to different shard, its possible they get the same sequence number. But the records would belong to different shards in this scenario.

If you want the records to be put to same shard every time.. use ExplicitHashKey which falls within the hash key range of the shard.

https://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html#Streams-PutRecord-request-ExplicitHashKey

https://docs.aws.amazon.com/kinesis/latest/APIReference/API_HashKeyRange.html

@thiagoh
Copy link
Author

thiagoh commented Nov 5, 2019

thanks for the answer. This ref explains thoroughly how to deal with seq numbers:
https://docs.aws.amazon.com/streams/latest/dev/developing-producers-with-sdk.html#kinesis-using-sdk-java-add-data-to-stream

for (int j = 0; j < 10; j++) 
{
  PutRecordRequest putRecordRequest = new PutRecordRequest();
  putRecordRequest.setStreamName( myStreamName );
  putRecordRequest.setData(ByteBuffer.wrap( String.format( "testData-%d", j ).getBytes() ));
  putRecordRequest.setPartitionKey( String.format( "partitionKey-%d", j/5 ));  
  putRecordRequest.setSequenceNumberForOrdering( sequenceNumberOfPreviousRecord );
  PutRecordResult putRecordResult = client.putRecord( putRecordRequest );
  sequenceNumberOfPreviousRecord = putRecordResult.getSequenceNumber();
}

The preceding code sample uses setSequenceNumberForOrdering to guarantee strictly increasing ordering within each partition key. To use this parameter effectively, set the SequenceNumberForOrdering of the current record (record n) to the sequence number of the preceding record (record n-1). To get the sequence number of a record that has been added to the stream, call getSequenceNumber on the result of putRecord.

@thiagoh thiagoh closed this as completed Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants