I look at the example from the AWS documentation (shown in many, many Youtube tutorials) and can’t think of any use cases that setup would make sense or even work.
Say we’re using this for orders in an online shop. Wouldn’t work, because a customer (AccountId) can make several orders per day (CreationDate).
Say we’re using this for logs. Won’t work either, because the same source (API, CMS, app, page…) will write more than one log per day.
So for what actual real-world use case would this make sense?
-----
P.S.
I can see “fixes” for this, like adding a timestamp to the CreationDate to make it more unique, but a) it’s not 100% unique then either and b) it would destroy the point of using a date there in the first place (being able to show logs / orders / blog posts for AccountId X on day Y).
You could add a unique ID to the table (what a lot of people seem to be doing) but that’s a) a lot more complicated than with a standard SQL database, requiring external client-side programming and b) kinda ruins the point of using DynamoDB in the first place, again.
Or (and I’m afraid this is what people are actually doing?) you could update the details field instead of inserting new rows when writing multiple entries for the same Partition Key / Sort Key values… please tell me you don’t do that.
I’d say this is just high-level ‘representative’ data. You could make the order date a full ISO string or an epoch or something to guarantee uniqueness.
That example is too basic. There’s actually another example I can send you where it shows how, based on your access patterns, you should have a series of different GSIs.
Yes, you are correct, the example as it is won’t work. I think they used this to explain the concept of keys etc.
Timestamp is required in place of “CreationDate”. For a timestamp format (24/Jan/2022:22:35:44)
You can use the DynamoDB query “begins_with” to show data for AccountId X on day Y.
Usual scenarios like e-com above timestamp format will work but if your use case requires more precision, you may want to include milliseconds “dd/MM/yyyy:HH:mm:ss.SSS”.
That second video proves my point, I think. The very example he’s showing in his video at 35:15min
does NOT work with Dynamo DB.
If you try to insert keys that have identical partition and sort keys (like he does with “Bob” and “2014-10-03”) , you get an error saying “The conditional request failed. An item with the primary key you provided already exists”.
What’s the deal?
-----
Btw, the first video is the first I’ve ever seen to make sense of this concept. Thanks! The pattern it shows have nothing to do with what’s usually shown though.
EDIT: I take that back. Re-watching the first video I realized that yeah, the patterns are interesting but, again, do NOT create unique primary keys. So I’m back to the start.
But wait a second. If no-SQL databases require a unique key like SQL databases do, why is there no automatic function for it? Why cant you auto-populate one field with a 100% unique ID like you can with any SQL db?
Why add an extra layer of external programming that can fail to produce a unique ID and corrupt your data?
If no-SQL databases require a unique key like SQL databases do, why is there no automatic function for it? Why cant you auto-populate one field with a 100% unique ID like you can with any SQL db?
Remember a DDB table is structured around access patterns. If your partition key is a basically random value, how will you find that particular item later on? “Get me the item where… ?” Instead if you use a user id and timestamp, you get pretty good uniqueness with that and you have a way to access your item (“get me the item for this user id and from this time”). If you need more guaranteed uniqueness, you can put some unique string at the end of the timestamp (as suggested above) and then use `begins_with` to look at only the date or timestamp portion.
In a relational database a “random” (by which I mean a number which may not have any inherent meaning) makes more sense as primary key because the reference to it will usually be from another table where you might only need to look up the row by primary key in a join or something. So as long as you can read from one table with criteria that is human-understandable, the various joins etc can load the appropriate data from other tables where the PK doesn’t have to mean anything on its own.
But in DDB the ideal setup is a single table containing all data, and you will ideally be able to look up what you need in a single query. So the item you need must be lookup-able based purely on data that can be known in advance, and you (probably) won’t know some random unique id in advance.
Is “pretty good uniqueness” really the concept DDB is based on…? Have the client add some random stuff to keys and hope it’s gonna be good enough to not clash?
because that would force an access pattern in which you must know a random ID to access data.
That doesn’t make any sense.
a) People are doing exactly that already, as we’ve seen in this very thread. The user you named proposed it, too. Having an automatic function would have only advantages (guaranteed uniqueness, better performance, no bugs, no testing) for this concept people are already using.
b) Having an auto ID does not force any access patterns. You’d just ignore the ID for your read queries and keep using the statements you’ve been using all the time.
You’d just ignore the ID for your read queries and keep using the statements you’ve been using all the time.
That isn’t how queries work in DynamoDB.
You can only query on partitionId==value. You must know the partition ID to query. Anything else is a full table scan.
Querying with the range/sort key allows for <, >, ==, and ‘begins_with’ operations so you get more flexibility there.
People are doing exactly that already, as we’ve seen in this very thread. The user you named proposed it, too.
People are doing this on the sort key, not the partition key. Because a query must know the partition, but can use begins_with on the range key, so trailing entropy characters aren’t a limiting factor there.
Have the client add some random stuff to keys and hope it’s gonna be good enough to not clash?
It depends on your use case. It’s also important to remember the “primary key” is a combination of the ‘partition key’ and the ‘sort key’.
So you have two fields by which to uniquely identify a record.
And it can’t be random values in the partition key portion of the primary key, because you must know the partition to which to issue the query.
Addendum:
Designing a dynamoDB schema is a non-trivial exercise. It is a highly detailed exercise which often will require denormalizion and/or duplication of data. It is very different from designing a sql schema.
Like I said before, adding random stuff is not my understanding of professional database design. It seems to be what Dynamo DB people are doing.
I’m just trying to understand this thing. Right now, it looks like somebody set out to re-invent the wheel to make a name and came up with a square rock.
In case somebody comes across this question on his journey, I solved this by going with one of the solutions from the first video u/AWS_Chaos posted and added a Unix timestamp to the sort key. So yes, it’s unique now and will not break.
Looks something like this: USER#43#2022-02-01#18:36:42#1643737002915
You can read here about how it is DynamoDB came to be.
But the TL;DR; on it, it was designed to solve amazon.com’s scaling needs. It is a cornerstone of amazon.com; making much of the world’s largest e-commerce platform possible.
So a lot of the design decisions may seem strange and or counter-intuitive if you don’t have the problem they had with data scale.
It is fundamentally different from a SQL database.