Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does DynamoDB have locking by default?

I'm looking over the dynamo documentation and it looks like they have optimistic. I'm wondering if this is used by default or not.

From the documentation, it looks like you need to code up the java application to use the @DynamoDBVersionAttribute annotation and get and set the versions. Without doing this, it looks like you can write to DynamoDB without any sort of locking.

Is that correct?

On a side note, I'm not too familiar with DBs without some sort of locking so what would happen if 2 people wrote to the same item at the same time in DynamoDB without any locking? Say the item we're writing to has 4 fields, would one write completely fail or is it possible that DynamoDB updates 2/4 fields with 1 write, and the other 2 fields with the other write?

like image 373
Kevin Avatar asked Jan 11 '18 02:01

Kevin


People also ask

Does DynamoDB have locking?

Optimistic locking is a strategy to ensure that the client-side item that you are updating (or deleting) is the same as the item in Amazon DynamoDB. If you use this strategy, your database writes are protected from being overwritten by the writes of others, and vice versa.

Does DynamoDB support pessimistic locking?

Therefore, it is crucial to handle cases like this. Luckily, DynamoDB provides both Optimistic and Pessimistic Locking to solve this problem.

Is AWS DynamoDB secure?

DynamoDB encrypts at rest all user data stored in tables, indexes, streams, and backups using encryption keys stored in AWS Key Management Service (AWS KMS) . This provides an additional layer of data protection by securing your data from unauthorized access to the underlying storage .


2 Answers

You are correct. DynamoDB does NOT have optimistic locking by default. There are various SDKs for DynamoDB and as far as I am aware the only one which provides optimistic locking functionality is the Java SDK.

Here's what the Java SDK optimistic locking actually supports:

  • Creates an attribute in your table called version
  • You must load an item from the database before updating it
  • When you try and save an item the SDK tests that the client item version number matches the one in the table, and if it does, the save is completed and the version number is incremented

This is pretty simple to implement yourself if you are using a different SDK. You would create the version attribute yourself. You would create a wrapper for the putItem method (and any other required save/update operations). You would use the Condition Expression to test that the version number in the database is one less than the version you saving.

To answer the second part of your question, both updates would succeed (assuming you had put no conditions on your update). The first one would make any updates specified, and the second one would come along and overwrite them.

like image 153
F_SO_K Avatar answered Sep 19 '22 16:09

F_SO_K


Dynamodb doesn't support optimistic locking by default. As you mentioned, you need to use the annotation in the Java model class in order to use the optimistic locking.

If two threads write to the same item, the Dynamodb item will have the last write data (i.e. last thread which writes the data).

like image 39
notionquest Avatar answered Sep 17 '22 16:09

notionquest