Now to get User and their cart we can (assuming strongly consistent reads):
Fetch all items in cart querying the User#{userId} item collection (consuming most likely 1 RCU or 2 RCU)
Fetch all related items using get item for each item (consuming n RCU’s, where n=number-of-items-in-cart)
I don’t see any better way of modelling this, one way would be to denormalise item info into UserCart but we all know what implications this would have.
So, the whole idea of using Single-Table-Design to fetch related data breaks down as soon as the data model gets in any way complicated and in our case we are consuming n RCU’s every time we need to fetch the cart.
Migrations
Now assume we do follow the data model above and we have 1 billion items of ItemInfo. If I want to simply rename a field or add a field, in on-demand mode, this is going to cost $1,250, or in provisioned mode, I need to run this migration in a way that only consumes maybe 10WCUs, it would take ~3years to complete the migration.
Is there something I’m missing here? I know DynamoDB is a popular DB but how do companies actually deal with it at scale ?
DDB gives you single digit latency, globally at very high scale. The cost is high platform $$$$ and requires skilled design and development teams with specific DDB training.
If your budget is 1250 and a primary use case is you want to make changes to your schema DDB ain’t for you.
I don’t understand why what you are presenting here is “untenable”. There are two possibilities here:
1 - You need to fetch the user cart often (in comparison to updates), and then it makes sense to denormalize ItemInfo into UserCart (which is very easy to do using DynamoDB streams, you shouldn’t do it directly from whatever writes to ItemInfo, if that’s what you mean by “we all know what implications this would have”).
2 - The cart is short-lived and is written to more than it is read (likely, users tend to add things to a cart, and then empty it by purchasing the items), and the small RCU consumption associated with that is not a problem.
In either case, you have acceptable solutions with tradeoffs depending on access pattern specifics. I don’t understand how this makes DDB potentially untenable.
Lastly, I don’t see how having to pay up to $1250 for a migration when you have an application that has 1 billion item infos is a problem. If your application has hundreds of millions of items, and probably millions of users, $1250 is a drop in a probably large budget.
Data Modelling in your version 1 is nearly always wrong. It becomes obvious very quickly where the pain points are if it’s wrong / difficult or expensive to query. Put it in and give it a spin with a reduced data set. It’s a lot easier to reason about when it’s running rather than guessing at hypotheticals. Version 2 should be what you consider going to production with.
Migrations (implicit costs too), the clue is in the pricing. Writes are 5x more expensive than Reads so they’re telling you that writes are expensive and to minimise them.
Real talk: Why not just use Postgres until you have a clear need for DDB’s strengths (massive horizontal sharded scaling)?
Option 1 - Accept that the field name isn’t quite accurate anymore
Option 2 - Take the hit to mass-migrate all your data to use the new schema. Keep in mind you can’t actually rename a field in Dynamo, you can add new ones. So you’d have to either duplicate the field with a different name and just ignore the old one, or create a whole new table and move the data and delete the old table.
Thanks for the info.
I hadn’t considered that streams can be used to keep records in sync within the table itself (also assumed they were for things like exporting to an OLAP DB/system), but good to know.
I guess in this example you would put a GSI on the `UserCart` and whenever an `ItemInfo` changes you fetch all related `UserCart` items via the GSI and perform the update?
I guess I take your point about budget however, I’m still unsure how people actually efficiently manage migrations.
I’ve worked with SQLServer, Postgres, MySQL, MongoDB etc before. To be clear, I would choose Postgres probably for any new app and only consider switching when scale starts to put too heavy a load on a traditional RDMBS. However I think I’ll be hitting that scale soon and am trying to forward plan/research. Hence my question here
And yes, I have always gone with RDMBS (MySQL/Postgres) or Mongo in the past, I am forward planning/researching using DynamoDB for large scale (also out of interest and continued learning).
Cool thanks for the info. This is kinda exactly what I’m looking for in posting my question, real experience and pragramtic solutions from people who have used the DB in anger. So, TY!
Use postgres unless you have at least a couple very experienced dynamo db people and a corporate level infra budget.
Postgres will scale very high ( aurora especially) so I would avoid heaches and just go with it unless it specifically does not meet a certain requirement.
This is actually the most important point. Ddb is not a relational database, and you can only query on partition key (and sort key), everything else is filtering. So, you need to match your data access to partition key and sort key. And/or maintain secondary indexes, which gets burdensome fast.
I honestly thing ddb is great for microservices with predictable data access. For everything else, start with postgresql and only later when your data access patterns are well understood, and your bottle necks well characterised, spin out components to use Ddb as microservices.
For migrations, it depends on application specifics, I think. We use event-sourcing at my company, and use DDB to store events, which means that whatever is written DDB is immutable, and migrations are performed in-software using versioning of events.
You’re exactly right about how I would perform the update.
Bear in mind too, that other than the PK/SK, other attributes are optional for each item in your table. So, you might have:
PK: item-id
SK: datestamp
If you want to add a Colour attribute, you can just start adding that to new records as you create them. Old records don’t need to have that attribute, and you don’t have to do a Migration to change the schema. You just start adding “Colour=” when adding a new item.
Yes, this means your code now has to handle the situation where items may or may not have a Colour. So you may (say) read an item, find it has no Colour, and update it with Colour==Beige.
This kind of approach is one of the tradeoffs you’re making by choosing DDB.
If you need what DDB does, there’s nothing better. We use it for some immutable event data and it works nicely (Cheap, fast). It depends on the use case more than anything!
Thanks u/Miserygut, good to know. Do you have any thoughts/experience/opinions on ScyllaDB and it’s DynamoDB alternator/API support?
It’s promises/claims are pretty impressive.