By using AWS re:Post, you agree to the Terms of Use

How can I do Distributed Transaction with EventBridge?


I'm using the following scenario to explain the problem.

I have an ecommerce app which allows the customers to sign up and get an immediate coupon to use in the application. I want to use **EventBridge ** and a few other resources like a Microsoft SQL Database and Lambdas. The coupon is retrieved from a third-party API which exists outside of AWS. The event flow is:

Customer --- sends web form data --> EventBridge Bus --> Lambda -- create customer in SQL DB --get a coupon from third-party API -- sends customer created successfully event --> EventBridge Bus

Creating a customer in SQL DB, getting the coupon from the third-party API should happen in a single transaction. There is a good chance that either of that can fail due to network error or whatever information that the customer provides. Even if the customer has provided the correct data and a new customer is created in the SQL DB, the third-party API call can fail. These two operations should succeed only if both succeed.

Does EventBridge provide distributed transaction through its .NET SDK? In the above example, if the third-party call fails, the data created in the SQL database for the customer is rolled back as well as the message is sent back to the queue so it can be tried again later.

I'm looking for something similar to TransactionScope that is available in Azure. If that is not available, how can I achieve distributed transaction with EventBridge, other AWS resources and third-party services which have a greater chance of failure as a unit.

3 Answers
Accepted Answer

There is not transaction scope in EventBridge. Each target handles the events it receives in an independent way. As you do need some coordination between them I would recommend that you invoke a Step Functions state machine from EventBridge. The state machine will implement a Saga Pattern to perform both operations and roll back in case of failures.

profile picture
answered 6 months ago

EventBridge by itself is not transactional.
The Lambda can do both create User in DB and create Coupon. If this fails, you can handle this in your Lambda function and then throw an error.
EventBridge has an retry for failed Lambda functions.

answered 6 months ago
  • This situation can happen. The user is created in the DB, but the coupon retrieval has failed. Because the coupon retrieval also has failed, the created user also should be rolled back. Deleting the already created user like it was created seems to the solution if there is no transaction scope can be created around both those operations. Do you see any other option to add transaction scope?


As @Uri explained, this would be more a case for coordinating using Step Functions, here's one example of implementation

answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions