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.

posta 10 mesi fa465 visualizzazioni
2 Risposte
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
ESPERTO
con risposta 10 mesi fa
profile picture
ESPERTO
verificato 10 mesi fa
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.

con risposta 10 mesi fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande