-
Notifications
You must be signed in to change notification settings - Fork 926
Optimistic locking for delete scenario with TransactWriteItemsEnhancedRequest #6043
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
base: master
Are you sure you want to change the base?
Conversation
9d90f5d
to
12779d9
Compare
bbbca6e
to
b1fd95d
Compare
This is a breaking change right ? What if currently user was using this API to delete any API m and such cases we will start getting exceptions ? |
Hello, @joviegas This is not a breaking change, as described in the Solution Proposal. A validation step was added to ensure that the version in the delete request matches the version stored in the database. If the versions do not match, the operation now throws a TransactionCanceledException, which is the expected behavior for conditional operations in DefaultDynamoDbClient in case a condition (in one of the condition expressions) is not met. This scenario is covered by the integration test deleteItemWithTransactWrite_shouldFailIfVersionMismatch in CrudWithResponseIntegrationTest.java. Before the changes from the current PR, the item would have been deleted even if the version did not match, and no exception was thrown. The new behavior enforces optimistic locking to improve data integrity. |
Right now, customers can delete items without worrying about versions, and it just works. But after our update, they're going to start getting TransactionCanceledException errors out of nowhere. Think about it this way: let's say someone has code that tries to delete item X with version 1. They don't really care if the actual version is 2 or 3 - they just want the item gone. Today, that works fine. Tomorrow, after they upgrade, boom - exception thrown and their code breaks. That's a classic breaking change. Their apps that work perfectly today will suddenly fail. They'll have to go in and modify their code to handle these new exceptions. And there's no way for them to get the old "just delete it" behavior back without changing their code. Don't get me wrong - I totally get that this change makes things better from a data integrity standpoint with the optimistic locking. Some customers probably built their apps specifically counting on that "delete regardless of version" behavior. |
f6e85cb
to
90dab7c
Compare
Hello @joviegas,
The current method will internally delegate to the new method using useOptimisticLocking = false, thereby maintaining the existing "delete regardless of version" behavior without introducing any breaking change. We will refactor the tests to add coverage on both current and new behavior. This approach allows us to introduce the feature in a backward-compatible manner, giving users the choice to adopt the new behavior at their discretion. We look forward to your feedback on this approach. |
Thanks anasatirbasa@ for the new version , will take a look at it in between this week |
1 similar comment
Thanks anasatirbasa@ for the new version , will take a look at it in between this week |
6c187a8
to
841d706
Compare
Thanks @anasatirbasa for the PR! I've reviewed the changes and tested some scenarios. I have a concern about the current implementation scope: optimistic locking shouldn't be limited to just @Test
public void deleteExistingRecordVersionMatches() {
mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one")));
mappedTable.putItem(new Record().setId("id").setAttribute("one").setVersion(1));
mappedTable.putItem(new Record().setId("id").setAttribute("one").setVersion(2));
// This fails as expected
// mappedTable.putItem(new Record().setId("id").setAttribute("one").setVersion(1));
// This should also fail for consistency when user is expecting Optimistic operation [In our case they will provide settings]
mappedTable.deleteItem(new Record().setId("id").setAttribute("one").setVersion(1));
} Alternative Approach Suggestion: Instead of overriding specific APIs, I recommend adding a feature flag to the VersionIdExtension (similar to the approach in PR #6019). We could introduce an "optimisticDelete" flag that:
Next Steps: Could you please conduct a surface API review considering:
This would help ensure we're taking the most comprehensive and user-friendly approach. |
Hello, @joviegas We understand your concern and we've analyzed your proposal (defining a feature flag for optimistic locking on delete operations at extension level - VersionRecordExtension.java)
Proposed Behavior with Optimistic Locking Flag
Implementation Strategies1. DynamoDbEnhancedClient level configurationDynamoDbEnhancedClient client = DynamoDbEnhancedClient.builder()
.dynamoDbClient(getDynamoDbClient())
.extensions(VersionedRecordExtension.builder()
.optimisticLocking(true)
.build())
.build(); We will have to pass the optimistic locking flag when we attach the custom VersionedRecordExtension when building DynamoDbEnhancedClient. 2. Annotation-based configuration@DynamoDbVersionAttribute(optimisticLocking = true)
public Integer getVersion() {
return version;
} Allows per-entity control of optimistic locking via annotations. The first approach offers a single, centralized configuration but lacks per-schema flexibility. The annotation-based approach provides control at the model level, allowing different entities to opt in or out independently. Could you please advise which solution fits better from your perspective? |
Thank you so much for the detailed documentation and thorough analysis of the APIs. I really appreciate the comprehensive breakdown of the implementation approach. I apologize for the delay - I had posted in our internal Slack asking for a quick API design review and was waiting for feedback. However, I see you've already provided excellent documentation directly in this PR, which is very helpful. Important clarification needed: Since we're using the generic name optimisticLocking, could you please confirm that when this flag is enabled, it will apply to all write operations (create, update, delete) consistently? Per the DynamoDB documentation:
This would mean (please help me confirm):
Regarding your implementation approach: I see you're proposing both client-level configuration via the builder AND annotation support. A few questions that would help me better understand the design:
If you could help clarify these points, I'll complete a quick internal review with the team and provide feedback. |
Hello @joviegas, Thanks for your feedback and the request for consistency across all operations. Just to clarify, we have provided two alternatives for how the optimistic locking flag could be introduced, either at Version extension level or at annotation level, but not both together. For the moment let’s consider only the flag at extension level option. Regarding the consistency across all operations, below is a clarification of our design options and the trade‑offs we see:
Also: the Question for you
Happy to refine either path further given your input. Thanks again and looking forward to your feedback! |
@anasatirbasa |
Description
Optimistic locking while using DynamoDbEnhancedClient - DeleteItem with TransactWriteItemsEnhancedRequest
Motivation and Context
#2358
Modifications
Testing
Added unit tests for ChainExtension, VersionRecordTest and integration tests for delete scenario with matching version and with exception if the version mismatch.
Screenshots (if appropriate)
Types of changes
Checklist
mvn install
succeedsscripts/new-change
script and following the instructions. Commit the new file created by the script in.changes/next-release
with your changes.License