Hi I've a Lambda function treated as a webhook. The webhook may be called multiple time simultaneously with same data. In the lambda function, I check if the transaction record is present in the DynamoDB or not. If it's present in db the Lambda simply returns otherwise it execute further. The problem arises here that when checking if a record in db the Lambda get called again and that check fails because the previous transaction still not inserted in db. and transaction can get executed multiple times. My question is how to handle this situation. will SQS be helpful in this situation?
You can use optimistic locking for this. I've written a more detailed blog about implementing it, but here are the core ideas.
For each item you track a version number that always gets incremented. Each update to the item will increment the version number by one.
When you want to perform an update, you first read the old item and store its version number locally. Then you change the item locally and increment its version number. When you write it back to the table in your transaction, you add a conditional write. The condition being that the current version number of the item is still the same it was when you read it.
This means the transaction will fail if the item has been update in the mean time. Optimistic Locking helps you with collision detection and is a good solution under the assumption that such collisions are relatively rare. You'd be better served with different locking strategies if they're more frequent.
Optimistic Locking will help you identify the cases you're worried about. It doesn't resolve them, you'll have to implement that yourself. A common conflict resolution approach would be to read the item again and check if your changes have already been applied.