Akash Patel
Akash Patel

Reputation: 199

How to get highest value of Range key in DynamoDB when range key is version number?

I have created one service X where accounting requests with some data comes from many other services Y. I create one idempotent_id from this data by adding many attributes so that this idempotent_id remains unique. I also create one idempotent_version and set it to 1. I store this record into dynamoDB (using idempotent_id and idempotent_version as partition key and sort key). now, I call some other service Z with this idempotent_id and idempotent_version. After some time, suppose request with same data comes I create idempotent_id but it will be same as before as we are processing same record. now I have to create idempotent_version which will be 2 in this case because same request have come before (this can be n if n-1 request have already come). how can i find what what will be idempotent_version for current data record because i will store the data into dynamoDB with this version and also call service Z with this version. (which will be 2 for current scenario)

Upvotes: 1

Views: 786

Answers (1)

Charles
Charles

Reputation: 23823

The docs have a section Using Sort Keys for Version Control.

Basically, when you create v1, also create as a copy of v1 as v0. Version 0 will always be the most recent. One additional attribute of v0 is named "Latest" and would start out as 1.

So when it's time for v2...

GetItem(hash:v0)
PutItem(hash:v2)
Copy v2 to v0, update "Latest" to 2
UpdateItem(hash:v0)

With the new transaction support, you can ensure that the Put of v2 and the update of v0 are consistent.

Upvotes: 2

Related Questions