Course contentsShow
← System Design
Lesson 574 of 1,91916. Distributed TransactionsFree lesson

Recovery Protocols and Logs

How write-ahead logs enable recovery from coordinator or participant failures.

Recovery Protocols and Logs

You'll learn how write-ahead logs enable systems to recover gracefully when coordinators or participants crash during two-phase commit.

The Problem: Crashes During 2PC

Distributed transactions involve multiple steps across machines. If a coordinator or participant crashes mid-transaction, we need a way to know what state they were in so we can complete or abort the transaction correctly after restart.

Without memory, a recovered node would be lost—did it already vote? Did it commit? The transaction would hang forever.

Write-Ahead Logs (WAL)

A write-ahead log is a durable record written to disk before performing any action. Think of it like a flight recorder—every decision is logged before acting on it.

Coordinator logs:

  • "Sending PREPARE to all participants"
  • "Received YES from Participant A"
  • "Received YES from Participant B"
  • "Sending COMMIT to all"

Participant logs:

  • "Received PREPARE request"
  • "Voted YES (holding locks)"
  • "Received COMMIT, transaction complete"

Recovery in Action

If a coordinator crashes after sending PREPARE but before COMMIT, it reads its log on restart. If all participants voted YES, it resends COMMIT. If any voted NO or didn't respond, it sends ABORT.

If a participant crashes after voting YES, it reads its log, sees it's waiting for a decision, and contacts the coordinator (or other participants) to learn the outcome. Its locks are still held because the log proves it promised to commit.

The log is the source of truth—it survives crashes and enables deterministic recovery.

Key Takeaway:
Write-ahead logs record every 2PC step to disk before executing it, allowing coordinators and participants to resume exactly where they left off after crashes.