SQL Server › Transactions & ACID
How would you design a distributed transaction that needs to span operations across two different database servers, and what additional complexity does this introduce?
AdvancedAnswer
You would typically use the Microsoft Distributed Transaction Coordinator, which manages a two phase commit protocol across the involved servers, first asking each server to prepare its portion of the transaction, and only committing on both servers once all participants confirm they are ready, though this introduces additional complexity, latency, and a dependency on the coordinator service being available and properly configured.
BEGIN DISTRIBUTED TRANSACTION;
UPDATE ServerA.SalesDB.dbo.Orders SET Status = 'Complete' WHERE OrderId = 1;
UPDATE ServerB.InventoryDB.dbo.Stock SET Quantity = Quantity - 1 WHERE ProductId = 5;
COMMIT TRANSACTION;Real-world example A company synchronizing an order update across two separate database servers uses a distributed transaction to guarantee that both the order status and inventory quantity are updated together consistently, even though they live in completely separate systems.