2
votes

updated

Original question below - revised question is this:

Ignoring my use case below, using dynamo can one do an old fashioned pessimistic lock whereby the entire item is locked for concurrent access until I am done with it?


Quick question, as per title - all my googling only shows talk of optimistic locking.

Would like to know if it's possible to simply do the equivalent of a 'select for update'?

Use case requires a read, then to update a modified version. Would be nice to do a single read and a single write, rather than 2 reads and a write required for the optimistic strategy.

Thanks.

1
I checked the documentation on 'UpdateItem' and it looked like it is doing a read/update on a single 'item' atomically already by nature. Is this what you want or is your use case more complicated? - Peter Branforn
BTW: I think was not clear enough on my above comment: the task you want to achieve is only partially related to optimistic locking really.Pessimistic locking only defines the behaviour for the situation where your operation is concurrently executed and 'the last writer will win'. This is actually dangerous. It can be enabled in the 'DynamoDBMapperConfig.SaveBehavior' configuration. - Peter Branforn
@PeterBranforn My understanding is that unless you specify optimistic locking (i think the java api provides for it) the last write wins. And optimistic locking uses a version number - requiring a read before the write so you can see if the version number has changed. Id like, if possible, to just do a pessimistic style lock on the item so that no other client can attempt to do an update until the first one had completed. - Aaron Shaw
ah, now I get your question: The intermediate read is executed automatically in the ORM layer when you execute an 'update' operation. Thus you do not have to manually execute it before the update of an item. - Peter Branforn
@PeterBranforn Peter! I think my rubber-ducking of you has helped me solve my problem - as possibly you alluded to originally. My question still stands (i think) but for my use case I'm no longer sure it's actually an issue. As I need to do a read first anyway, I could possibly do a conditional update based on that. Thanks. - Aaron Shaw

1 Answers

3
votes

The locking of 'items' is provided as a client side extension in the Java SDK, you need to do some work to actually make use of it.

The reason for this is the distributed nature of the dynamo DB. Note that the lock is not a classic 'row based' lock on the 'server side'. The lock must be obeyed by the clients using the related 'items'.

The client side class to manage a lock is called 'AmazonDynamoDBLockClient'. If you search for this class you will find examples on using it from amazon SDK developers.

Things that are required to do with it:

  • Add a global ID for a lock to ask before writing an 'item'
  • Make the writer aware of locks and prevent writing in case it is 'active'
  • Timeout the lock, so that in case a client dies the lock gets released

Taken right from such a sample:

    //build a lock client. any other client _not_ using this _same_ lock
    //can still modify the data! this is not 'safe' in this regard!
    final AmazonDynamoDBLockClient client = new AmazonDynamoDBLockClient(
        AmazonDynamoDBLockClientOptions.builder(dynamoDB, "customLockTable")
                .withTimeUnit(TimeUnit.SECONDS)
                .withLeaseDuration(100L)
                .withHeartbeatPeriod(40L)
                .withCreateHeartbeatBackgroundThread(createHeartbeatBackgroundThread)
                .build());
    //try to acquire a lock on the partition key "FirstPart"
    //of course this can fail, since someone else could have the lock
    final Optional<LockItem> lockItem =
            client.tryAcquireLock(AcquireLockOptions.builder("FirstPart").build());

For the above code to work you need to create a specific 'customLockTable', this is not part of the DB backend by nature. Therefore the above sample is incomplete!