AWS Amplify DataStore time to sync new item to backend so it is accessible in a Lambda function?

559 Views Asked by At

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?

2

There are 2 best solutions below

1
On

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.

0
On

found a solution, I use DataStore.observe to observe changes in the item (model) that I have saved - msg.opType === 'INSERT' is triggered on first saving (in local storage) and msg.opType === 'UPDATE' when the item is written in the backend dynamoDb table.

Also, these attributes and values are returned in update

              "createdAt": "2022-12-23T13:23:57.978Z",
              "updatedAt": "2022-12-23T13:23:57.978Z",
              "owner": "test",
              "_version": 1,
              "_lastChangedAt": 1671801837996,
              "_deleted": null

and not in first save. So by using a conditional statement I can ensure that the observed item matches mine and then I can trigger my code knowing that the data is available in the dynamoDb table.

Hope this helps anyone coming across a similar issue as mine.