Choice PK and SK in DynamoDB

0 votes

I have the following two items which I need to store in dynamodb

  • Artists

  • Songs

The artists have an ID (unique for artists), name and gender. The Songs have an ID (unique for songs), title, genre, artist and rating.

How should I model this in DynamoDB?.

I was thinking about this: ID as the primary key and having a sort key that contains the artist or song so they are distinguished. Is this a good choice? For example, I find I see more variety in the sort key.

What about the field artist in song items? Should I just point to the ID of the artist?

Apr 12, 2022 in IoT (Internet of Things) by Rahul
• 9,690 points
1,344 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

One thing I found out (the hard way) about NoSQL (DynamoDB) modelling is that you need to know all your access patterns before you model your table. In an RDBMS, it's rather common to model first and optimize indexes later as access patterns change. This is not straightforward in NoSQL modelling (otherwise there would be massive migrations from RDBMS to NoSQL).

Having that said, I will now suggest a simplistic model and I will update my answer as the question is updated with access patterns (i.e. "I need to get all songs for an artist").

Artist:

PK: Artist-<Artist ID>, i.e. Artist-1234 SK: <Name> Attributes: Gender etc.

Song:

PK: Song-<Song ID>, i.e. Song-5678 SK: <Genre> Attributes: Genre, Artist ID, Rating

This approach will only allow you to get your entities by using their ID.

While it's common in NoSQL to de-normalize data (i.e. store artist data in song) for easiest/more efficient access, I'd go with storing the artist ID because it allows easier updates and better consistency.

answered Apr 13, 2022 by Aditya
• 7,680 points

edited Mar 5

Related Questions In IoT (Internet of Things)

0 votes
0 answers

Understanding IoT Rules of DynamoDB and Lambda in AWS

I am currently going through the "Quick ...READ MORE

Sep 27, 2018 in IoT (Internet of Things) by