Distributed Transactions and Coordination

3
0

Introduction

A transaction inside one database can group related changes into a single commit or rollback. When those changes span independent databases or shards, no single local transaction controls the entire operation.

Distributed transactions introduce coordination between participants. The challenge is to reach a safe outcome even when messages are delayed, nodes fail, or parts of the system cannot communicate.

Why Coordination Is Needed

Suppose an application transfers 100 units from an account on Shard A to an account on Shard B.

The intended changes are:

Participant

Required change

Shard A

Deduct 100 from the source account

Shard B

Add 100 to the destination account

If A commits while B fails, the transfer is incomplete.

Independent local transactions do not automatically provide an all-or-nothing outcome across both shards. A distributed protocol must coordinate their decisions.

Also, distributed execution does not automatically provide a particular isolation level. Atomic commitment and isolation are separate guarantees that the system must implement.

Coordinator and Participants

A common arrangement has two roles:

  • Coordinator: Collects participant responses and manages the transaction’s outcome.

  • Participants: Perform the local work and follow the agreed commit or abort decision.

Participants need durable records of important protocol states so they can recover after failures.

Sending “commit” messages alone is insufficient. A protocol must define what happens when a message is lost, a participant restarts, or the coordinator becomes unreachable.

Two-Phase Commit

Two-phase commit, or 2PC, is a protocol for coordinating an atomic commit decision across participants.

Its phases are prepare and decision.

Phase 1: Prepare

The coordinator asks each participant whether it can commit.

Each participant checks the required conditions and prepares its local transaction.

A participant that votes yes must be able to honor a later commit decision after recovery. This generally requires durable preparation information and retaining the resources needed to finish.

A yes vote is not a completed commit. The participant has entered a prepared state.

If a participant cannot prepare, it votes no.

Phase 2: Decide

If every participant votes yes, the coordinator can durably record a commit decision and send it to them.

If a participant votes no, or preparation cannot be completed, the coordinator can decide to abort instead.

Participants record and carry out the decision, release the relevant resources, and acknowledge completion as required by the protocol.

Once a commit decision is durable, a timeout must not turn it into an abort. Recovery must preserve the chosen outcome.

A Simple Successful Transfer

The account-transfer example follows this simplified sequence:

Step

Coordinator

Shard A

Shard B

Prepare

Requests preparation

Prepares deduction; votes yes

Prepares addition; votes yes

Decide

Durably records commit

Waits for decision

Waits for decision

Complete

Sends commit

Commits deduction

Commits addition

The participants may finish at different moments, but they must follow the same final decision.

The protocol’s messages and logging add latency compared with a local transaction.

A Simple Successful Transfer.

A Simple Successful Transfer.

Why Two-Phase Commit Can Block

Suppose both shards vote yes, but the coordinator becomes unreachable before they learn the outcome.

A prepared participant cannot safely assume that a timeout means abort. The coordinator may already have committed durably, and another participant may already know that decision.

The participant must obtain authoritative outcome information through recovery or the protocol’s termination procedure. Until then, it may remain in doubt, retaining locks or other resources.

This is why classic 2PC is described as a blocking protocol.

A timeout can detect a communication problem. It cannot always reveal whether the transaction committed.

Prepared and waiting

Prepared and waiting

Consensus Is a Different Coordination Tool

Consensus protocols such as Raft and Paxos help a group of nodes agree on values or an ordered history despite certain failures.

They are often used to replicate database logs, select leaders, and maintain coordination metadata.

Consensus and 2PC address different questions:

Mechanism

Main question

Atomic commit, such as 2PC

Can all transaction participants agree to commit or abort?

Consensus

Can a replica group agree on a decision or ordered history?

A distributed database may use both. For example, consensus can replicate each shard’s state and protect coordination records.

Replicating the coordinator improves resilience, but does not remove every cross-shard failure or communication dependency.

Sagas: Coordinate Without One Global Rollback

Some workflows use a saga instead of holding one distributed atomic transaction.

A saga executes a sequence of local transactions. If a later step fails, it runs compensating actions for earlier completed steps.

For a booking workflow:

  1. Reserve a room.

  2. Reserve transport.

  3. If transport fails, cancel the room reservation.

Compensation is a new business operation, not a database rollback that erases history.

Other clients may observe intermediate states, and some actions cannot be fully reversed. Compensation can also fail and require retries or intervention.

Sagas therefore require explicit business rules rather than providing the same guarantees as an atomic transaction.

Saga compensation

Saga compensation

Retries and Safe Recovery

Distributed systems must expect retries and uncertain responses.

A client that times out may not know whether its request completed. Retrying without protection can apply the same operation twice.

Useful techniques include:

  • Idempotency keys: Recognize repeated requests for the same logical operation.

  • Durable operation records: Track progress and outcomes across restarts.

  • Recovery procedures: Resolve prepared or partially completed work.

  • Clear compensation rules: Define how failed workflows are repaired.

Idempotency helps prevent duplicate effects. It does not, by itself, make several independent changes atomic.

Summary

Distributed transactions coordinate changes across independent participants. Two-phase commit provides an atomic decision protocol, but prepared participants can block when the outcome is unavailable.

Consensus supports agreement within replica groups, while sagas coordinate workflows through local commits and compensation. Choosing an approach requires understanding the required atomicity, acceptable intermediate states, and failure-recovery behavior.

CS Core

Read Similar Blogs

Comments0