0
votes

How do we handle the case in dynamoDB when there is a older put request for a key for which there has been a newer delete operation already performed.

Since the newer delete operation has already deleted the record, the older put request can simply write the record again which is not correct.

Does DynamoDB save any metadata for recently deleted records? Is there any way to handle this case in DynamoDB? Or anyone has any suggestion how to handle this case.

I am using high level DynamoDBMapper instead of low level API.

2
I'm not sure if I followed: you want to keep track of every primary key you've deleted so that those keys are never inserted again, is that it?Renato Byrro
@RenatoByrro yes correct. That I want to achieve without having an extra attribute in the table. Also I can not use TTL attribute for this use case, as I already have a TTL attribute.Sanjib

2 Answers

1
votes

Dynamo doesn't keep any 'metadata' about what has been previously deleted. Considering you can't create a new attribute to keep track of deleted status, the only two ways I can think for you to handle this are:

Option 1: create your own 'metadata' table

Create a separate table to keep track of everything you deleted. You'd have a main table where you store your regular data, and a main_deleted table, where you store only primary_keys that have been deleted from the main table.

Before inserting any item in the main table, check for the primary_key in the main_deleted table. If it's there, do not proceed with the insert.

Option 2: use the range_key

If your items have a sort key, you could use it to flag items as deleted without creating a new attribute. Suppose you have this item where the range_key is a UNIX timestamp:

{
    "primary_key": "example",
    "timestamp": 1234567890,
    "other": "stuff"
}

Instead of deleting the item primary_key=example, remove all its attributes and set the timestamp to a value that you'd never use for regular items, such as 0. When inserting or updating items in the table, use a condition expression or query the database previously to check if the item was not deleted before (in other words, timestamp=0).

I'm sure there will be plenty of other ways and maybe (or probably) those two above aren't the best ones. Use your creativity! ;)

0
votes

I believe you can use ConditionExpression to check whether the data is already exists before updating (i.e. putting) the item. Both UpdateItem and PutItem have ConditionExpression to check some condition before performing the action.

Refer this conditional writes

In your case, you need to check whether the hash key exists before performing the update operation.