Search code examples
amazon-dynamodb

DynamoDB table with json property or each property in a record


I have the need to store temporarily some change tracking data in DynamoDB.

Initially I thought having the following attributes:

Attributes:

  • recordId (String): Identifier for the entity.

  • lastTimestamp (Number): Timestamp of the last change.

  • changes (String): JSON string containing the change details.

    { changes: [ { timestamp: '2024-04-01 16:14:23', user: 11111, property: 'inventory', original: '0', current: '1.5'}, { timestamp: '2024-04-01 16:14:23', user: 11111, property: 'assetId', original: '123455', current: '223344'}, { timestamp: '2024-04-01 17:20:01', user: 2222, property: 'cost', original: '1.5', current: '1.0'}, ] }

Instead of creating a new object in the table I just get the item by recordId and append to the json attribute "changes" a new item.

The problem with the above is that i think is not that performant if I want to filter by timestamp of each of the changes because that timestamp lives in each changes array item.

So now Im thinking on having these attributes:

Attributes:

  • recordId (String): Identifier for the entity.
  • timestamp (Number): Timestamp of the change.
  • property (String): Name of the property that was changed.
  • original (String): Original value of the property.
  • current (String): Current value of the property.

In the above I will have to create a new DynamoDB record for each change.

Is important to mention that TTL will be set to they can expire in about 3 months.

What scenario do you think is better?

The first approach does less writes for sure with the drawback that filtering by timestamp at property level will be a problem.

The second one does a lot much writes but will be more performant for querying by timestamp.


Solution

  • The second approach is by far more efficient.

    The first approach does not do more writes, it does the same amount of writes.

    The second approach ensures you won't hit the maximum item size of 400KB while also allowing you to more efficiently read the data back.