Should DynamoDB adjacency lists use discrete partition keys to model each type of relationship?

前端 未结 1 2000
渐次进展
渐次进展 2021-01-04 20:54

Context

I am building a forum and investigating modeling the data with DynamoDB and adjacency lists. Some top-level entities (like users) might have multiple types

相关标签:
1条回答
  • 2021-01-04 21:34

    Your Option 1 is not possible because it does not have unique primary keys. In your sample data, you can see that you have two entries for (Comment-A, User-Harry).

    Solution 1

    The way to implement what you are looking for is by using slightly different attributes for your table and the GSI. If Harry likes Comment A, then your attributes should be:

    hash_key: User-Harry
    gsi_hash_key: Comment-A
    sort_key_for_both: Likes-User-Harry-Comment-A
    

    Now you have only one partition key value for your top level entities in both the table and the GSI, and you can query for a specific relationship type by using the begins_with operator.

    Solution 2

    You could make the relationship a top-level entity. For example, Likes-User-Harry-Comment-A would have two entries in the database because it is “adjacent to” both User-Harry and Comment A.

    This allows you flexibility if you want to model more complex information about the relationships in the future (including the ability to describe the relationship between relationships, such as Likes-User-Ron-User-Harry Causes Follows-User-Ron-User-Harry).

    However, this strategy requires more items to be stored in the database, and it means that saving a “like” (so that it can be queried) is not an atomic operation. (But you can work around that by only writing the relationship entity, and then use DynamoDBStreams + Lambda to write entries for two entries I mentioned at the beginning of this solution.)

    Update: using DynamoDB Transactions, saving a "like" in this manner can actually be a fully ACID operation.

    0 讨论(0)
提交回复
热议问题