0
votes

In a new DynamoDB table, my use cases are already fulfilled by the following key schema design:

  • partition key: user_id
  • sort key: entity_id

enter image description here

Basically, access patterns are:

  1. Get specific post by a specific user.
  2. Get specific comment by a specific user.
  3. List all posts by specific user.
  4. List all comments by specific user.
  5. List all entities (post or comment) by a specific user.

What benefits do I get if I use a more random ID as partition key instead and simply use GSIs for my access patterns above?

  • partition key: pseudo_random_id (This is going to be a UUID in reality. Please ignore that this is NOT a UUID in the illustration).
  • GSI:
    • partition key: user_id
    • sort key: entity_id

enter image description here

2
FYI, your example pseudo_random_id values are not UUIDs. The term UUID has a very specific standardized meaning. A UUID is a 128-bit value, presented to humans in canonical format as 32 hex characters grouped by 4 hyphen characters. Example: 403fc3f4-9bb9-11e9-a2a3-2a2ae2dbcce4. Please edit the title and body of your Question to make clear if you meant UUID or something else. - Basil Bourque

2 Answers

1
votes

You don’t need UUIDs or any pseudo-random ID.

It was once possible that you could have a hot partition if one user is particularly active, but hot partitions are basically a non-issue now because of DynamoDB’s adaptive capacity. Furthermore, you should probably be limiting how fast users can create comments/posts, which would prevent hot partitions even if adaptive capacity didn’t exist.

(Why should you limit the rate a user can post? You don’t want a malicious actor to be able to create a new post every few milliseconds—you should have some sort of rate limit as a protection against denial of service attacks.)

1
votes

Using a UUID doesn't do anything for you...

It doesn't matter how random the partition key is. All that matters is how many distinct partition keys you have and the volume/velocity of entries for that partition key.

In other words, a unique value is a unique value. Dynamo doesn't care if it's 16 bytes, 36 bytes or 128 bytes.

Dynamo applies it's own hash to the partition key to determine which partition the data will be placed into.