Build Leaderboard with DynamoDB GSI

0

I'm going to create Leaderboard using this example as a reference. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

There are two access patterns.

  1. Get Top 100
{
    "TableName": "GameScores",
    "IndexName": "GameTitleIndex",
    "KeyConditionExpression": "GameTitle = :v_title",
    "ExpressionAttributeValues": {
        ":v_title": {"S": "Meteor Blasters"}
    },
    "ProjectionExpression": "UserId, TopScore",
    "ScanIndexForward": false,
	"Limit": 100
}
  1. Get My Rank
{
    "TableName": "GameScores",
    "IndexName": "GameTitleIndex",
    "KeyConditionExpression": "GameTitle = :v_title and TopScore > :score",
    "ExpressionAttributeValues": {
        ":v_title": {"S": "Meteor Blasters"},
	":score": {"N": myScore},
    },
    "ScanIndexForward": false,
    "Select": COUNT	
}

Is this a good idea? I'm concerned that Getting My Rank will get slower as slower as getting more and more items like millions of items.

已提问 1 个月前550 查看次数
1 回答
1
已接受的回答

DynamoDB would not be my go-to for a leaderboard, as other better alternatives exist. But i'm assuming here that you want to use a single database (DynamoDB) for your entire application....

While your first access pattern is highly efficient, your second one may not be as scalable. Imagine 1M players played the game Meteor Blasters and your rank is last place, you have to read 999,999 items to understand where you are in the list. That's going to be slow and expensive.

I would consider updating the users game rank on a daily basis for example, especially if they are not explicitly asking for it and its just used to populate a dashboard.

profile pictureAWS
专家
已回答 1 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容