Junior — Senior
Transaction inconsistency: database record and HTTP request sending
livecode
Task condition
The example shows code that saves a computed value to a database and immediately attempts to send it to an external system via an HTTP request. The operation appears atomic, but in reality, atomicity is violated: the database write occurs within a transaction, but the network request is executed after its completion. If the network call fails, the write is already committed, and the system may end up in an inconsistent state.
@Transactional
public void calculate(GetFilesInfoRequest request) {
double divisor = 6.85;
double commissionRate = 5.2534;
double result = (count * commissionRate) / divisor;
repo.save(result);
try {
// HTTP POST
notifier.sendCalculate(result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
The task is to discuss why this approach does not guarantee atomicity, what problems may arise, and how to solve them (for example, using two-phase commit, outbox pattern, compensating transactions, etc.).