Currently, I am experimenting with a DynamoDB-based voting system with this single-table design:
- A vote item Primary Key is composed of a partition key "election_id", the sort key is a "voter_id", and an attribute "candidate" has a value of "candidate_id".
- A count item Primary Key is composed of a partition key "election_id" the sort key is the "candidate_id", and the attribute "count" contains the current count of votes for a given candidate for a given election.
The system allows voters to overwrite a previous vote item until the voting period is complete.
I have been unable to find a durable means to transactionally decrement the count for the previous candidate's count item, increment the count for the new candidate's count item, and overwrite the vote item with the newly selected candidate_id value.
While I have tested with DynamoDB Streams, I have also read opinions indicating Streams are not an uncompromisingly reliable means to keep track of counts -- although the previous value and new value "could" be used to subsequently increment/decrement counts.
Is there a means to test for an existing item and using that information, increment/decrement the respective counts, all within a transaction?
Or is there a more fundamental approach I am overlooking in solving this problem?