- Newest
- Most votes
- Most comments
Hi,
I think you might be mixing two different concepts for the overloaded term "version".
The best practices guide for Using Sort Keys for Version Control: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-sort-keys.html#bp-sort-keys-version-control
is more related to audit/compliance version control design pattern. Here, you are interested in two use cases:
1. Get the latest audit entry by using the sort key to search on "v0_"
2. Get the historical chronological list of audits, by using the partition key/sort key and filter out "v0_".
For this design pattern, you are NOT allowed to update/change historical audit data. You are only allowed to update "v0_Audit" so that the latest audit entry is available to auditors. When a new audit entry is made, the software must:
1. Insert a new audit entry row with sort key "v<max version number + 1>_Audit"
2. Update the row with sort key "v0_Audit" with copy of the data inserted in step 1.
The DynamoDBMapper usage of DynamoDBVersionAttribute, is all about updating data and preventing two different clients from updating the same row in the table (optimistic locking). Each row will be updating the row version number on every new update. i.e. each client will initially get the current version for that row before updating the data. When the client attempts to update that existing row, the version the client has needs to match the version stored in the db for that row before it is saved. If the versions do NOT match, then the client's update will fail, which means that another client has already updated that row.
Hopefully, that makes sense,
-randy
Got this answer from AWS support which echos the first answer on this threas
The DynamoDBMapper usage of DynamoDBVersionAttribute, is all about updating the same Item and preventing two different clients from updating the same Item in the table (optimistic locking). Each Client will be updating the Attribute version number for every new update. i.e. each client will initially get the current version for that item before updating the data. When the client attempts to update that existing item, the version the client has needs to match the version stored in the db for that item before it is saved.
Now since with DynamoDB an item is uniquely characterized by it's key (whether partition key only, or partition + range keys together) and DynamoDBMapper assigns a version number when you first save the object, and it automatically increments the version number each time you update the item hereby changing the range key. This will creates a new item in the table everytime which is technically not an update of the same Item but creates a new one always. which defects the purpose of optimistic locking in DynamoDB.
The Documentation here [1]. talks about using Sort key for Audit purposes were each update in the table is a new item ( a Versions all existing in the same table) using composite primary key, which uniquely identifies an item in a table. While with optimistic locking[2], each item has an attribute that acts as a version number.( Not Sort key as this will create a new item in the table ) If you retrieve an item from a table, the application records the version number of that item. You can update the item, but only if the version number on the server side has not changed. If there is a version mismatch, it means that someone else has modified the item before you did. The update attempt fails, because you have a stale version of the item. If this happens, you simply try again by retrieving the item and then trying to update it. Optimistic locking prevents you from accidentally overwriting changes that were made by others. It also prevents others from accidentally overwriting your changes.
[1]. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-sort-keys.html#bp-sort-keys-version-control
[2].https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html
Relevant content
- Accepted Answerasked 3 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 8 months ago
- AWS OFFICIALUpdated 2 years ago