transaction-correctness
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTransaction Correctness Guide
事务正确性指南
Turso uses WAL (Write-Ahead Logging) mode exclusively.
Files: , (no - Turso uses in-memory WAL index)
.db.db-wal.db-shmTurso仅使用WAL(预写日志)模式。
文件:、(无文件 - Turso使用内存中的WAL索引)
.db.db-wal.db-shmWAL Mechanics
WAL机制
Write Path
写入路径
- Writer appends frames (page data) to WAL file (sequential I/O)
- COMMIT = frame with non-zero db_size in header (marks transaction end)
- Original DB unchanged until checkpoint
- 写入器将帧(页面数据)追加到WAL文件(顺序I/O)
- 提交(COMMIT)= 头部包含非零db_size的帧(标记事务结束)
- 原始数据库在检查点完成前保持不变
Read Path
读取路径
- Reader acquires read mark (mxFrame = last valid commit frame)
- For each page: check WAL up to mxFrame, fall back to main DB
- Reader sees consistent snapshot at its read mark
- 读取器获取读取标记(mxFrame = 最后一个有效提交帧)
- 对于每个页面:检查WAL中直到mxFrame的内容,若未找到则回退到主数据库
- 读取器在其读取标记处看到一致的快照
Checkpointing
检查点机制
Transfers WAL content back to main DB.
WAL grows → checkpoint triggered (default: 1000 pages) → pages copied to DB → WAL reusedCheckpoint 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内容回写到主数据库。
WAL grows → checkpoint triggered (default: 1000 pages) → pages copied to DB → WAL reused检查点类型:
- 被动(PASSIVE):非阻塞,在活跃读取器需要的页面处停止
- 完全(FULL):等待读取器完成,对所有内容执行检查点
- 重启(RESTART):与完全检查点类似,同时将WAL重置到起始位置
- 截断(TRUNCATE):与重启检查点类似,同时将WAL文件截断为零长度
WAL-Index
WAL索引
SQLite uses a shared memory file () for WAL index. Turso does not - it uses in-memory data structures ( hashmap, atomic read marks) since multi-process access is not supported.
-shmframe_cacheSQLite使用共享内存文件()作为WAL索引。Turso不采用这种方式——由于不支持多进程访问,它使用内存中的数据结构(哈希表、原子读取标记)。
-shmframe_cacheConcurrency 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:
- First connection acquires exclusive lock
- Replays valid commits from WAL
- Releases lock, normal operation resumes
崩溃时:
- 第一个连接获取排他锁
- 从WAL中重放有效的提交
- 释放锁,恢复正常操作
Turso Implementation
Turso实现细节
Key files:
- WAL implementation - WAL implementation
- Page management, transactions
关键文件:
- WAL实现 - WAL实现代码
- 页面管理、事务处理
Connection-Private vs Shared
连接私有 vs 共享资源
Per-Connection (private):
- - page cache, dirty pages, savepoints, commit state
Pager - - connection's snapshot view:
WalFile- /
max_frame- frame range for this connection's snapshotmin_frame - - which read lock slot this connection holds
max_frame_read_lock_index - - rolling checksum state
last_checksum
Shared across connections:
- - global WAL state:
WalFileShared- - page-to-frame index (replaces
frame_cachefile).shm - /
max_frame- global WAL progressnbackfills - - read mark slots (TursoRwLock with embedded frame values)
read_locks[5] - - exclusive writer lock
write_lock - - checkpoint serialization
checkpoint_lock - - WAL file handle
file
- - main
DatabaseStoragefile.db - - shared memory allocation
BufferPool
每个连接私有:
- - 页面缓存、脏页、保存点、提交状态
Pager - - 连接的快照视图:
WalFile- /
max_frame- 该连接快照的帧范围min_frame - - 该连接持有的读取锁槽位
max_frame_read_lock_index - - 滚动校验和状态
last_checksum
跨连接共享:
- - 全局WAL状态:
WalFileShared- - 页面到帧的索引(替代
frame_cache文件).shm - /
max_frame- 全局WAL进度nbackfills - - 读取标记槽位(带有嵌入帧值的TursoRwLock)
read_locks[5] - - 排他写入锁
write_lock - - 检查点序列化锁
checkpoint_lock - - WAL文件句柄
file
- - 主
DatabaseStorage文件.db - - 共享内存分配池
BufferPool
Correctness Invariants
正确性不变量
- Durability: COMMIT record must be fsynced before returning success
- Atomicity: Partial transactions never visible to readers
- Isolation: Each reader sees consistent snapshot
- No lost updates: Checkpoint can't overwrite uncommitted changes
- 持久性:COMMIT记录必须在返回成功前完成fsync同步
- 原子性:部分完成的事务对读取器不可见
- 隔离性:每个读取器看到一致的快照
- 无更新丢失:检查点不能覆盖未提交的更改