3

I'm working on a website that mostly displays items created by registered users. So I'd say 95% of API calls are to read a single item and 5% are to store a single item. System is designed with AWS API Gateway that calls AWS Lambda function which manipulates data in DynamoDB.

My next step is to implement voting system (upvote/downvote) with basic fetaures:

  • Each registered user can vote only once per item, and later is only allowed to change that vote.
  • number of votes needs to be displayed to all users next to every item.
  • items have only single-item views, and are (almost) never displayed in a list view.
  • only list view I need is "top 100 items by votes" but it is ok to calculate this once per day and serve cached version

My goal is to design a database/lambda to minimize costs of AWS. It's easy to make the logic work but I'm not sure if my solution is the optimal one:

  • My items table currently has hashkey slug and sortkey version
  • I created items-votes table with hashkey slug and sortkey user and also voted field (containing -1 or 1)
  • I added field votes to items table
  • API call to upvote/downvote inserts to item-votes table but before checks constraints that user has not already voted that way. Then in second query updates items table with updated votes count. (so 1 API call and 2 db queries)
  • old API call to show an item stays the same but grabs new votes count too (1 API call and 1 db query)

I was wondering if this can be done even better with avoiding new items-votes table and storing user votes inside items table? It looks like it is possible to save one query that way, and half the lambda execution time but I'm worried it might make that table too big/complex. Each user field is a 10 chars user ID so if item gets thousands of votes I'm not sure how Lambda/DynamoDB will behave compared to original solution.

I don't expect thousands of votes any time soon, but it is not impossible to happen to a few items and I'd like to avoid situation where I need to migrate to different solution in the near future.

1 Answer 1

3

I would suggest to have a SET DynamoDB (i.e. SS) attribute to maintain the list of users who voted against the item. Something like below:-

  • upvotes : ['user1', 'user2']
  • downvotes : ['user1', 'user2']

When you update the votes using UpdateExpression, you can use ADD operator which adds users to SET only if it doesn't exists.

ADD - Adds the specified value to the item, if the attribute does not already exist. If the attribute does exist, then the behavior of ADD depends on the data type of the attribute:

If the existing data type is a set and if Value is also a set, then Value is added to the existing set. For example, if the attribute value is the set [1,2], and the ADD action specified [3], then the final attribute value is [1,2,3]. An error occurs if an ADD action is specified for a set attribute and the attribute type specified does not match the existing set type. Both sets must have the same primitive data type. For example, if the existing data type is a set of strings, the Value must also be a set of strings.

This way you don't need to check whether the user already upvote or downvote for the item or not.

Only thing you may need to ensure is that the same user shouldn't be present on upvote and downvote set. Probably, you can use REMOVE or ConditionExpression to achieve this.

6
  • Thanks, that clears out one part of my doubts, how to deal with registering votes. But second part remains: where are the limits and are there any performance issues with registering thousands of votes that way? With 1000 votes x 10 chars per userId it's about 1KB of data. I think DynamoDB limit is 400K per record. If I reserve 100KB for item data, that leaves me 300KB for votes which would mean limit is at about 300.000 votes. It's way above what I need but what about query performance? Feb 22, 2017 at 14:07
  • Also, to display count of votes, I'm wandering should I just count items in set every time I get an item (95% of requests) or I can (in the same pass with updating vote) calculate totals and store it as attribute of the item? Feb 22, 2017 at 14:08
  • Ans1: It should not impact performance as you have the required hash key to query the item (which is the most efficient way to query). As long as the item size doesn't exceed 4KB, it shouldn't create any problem. Ans 2: I would count the votes at client side. The JavaScript has functions to count the number of values in an array. No need to maintain separate count attribute and create additional overhead to maintain it. Feb 22, 2017 at 15:02
  • you said "As long as the item size doesn't exceed 4KB, it shouldn't create any problem."... you meant 400KB? Not sure I understand where this 4K is coming from. As for calculating votes on the client, that would mean to pull all user names from DB and API to client. It will work for a very long time, but for general discussion, in case most popular items get thousands of votes, would benefits of simplifying DynamoDB query be lost over increased bandwidth (10K of data only to make array.count on the client)? Feb 22, 2017 at 15:39
  • Yes, I meant 400kb. I see your point reg count. In this case, you can maintain count attribute separately. Feb 22, 2017 at 15:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.