'Dynamodb get items based on time and likes
Currently, I am making a 9gag.com replica, where users can see posts having more than 20 votes from the last 2 months, how can I achieve this efficiently?
Here is my current design for post table
userID | createTime | totalUpvotes | imageUrl | title
where userID is the partition key and createTime is the sort key, I also need pagination so finally here is my parameter for fetching the items:
const params = {
TableName: TABLE_NAME,
FilterExpression:
'#createTime >= :_createTime AND #totalUpvotes >= :_votes',
ExpressionAttributeNames: {
'#createTime': 'createTime',
'#totalUpvotes': 'totalUpvotes'
},
ExpressionAttributeValues: {
':_createTime': Date.now() - FEED_TIME_THRESHOLD,
':_votes': votes
},
Limit: MAX_POST_FETCH
};
if (lastEvaluatedKey) params.ExclusiveStartKey = lastEvaluatedKey;
return db.scan(params).promise();
I know scan function is not recommended, but I am thinking of using Redis to cache the posts, and re-run the scan operation every 20 minutes or so since all users should have seen the same content on my website.
Solution 1:[1]
Create an attribute popular with a value that's present only for popular items (i.e. 20+ upvotes). You can use a simple constant as the value. Then create a (sparse) GSI having popular as the PK and createTime as the SK. You can then query the GSI for popular items with a createTime greater than two months ago.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hunterhacker |
