Transaction Correctness

tursodatabase/turso/.claude/skills/transaction-correctness

作者 tursodatabaseff97ec42cdef無授權條款24K 個星標收錄於 2026年10月9日更新於 2026年10月8日儲存庫今天更新

How WAL mechanics, checkpointing, concurrency rules, recovery work in tursodb

AI 產生的概覽

說明 Turso 的 WAL 交易機制、檢查點、並行規則與當機復原。

功能
此技能是 Turso 儲存引擎交易正確性的參考指南。它說明 WAL 的寫入與讀取路徑、檢查點類型(PASSIVE、FULL、RESTART、TRUNCATE)、取代共享記憶體檔案的記憶體內 WAL 索引、並行規則、當機復原步驟,以及連線私有狀態與共享狀態的區分。它也列出持久性、原子性、隔離性與避免更新遺失等正確性不變量。
適用情境
在分析 Turso 或 SQLite 風格的 WAL 行為時使用,例如理解檢查點、讀取標記、快照隔離或當機後的復原。適合需要概念性參考而非可執行工具的開發者與審查者。
執行需求
不含指令碼或工具,僅為說明性文件。閱讀它除代理程式外不需要任何套件、憑證或網路存取。

Transaction Correctness Guide

Turso uses WAL (Write-Ahead Logging) mode exclusively.

Files: .db, .db-wal (no .db-shm - Turso uses in-memory WAL index)

WAL Mechanics

Write Path

  1. Writer appends frames (page data) to WAL file (sequential I/O)
  2. COMMIT = frame with non-zero db_size in header (marks transaction end)
  3. Original DB unchanged until checkpoint

Read Path

  1. Reader acquires read mark (mxFrame = last valid commit frame)
  2. For each page: check WAL up to mxFrame, fall back to main DB
  3. Reader sees consistent snapshot at its read mark

Checkpointing

Transfers WAL content back to main DB.

WAL grows → checkpoint triggered (default: 1000 pages) → pages copied to DB → WAL reused

Checkpoint types:

  • PASSIVE: Non-blocking, stops at pages needed by active readers
  • FULL: Waits for readers, checkpoints everything
  • RESTART: Like FULL, also resets WAL to beginning
  • TRUNCATE: Like RESTART, also truncates WAL file to zero length

WAL-Index

SQLite uses a shared memory file (-shm) for WAL index. Turso does not - it uses in-memory data structures (frame_cache hashmap, atomic read marks) since multi-process access is not supported.

Concurrency Rules

  • One writer at a time
  • Readers don't block writer, writer doesn't block readers
  • Checkpoint must stop at pages needed by active readers

Recovery

On crash:

  1. First connection acquires exclusive lock
  2. Replays valid commits from WAL
  3. Releases lock, normal operation resumes

Turso Implementation

Key files:

  • WAL implementation - WAL implementation
  • Page management, transactions

Connection-Private vs Shared

Per-Connection (private):

  • Pager - page cache, dirty pages, savepoints, commit state
  • WalFile - connection's snapshot view:
    • max_frame / min_frame - frame range for this connection's snapshot
    • max_frame_read_lock_index - which read lock slot this connection holds
    • last_checksum - rolling checksum state

Shared across connections:

  • WalFileShared - global WAL state:
    • frame_cache - page-to-frame index (replaces .shm file)
    • max_frame / nbackfills - global WAL progress
    • read_locks[5] - read mark slots (TursoRwLock with embedded frame values)
    • write_lock - exclusive writer lock
    • checkpoint_lock - checkpoint serialization
    • file - WAL file handle
  • DatabaseStorage - main .db file
  • BufferPool - shared memory allocation

Correctness Invariants

  1. Durability: COMMIT record must be fsynced before returning success
  2. Atomicity: Partial transactions never visible to readers
  3. Isolation: Each reader sees consistent snapshot
  4. No lost updates: Checkpoint can't overwrite uncommitted changes

References

來源與署名

來源:tursodatabase/turso位於.claude/skills/transaction-correctness提交ff97ec4

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架