Update: In Summary
My choices are:
Update the contribution total every time a new purchase is made. This runs the risk of one operation failing and the new complexity of adjusting the total if we give a refund or otherwise change purchases. That might be remedied by doing the occasional query to sync of the total again.
Use DynamoDB streams and update a record containing the total every time a purchase is made. Hm...
Original Question...
I have a table in DynamoDB with a large number of purchase records (in my case, contributions to a charitable cause). I want to display the total amount of funds raised thus far on a webpage.
If I simply query all the purchase records and sum the contribution amounts, I'd have my number, and that wouldn't be a problem, until I started getting a large number of contributions at which point I'd be querying an enormous amount of records every time someone loaded the webpage.
I can include in my backend logic a batch write operation, so when a purchase record goes in, an update also goes in to a record that simply stores the total... but batch write operations are not all or nothing in Dynamo, one may fail while the other succeeds.
I could use DynamoDB streams to detect every time a purchase record is created, and then run an update operation to update a record that contains the total. I can then simply get this one record.
Am I over-complicating things? Is there a better way to do this? In CouchDB, I used to just have a view that mapped and reduced database records for aggregate info like this.
: )