Yuriy Piskunov
Yuriy Piskunov

Reputation: 1224

The service threw an exception during a call to the second service within a transaction. What general solution is possible?

My problem schematically is:

  1. service A beginning transaction;
  2. service A prepared and save any data to DB
  3. service A make some (HTTP-REST) request on service B
  4. if service B success responded (and saved data), then A commit transaction
  5. If service B throws any exception - service A rollback transaction

The problem is that service B just responded slowly and service A caught a timeout exception and rolled back the transaction. Service B, however, processed the data (although it did not respond in time). When, the process is considered successful if both service A and service B have saved and processed the data. In this case, due to the rollback of the transaction, the data of service A was not saved.

Is there a general solution to this problem? I look towards 2PC and event-based distributed transactional mechanism (such as saga pattern). Thans for any help/links/etc.

Upvotes: 0

Views: 106

Answers (1)

Rob Conklin
Rob Conklin

Reputation: 9456

I generally solve this by splitting the call to Service B into a "prepare" and a "commit" phase. This is effectively putting in-place 2PC for a service layer, inserting records in a "Pending" state, and then having a final call to "Activate" the change.

The fact of the matter is, REST over HTTP is not great for transactional consistency. You are much better off with eventual consistency models when you are talking about potential long-running processes. Put a message queue behind Service B, queue the update up, and have Service A poll for completion (or have a call-back).

Upvotes: 1

Related Questions