0

Update: In Summary

My choices are:

  1. 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.

  2. 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.

: )

Costa Michailidis
  • 7,691
  • 15
  • 72
  • 124
  • 1
    Would you consider caching as a solution? Only perform the calculation once every x seconds/minutes/hours, perhaps on a lambda invoked on schedule writing to a special DynamoDB item? – Tasos P. Apr 05 '21 at 11:29
  • Hmm... if I cache, I'd need to run a query, still inefficient, no? Also, people wouldn't be able to immediately see their contribution show up on the page, though I could fake that. – Costa Michailidis Apr 05 '21 at 17:44

0 Answers0