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.

asked 20 days ago481 views
1 Answer
1
Accepted Answer

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
EXPERT
answered 20 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions