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ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ