- Newest
- Most votes
- Most comments
With DynamoDB you typically store related data together, in denormalized fashion. Making multiple requests to fulfill a single access pattern is generally considered an access pattern.
pk | sk | status | other |
---|---|---|---|
user1 | message123 | Active | message payload |
user1 | message674 | Active | message payload |
user2 | message174 | Removed | message payload |
This simple data model will allow you to retrieve all of the users messages in a single Query
request, by specifying the userId:
SELECT * FROM mytable WHERE pk = 'user1'
Now, if a status is related to a user and not a users messages, then you can keep a metadata record for each user, the schema would look like this:
pk | sk | status | other |
---|---|---|---|
user1 | USER#user1 | Active | Data |
user1 | MESSAGE#message123 | message payload | |
user1 | MESSAGE#message674 | message payload | |
user2 | USER#user2 | Removed | Data |
user2 | MESSAGE#message174 | message payload |
You can still make the same single request to get the users status and messages. But it gives you a single status item to update, as its not directly related to the messages.
Update on comment
Given that a single message can go to thousands of users, you could model it like this, keeping a message item which holds the content of the message and then having an item with a pointer to the message for each user.
This will allow you to obtain all the message id's for a user, then you would need to do a BatchGetItem
to obtain the actual message payloads. But typically for systems like this, and the speed of DynamoDB, the message metadata is stored on the UI, then when a user clicks on the message you fetch the message payload GetItem
which will retrieve the message in a couple of milliseconds.
pk | sk | status | other |
---|---|---|---|
message123 | MESSAGE#message123 | Active | message payload |
message674 | MESSAGE#message674 | Active | message payload |
message174 | MESSAGE#message174 | Active | message payload |
user1 | MESSAGE#message123 | time sent | |
user1 | MESSAGE#message674 | time sent | |
user2 | MESSAGE#message174 | time sent |
Hi, DynamoDb is the right tools for such large scale.
The right question is: what are your access patterns?
- message id
- user id for sender or receivers of both?
When this is known you will be able to create the proper indexes to access your table efficiently.
I strongly recommend DDB book by Alex de Brie: https://www.dynamodbbook.com/
Have a look at his video: https://www.youtube.com/watch?v=h7mH2Bxkc6k
Thank you.
I need all messages associated with a userid
Relevant content
- asked 24 days ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 9 months ago
Thanks Leeroy. My issue is users are unique but many messages are sent to more than one user so I don't fully understand if I should counter-intuitively store multiple copies of those messages (and each message could go to many million users).
Its the message that has the status. In SQL I'd have messages in one table and userid, messageid, status in another and join the tables.
Updated my answer
Excellent suggestion. Thank you very much.