I’m trying to learn some aws services and creating an application to track current and past invoices. The application will need to query the invoices table to display items based on filters and also some aggregation (number of invoices per month).
I’m curious to learn more about using DynamoDB. There is some relational components to the data, for example a table with invoices and then a one to many relationship to another table that lists customers on the invoice. But I guess this can just be two separate tables with separate queries.
Some functionality I’ll want:
Basic CRUD operations in the front end. I think I can use API gateway and lambda functions for this.
return aggregate results from the table using user entered parameters.
Tracking what user made the edit. I figure I could use Cognito to handle user logins and such?
DynamoDB is nosql and requires a special data modeling approach. Trying to replicate relational schema there is inefficient (but could work for pet project).
Hi there, here’s an article about how to choose the right database that might be just what you’re looking for: https://go.aws/3V5xzSt. Additionally, our Sales team can help you with finding the best service for your use case: https://go.aws/3LtGSZg.
Forget everything you know about normalization. Data duplication is the name of the game when using Dynamo.
Indexes with composite values are used extensively to speed up queries. It works, but you need to know your usage/query patterns up front to implement it well.
So I would only have one table at the most granular level (customers on invoices) and simply duplicate the invoice attribute data for each row? When querying would the deduplication happen on the client side?
I agree with this. I originally typed up a response explaining how to do this with denormalizing Customer into every invoice item, but it’s tricky because Customer is going to be a Set. How do you query a GSI to return all invoices for which a customer N is a part of the customer set? That is difficult to do in Dynamo and an OLAP problem Dynamo does not tackle well.
First, watch YouTube videos and read articles by Rick Houlihan and Alex DeBrie to learn the basics of single table design. Essentially, don’t think in multiple tables and joins, think a single table and GSIs (kind of like views), so you want to model and query your data in a way such that you can mostly just grab it with one PK + SK or PK + SK begins_with for that access pattern. You want to “pre-join” at write time to make the reads simple, and don’t be afraid to duplicate data if a GSI isn’t enough.
You will probably also want an GSI inverted index to query on PK: CUSTOMER# with SK: INVOICE#. You could also do it the other way around. Customer as PK is actually more common but it sounds like you’ll mostly be querying by invoice. Check out: https://www.alexdebrie.com/posts/dynamodb-single-table/
If the aggregates are only done on a single attribute like timestamp, you can just create a GSI using that timestamp as SK to do a range query. If it’s more than that, you have to get fancy like creating a composite sort key of multiple attributes, duplicating attributes, sparse indexes, overloading indexes, etc. Some more info: https://www.dynamodbguide.com/querying#using-key-expressions, https://www.alexdebrie.com/posts/dynamodb-one-to-many/ You may also choose to just update the monthly counts right after inserting. For even more complicated aggregates or real-time, what people do is they use DynamoDB Streams to put it somewhere where they can use SQL or a better tool lol.
Yes, Cognito can plug into API gateway to handle auth.
Since the number of invoices is pretty small <3000, if this all sounds like too much of a pain in the ass or you have more complicated aggregates or ad-hoc queries that may require many attributes, you should probably just go relational database instead. But it sounds simple enough and if done correctly it should cost nothing on the free tier.
There is wayyyy to much to go into over Reddit, but the AWS:Reinvent videos for advanced Dynamo usage really clued me in. It really is a completely different way to think about storing and querying.
The points I made above are just broad changes in thinking required to get into Dynamo.
Thanks for your time to write out this really helpful response. Part of this project is just me wanting to learn more about DynamoDB, where I come from a more traditional RDMS background.
With a small amount of records, there is likelihood the users will just want to return all invoices - would I just use pagination if there was a query with a lot of items? I’ll check all the resources you and others have linked for sure.
If there are a lot of customers for each invoice , querying by invoice will become a giant PITA as you’ll get back a huge number of invoices. For this reason I think RDS is better at this stage for the OP as well.
ChatGPT gives a decent overview of single table design, and it can even suggest the composite keys and secondary indexes you’d want to create based on the sorts of queries you’ll be doing.
Just be careful using pagination on DynamoDB, as the fetch limit is applied before any filtering. If you limit to a fetch size of 100, for example, then filter out about 25% of the records, you’ll have only 75 records on your gui. You then need to iteratively fetch again until you hit your required actual page size.
In many cases, fetching the whole lot can be quick enough, as DynamoDB is blazingly fast on reads, but you may hit some network issues if it’s a huge amount of data
I suspect in your case you would want to fetch everything and paginate on the front-end, as the number and size of the records would not be particularly hard to handle.