my problem:
I am busy developing a Vue web app using Amplify DataStore.
In the web app, when I save a new Item to the DataStore I am unable to immediately access that item using a Lambda function and AWS.DynamoDB.DocumentClient() to query the dynamoDb table using await docClient.query(params).promise(). The result is empty.
If I wait a minute or two after saving the item, I am able to access the item in my lambda function.
I assume that there is a delay in syncing the locally saved item to the backend.
The lambda function is called by a third party via an API immediately after the new item is saved in the web app.
How can I ensure that the item is available to access in my lambda function?
You should wait for a 200 response when saving the item, and then issue a strongly consistent read from DynamoDB which will ensure it returns the latest result:
ConsistentRead: true
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property
DynamoDB works on a leader/follower architecture, all writes are persisted by the leader and later replicated to the followers (in milliseconds). It's possible that you are reading from a follower and thus getting an empty response and DynamoDB has not yet replicated that write. Setting ConsistentRead to true will force the read to happen on the leader, which will return the item, so long as your save API had already returned a 200.