Skip to content

How to Save a List of a Complex Class in DynamoDB?

0

Using an example scenario of a Question Forum such as this one. I have a Question class which contains fields such as QuestionId, Title, Description, and ResponseList. ResponseList is a List of Response which includes fields such as Username, Content.

Example JSON of a record in DynamoDB may look like: { QuestionId: "1000111", Title: "How to Save a List of Complex Class in DynamoDB", Description: "Some Description of json", ResponseList: [ {Username: "Member1", Content: "Use SaveAsync()"}, {Username: "Member2", Content: "Use typeconverter"} }

There are 2 ways of saving new Responses to a question:

  1. Save all responses into the ResponseList as one record
  2. Save each response as a separate record under a separate Sort Key in this format QuestionId#Response#ResponseId

Which method would be prefered if we expect the ResponseList to be an append-only list? (We're not going to edit, delete, or remove each Response)

asked 2 years ago276 views

1 Answer
1
Accepted Answer

When dealing with an append-only list of responses in a DynamoDB-based question forum, the preferred method would be to save all responses into the ResponseList as one record. This approach is more suitable for your scenario for several reasons:

  1. Simplified Data Model: Keeping all responses in a single record maintains a simpler data model, which is easier to manage and query.

  2. Atomic Updates: DynamoDB allows you to update specific attributes of an item. You can use the UpdateItem operation with a list append action to add new responses to the ResponseList without retrieving and rewriting the entire item.

  3. Read Efficiency: When you need to display a question with all its responses, you can retrieve all the data in a single read operation, which is more efficient than making multiple queries to fetch individual responses.

  4. Cost-Effective: Since DynamoDB charges based on read and write capacity units, having fewer, larger items can be more cost-effective than many smaller items.

  5. Scalability: DynamoDB can handle items up to 400KB in size, which should be sufficient for most question-answer scenarios. If you expect an extremely large number of responses that might exceed this limit, you may need to consider alternative approaches.

To implement this approach using DynamoDB, you can use the DynamoDBMapper class in the AWS SDK. When adding a new response, you would:

  1. Retrieve the existing Question item.
  2. Append the new Response to the ResponseList.
  3. Save the updated Question item back to the table.

The DynamoDBMapper provides methods like save() or saveAsync() that can handle complex objects, including lists of custom types. You may need to use a custom TypeConverter for the Response class if it's not a standard DynamoDB type.

Remember to use optimistic locking with a version attribute to handle concurrent updates if multiple users might be adding responses simultaneously. This ensures that updates don't overwrite each other unintentionally.

This method is preferred for append-only scenarios where you don't need to edit or delete individual responses frequently. It provides a good balance of simplicity, performance, and cost-effectiveness for your question forum use case.

Sources
DynamoDBMapper Class - Amazon DynamoDB
Community | Safe List Updates with DynamoDB

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.