How should I structure this relationship in DynamoDB?

I have albums that by default are private. I want to be able to give certain users access to albums and store those permissions in a DynamoDB table.

My first instinct is to have a table where albumName is the hash key and userId is the sort key - this would allow me to easily return all users who have permission to view an album, correct? And I would also be able to easily check if a specific user has access to the album as well.

However, as a secondary option I would like a user to be able to easily see all of the albums they have access to. I could accomplish this by adding an attribute userId (or use the sort key if possible) as a GSI and then query that GSI when I need the list of albums a user has access to.

Does this sound like good database design to those who are more advanced DynamoDB users? Or is there a way that would be more efficient that I’m not thinking of?


Related discussions:

In my experience with dynamodb, or really any no-sql dB, it is okay to duplicate data where necessary as long as you build the right triggers to update the relationships.

Your first table (album-user) sounds good since a single album may eventually be shared with a million users or so.

But for the second, if you use the sort key, dynamodb will have to look through the whole index each time you look through and try to collect all albums for a given user. This will be painful since there’s no guarantee that you will get all your desired results in one fetch.

My recommendation would be to create a set on the user object with album keys. And then do a batchget for all album keys when you get the user.albums set. Just make sure you add/remove from this set everytime the album-user is updated.

Wouldn’t adding a GSI on the album-user table would work as well? The GSI could specify userId as the hash key and albumName as the sort key and all albums for a particular user can be retrieved by querying the GSI.

Oh, I was under the impression that you couldn’t use primary partition key as a sort key for another index? I can’t seem to find anything to back this up anymore, so you are correct. If read/write capacity increase is not a concern for the table… this could be a perfectly valid solution as well.

As far as I know there aren’t any restrictions on what attributes can be used as the hash and range keys in a GSI. Conceptually, the GSI is almost like a second table that gets automatically updated when the first table is modified. As you mentioned, you’ll have to provision additional read and write capacity, but those can be set separately for the GSI, if it’s read less frequently (not sure what the impact of having a low write capacity on the GSI and a high write capacity on the table would be when there are a lot of writes; could cause some weird problems).

You’re correct the GSI can have a separate partition key than the table (LSI use the same partition keys as the table). The mis-match write capacity will show up if you are projecting all of the attributes onto the GSI.

Did you consider a hot partition scenario?

3 tables:

  • Accounts
  • Albums
  • Permissions

The permission table would store the authorization between account and album. This would scale for users with tons of albums or maybe you want to see who all has permission to see a specific album. Either way, this is ideal IMO.