Hey,
I’m looking for suggestions on how to better structure data in dynamodb for my use case.
I have an account, which has list of phone numbers and list of users.
Each user can have access to list of phone numbers.
Now tricky part for me is how do I properly store chats for users?
If I store chats tying them to users - I will have to duplicate them for each user having access to that number.
Otherwise I’ll have to either scan whole table, or tying to phone number - then querying for each owned number.
Whatever help or thoughts are appreciated!
one thing to keep in mind is that dynamodb operations are billed by 4kb units. so try to read and write the least amount of data per operation as possible.
Don’t use dynamo
It’s publicly known how Twitter ended up approaching this. They did indeed end up duplicating messages for each user. Akin to an email mailbox. This is despite the network being much more 1 to many than typical chat.
This has the advantage that you can scan only the tweets for the user upon an api request getting latest messages. I. E. Partition key could be user id, sort key / message ID could be a timestamp. Then you can get messages since time x cheaply. The duplication ia not expensive, large blobs can e.g. be refs to S3 and thus deduplicated.
Hello,
I’ve pulled together a few resources that I encourage reading into:
&
&
If these aren’t quite what you’re looking for, feel free to explore our additional help options, here:
- Thomas E.
You probably need to look into GSIs if the amount of users per message is finite/small.
In general DynamoDB is a NoSql database. To think of relations in a NoSql db is pointless and leads to bad data structures. Instead forget about relations and start with the query/read pattern. What is the user story you need to fulfill?
For each query pattern you create a GSI, for each write pattern you create a new idempotent write operation. By starting with the frontend/API you automatically end at the best data patterns. Data denormalization/duplication is very common under such use scenarios. That’s not bad but just NoSql.
Only if you start with a data first approach and try to do joins and fancy queries during write and read time you end up with multiple reads/writes per API call and antipattern. Don’t treat DynamoDb as you’d treat a SQL db.
Hi, I don’t fully follow the business logic, but you can utilize a global secondary index to tie users to numbers and numbers to users. Then when you create a chat you can attach it to number or numbers and get users attached to those number/numbers with gsi avoiding scanning the table
Well, I’m not the one who’s making decisions here, I have a task and I need to do it as good as possible with given limitations
Also look at and download this tool
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/workbench.html
Cool, thanks, i somehow thought duplication isn’t a great idea, since for example if I need to update last message Id need to update records fairly frequently and in bulk.
Edge case is when I have a main number which might be assigned to every user - like up to 500 users
Greatly appreciate your response! I’ll definitely look into those articles
I’m not, I also don’t know how to express account having users, it’s not relations as in sql.
And actually, relations is exactly what I’m looking to avoid here and as well - indexes.
The reason is even if I add index, I’ll still need to run query multiple times (amount of phone numbers user is assigned), since I can’t use or statement.
But as another user also mentioned that duplication is normal- I’ll definitely try to leverage it.
My only concern is that I don’t want to reach a point where I’ll have to overly duplicate data, say more than 100 times
Hi,
We’re glad to be of service! Thank you for being a part of our cloud community!
- Thomas E.
Well… you could store a list of messages under each user and point back to the original. It would save on storage but you’d still have to go back to the original record to get the message. But you could use a batch get. But your RCU count would be 2x. I think it depends on whether your messages can change. If they change, that may be the cheaper access pattern. If it’s write once, read many, full duplication would be cheaper
You’re still billed for per item in 2kb or 4kb increments (depending on operation)
So reading 1 x 2kb item cost less than 2 x 1kb items.
for better or worse, you have to break out of the “highly normalized relational data model” when using ddb. Duplication is not necessarily an anti-pattern
Don’t duplicate all messages for all users, that will never scale.
When you start a new chat, assign a chat ID, (Like a UUID), a new chat gets it own partition, same as UUID. Each chat message gets a partition key (chat id), and the range key is an epoch timestamp for the message creation time. This way, you can load all chat messages forward or backward in time. Each user has an array of chat IDs that they participate in.
Keep a separate partition, call it CHATS, range key is the chat ID for all generated chat. This way you have a record of all created chats. Use UUID v7, so chat IDs sort in chronological order.
Hmm, I’m kinda confused with chat ids, how do I then access some additional info? Like most recent message, etc…
Your sort key is going to be a timestamp based sort key so You can always go from the beginning or end
My intention is to have the last message as a part of chat record, so the messages list isn’t an issue.
Sorry I wasn’t specific enough