James
James

Reputation: 1260

Python: how to get position/rank after sorting using dynamodb?

I am currently trying to use the dynamodb extension in python to get the position\rank in a descending query on an index, the following is what I have come up with:

router.get('/'+USER_ROUTE+'/top', (req, res) => {
    POST.query()
    .usingIndex('likecount')
    .attributes(['id', 'likecount'])
    .descending()
    .loadAll()
    .exec((error, result) => {
        if (error) {
            res.status(400).json({ error: 'Error retrieving most liked post' });
        }

        //convert res to dict and sort dict by value (likecount)

        res.json({position:Object.keys(result).indexOf(req.body.id)});
    });
});

As you can see, I would convert the result into a dict of ID and likecount and then sort the dict after which I get the index of the key I am looking for. Obviously this fails in multiple respects, it is slow/inefficient (iterates over every item in the database per call) and requires multiple steps.

Is there a more succinct method to achieve this? Thanks.

Upvotes: 0

Views: 603

Answers (2)

Phan Việt
Phan Việt

Reputation: 1343

DynamoDB is not design for leaderboard. You should choose another DB for this purpose. I suggest you use "DynamoDB" and "Redis" in your case. You can ref this document to know about Redis leaderboard: https://redislabs.com/redis-enterprise/use-cases/leaderboards/

Workflow:

  1. Setup Dynamo Stream => trigger event to Lambda function => Lambda function will calculate the "liked count" and insert to Redis (e.g: hash is "id" and score is "liked count")
  2. When Client touch an action that affect to "liked count" => insert/update a record in DynamoDB => then Redis will update "liked count" immediately because of Dynamo Stream.
  3. Client query leaderboard => API will query from "Redis" and return to Client

Upvotes: 1

Collin Dauphinee
Collin Dauphinee

Reputation: 14003

There isn't a better way to do this. DynamoDB isn't an appropriate database to use in scenarios where you need complex querying capabilities.

Additionally, a single query operation has a strict limit on how many documents it can read; even if it did support this kind of querying, it wouldn't work for scenarios where you have more documents than it can process in a single read.

Upvotes: 1

Related Questions