DynamoDB Strongly Consistent Reads, not working all the time

0

In my Node.js application using '@aws-sdk/lib-dynamodb', I'm querying my DynamoDB table with a Local Secondary Index (LSI) and the primary key (PK), while setting ConsistentRead to true. Despite the fact that the desired record was created at least 4 minutes ago according to my log, I am unable to retrieve the specific record I'm querying for. I've double checked on both the PK and Secondary Index and making sure they are correct. I'm really clueless now.

Are there any considerations and use cases for enabling ConsistentRead: true in DynamoDB, and are there any specific scenarios or configurations that need to be in place to ensure consistent reads?

below is my code in Typescript

const queryCommandInput: QueryCommandInput = {
      TableName: process.env.DB_TABLE_NAME,
      IndexName: 'LSI1',
      KeyConditionExpression: '#PK = :PK AND begins_with(#SI1, :SI1)',
      ExpressionAttributeNames: {
        '#PK': 'PK',
        '#SI1': 'SI1',
        // '#CreatedAt': 'CreatedAt',
      },
      ExpressionAttributeValues: {
        ':PK': `MERCHANT_ACC_TRNX#${req.merchantAccId}`,
        ':SI1': `MERCHANT_ACC_TRNX#${trnxDate}#${Number(req.amount)}#false#${req.senderName}`
      },
      ConsistentRead: true,
      Limit: 1
    };
  
    const queryCommand = new QueryCommand(queryCommandInput);
    const queryCommandOutput = await DBClient.send(queryCommand);
    return queryCommandOutput.Items?.[0] as MerchantAccTrnx.Item;
  • While the query seems to be correct, you might want to try searching for a specific record in a very simple format with no parameters or anything. Just to eliminate anything else.

已提问 10 个月前465 查看次数
2 回答
1

A strongly consistent read is guaranteed to retrieve the latest item view from the table. Some things to check:

  1. You got a 200 Success for the write of the item before querying
  2. No other process is removing the item (stream consumer/TTL)
  3. Be sure to log your variables to ensure they are resolving as you'd expect.

Failing both of those, if you can provide me with reproducible code I would be happy to test it.

profile pictureAWS
专家
已回答 10 个月前
profile picture
专家
已审核 10 个月前
0

I'm sure on 1 & 2 and have added more details into logging to ensure they are resolving as I expect. Will update the details here again when the issue happen again.

FYI this issue only happen once or twice every once in a while and I'm not able to reproduce on local development as well. It is working perfectly as expected when I tested my code on local.

已回答 10 个月前

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

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

回答问题的准则