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달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠