Basically what I'm trying to do is send data to both RDS MySQL database and a message to an SQS queue in the application layer and make sure both of them succeed.
The flow goes as follows: when a user requests an API a lambda function is called inserting data into the database and also sending a message to an SQS queue which another lambda functions pulls then sends the same data to elasticsearch. I'm using an SQS queue here to handle retries and so on.
To ensure this flow works I just need to make sure both calls to database and SQS succeed or else this whole thing fails. e.g. if database succeeds and the call to SQS queue failed the data won't make it to elasticsearch, so both of them should succeed or fail together.
Any ideas how to do this?
CodePudding user response:
Once a message is sent to an Amazon SQS queue, you can't "undo" it. Therefore, I would recommend:
- Insert the data into the database
- If it failed, retry a few times
- If all retries fail, then exit with a FAIL status
- Send the message to SQS
- If it failed, retry a few times
- If all retries fail, then reverse the insertion into the database and exit with a FAIL status
- Return a SUCCESS status