DynamoDB, how to architect and query effectively.

I’m new to DynamoDB and NoSQL architecture. I’m trying to figure out how to structure my keys in the most efficient way. AFAICT this means avoiding scans and only doing queries.

I have a set of records, and other records related to those in a many-to-many relation.

Reading documentation, the advised approach is to use

pk sk attributes
--------------------------------------
Parent#123 Parent#123 {parent details}
Parent#123 Child#456 {child details}

I’m building an API that needs to list all parents. How would you query the above table without using scan?

My pk/sk design at the moment is this:

pk sk attributes
--------------------------------------
Parent Parent#123 {parent details}
Parent#123 Child#456 {child details}

Which means I can query (not scan) for the pk ‘Parent’.

But then, how do I ensure key integrity when inserting Child records?


Related discussions:

Use the first approach and query with begins_with Parent

Do you have any idea on how many parent items will be returned?

I’ll just ask whether you have a use case that fits a nosql database or are you trying to force it. I’ve seen customers drink the “we use dynamo for everything” cool aid but not realize that rearchitecture may be required and that Amazon has a scale that made it necessary.

Start with you data access storage, then patterns, then the db that fits (nosql, relational, whatever)

Alex debrief has a good book a dynamodb patterns which will help.

For the key constraints for adjacent records you can apply that in the application layer and/or use a condition expression

https://www.alexdebrie.com/posts/dynamodb-condition-expressions/#1-confirming-existence-or-non-existence-of-an-item

You can’t do begins_with on partition key. Only eq (equal).

For this one case, probably only a few hundred. But the overall table size won’t be a few hundred.

go with first but you need to add a second index that creates a parent lookup for you that would like what you have now. this will result in double writes

You can use a GSI. I’d create a model or type field to hold the parent type name as a string and use the GSI to find all records that have the GSI pk as model name and sort as the actual SK.

https://www.trek10.com/blog/dynamodb-single-table-relational-modeling

It’s a weekend project. I could just scan and filter everything, but learning the tool and best practices is part of the goal.

And for modeling your pk/sk you first need to think about how you will want to access your data.

For example

  1. Get all parents
  2. Get a specific parent by id
  3. Find all children for a specific parent id
  4. Get a child by id

Parent
PK parent#123
SK parent

Gsi1pk parent
Gsi1sk parent#123

Child
PK parent#123
SK child#123

Gsi1pk child#123
Gsi1sk child

To give a vague example of how this can be done without using a scan. You generally want to avoid scans in almost all cases

Yeah, transact write is where I’m leaning.

Yea, only sortKey can have expressions applied.
A pk must be an exact match

It sounds like you’re trying to used nosql for relational data. One thing which you’ll likely need to do is use the NextToken in the response to get all the items.

To answer your actual question. I’m making the assumption that you would want to get a child by first finding the parent.

pk: parent, sk:#123
pk: child, sk:#123#456

To return all parents you would do - pk: parent, sk: begins_with(“#”)

To return all children of parent 123 you would do - pk: child, sk: begins_with(“#123#”)

Yup, a transactwrite with a condition check will more or less give you a foreign key constraint

A good article but doesn’t address the integrity issue afaict.

An access pattern I need to address is “Write Child record only if Parent exists.”

OK thanks! So in your model there, your pk is effectively the record type.

How would you ensure that Child records can only be created if Parent #123 exists?

Write down your needed queries and try to estimate their usage. This will give you what should be table keys, local index(es) (if any), and global index(es).Your examples might work with added GSI.

The most used queries should hit table directly, don’t design the table primarily what’s easiest to write.

As someone mentioned: The DynamoDB book by Alex Debrie will be a great starting point to learn DynamoDB concepts.

Just choming in on single table design for dynamodb having just implemented accessed in Java - I think it suits JavaScript or python more than a strongly typed language. I spent a lot of time creating something that worked, don’t skip your integration tests: you really need them.