databaseamazon-web-servicesamazon-dynamodbdynamodb-queriesamazon-dynamodb-streams

{Solved} What will happen if I insert a record into DynamoDB with the same primary key as an existing record that has an expired TTL?


I currently have a record with the primary key 389 in DynamoDB, but its TTL has expired and it hasn't been deleted yet.

If I attempt to insert a record with the same primary key but with an updated TTL, will I encounter any errors?

I am expecting that the old record with expired TTL is deleted as soon as I try to insert new record with same Primary Key and an delete event is generated in DynamoDB Streams.

[Solution] old record with expired TTL will get updated.


Solution

  • If you overwrite the item (PutItem with the same key) then it will overwrite the existing TTL also.

    When DynamoDB deletes data based on TTL it does so with a ConditionExpression:

    DeleteItem if ttl < time.now()

    This condition will evaluate to false should your new TTL value be in the future, and the item will not be deleted.

    As you overwrite the item, you will never see a REMOVE event in DynamoDB Streams. You will see a MODIFY event with OLD_AND_NEW_IMAGE should that be the view you've selected for DynamoDB Streams.