When implementing distributed Snapshot Isolation on a KV store, use Percolator's `data/lock/write` columns and lazy Primary-lock resolution.
TL;DR30-second simplified summary▾
Prewrite checks for newer writes or active locks; Primary Commit atomically swaps lock for write(commit_ts) -> start_ts; expired Secondary locks resolve lazily via Primary status.
start_ts isolates readers from concurrent uncommitted writes.P · Problem & Why Classic 2PC Coordinators Block.
In traditional 2PC, Coordinator C asks Shard 1 and Shard 2 to PREPARE. Both write PREPARED to disk and hold row locks.
If Coordinator C crashes before sending COMMIT, Shard 1 and Shard 2 cannot safely commit or abort on their own—they are stuck holding locks indefinitely, blocking every other transaction touching those keys!
Stateful 2PC Coordinator (Blocks on Crash) vs. Percolator Primary Lock
- Coordinator crash blocks locks
- Participants stuck in-doubt
- Separate central TX log
- Primary Key is single commit point
- Lazy roll-forward / rollback
- Zero central TX coordinator
R · Representation: `data`, `lock`, and `write` MVCC Columns.
Every key in Percolator has three logical column families indexed by timestamp:
(1) data @ start_ts: holds the actual uncommitted/committed value; (2) lock @ start_ts: holds {primary_key, lock_type, ttl} while a transaction is in flight; (3) write @ commit_ts: holds a pointer -> start_ts once committed (making data @ start_ts visible to reads with read_ts >= commit_ts).
Three RocksDB Column Families Per Key
I · Invariant: Prewrite Conflict Checks & Atomic Primary Commit.
During Prewrite(start_ts), each involved shard checks two conditions in a single local Raft transaction: (1) Is there any write at ts >= start_ts? (Write-write conflict -> Abort!); (2) Is there any existing lock on the key? (Contention -> Wait/Resolve/Abort!).
If both are clear, the shard writes data(start_ts) = val and lock(start_ts) = {primary: A, ttl}. Once all shards ack Prewrite, the client fetches commit_ts from TSO and sends Commit(start_ts, commit_ts) to Primary Key A alone.
TSO(start_ts) -> Prewrite A & B -> TSO(commit_ts) -> Commit Primary A -> Async B
M · Mechanics at Scale: Lazy Lock Resolution & TSO Batching.
What if a client commits Primary A (write @ 15), then crashes before committing Secondary B (lock @ 10)?
When Reader R later scans Key B at read_ts = 20, it sees lock @ 10 (with primary: A). If the lock's TTL has expired, Reader R checks Key A: (1) If Key A has write(commit_ts=15) -> 10, the transaction committed—so Reader R rolls forward Key B by writing write(15) -> 10 and deleting lock @ 10! (2) If Key A still has lock @ 10 or a rollback record, Reader R rolls back both Key A and Key B.
Reader Sees Expired Lock on B -> Inspect Primary A -> Roll Forward or Rollback
- Problem
- How to run distributed ACID Snapshot Isolation across sharded KV without central coordinator stalls.
- Naive Solution
- Use classic coordinator 2PC with per-transaction WAL logs on a central manager.
- Why It Fails
- Coordinator crashes leave locks stuck indefinitely and centralize I/O.
- Alternatives
- Classic 2PC vs. Percolator Primary-Lock 2PC vs. Spanner TrueTime.
- Engineering Decision
- Use Percolator Primary-Lock 2PC with a batched monotonic TSO and lazy TTL-based lock resolution.
- Trade-Offs
- Requires a single TSO service for global timestamps and extra Prewrite+Commit RTTs (unless 1-RTT Async Commit is enabled).
E · Edge Cases, Assumptions & Limitations.
Standard Percolator provides Snapshot Isolation (SI), NOT full Serializable isolation—concurrent transactions reading disjoint keys and writing overlapping invariants can still experience Write Skew unless pessimistic SELECT FOR UPDATE locks are acquired.
Assumptions & Limitations: Relies on a highly available Raft-backed TSO (if TSO loses quorum, new transactions pause until TSO leader re-elects). Also, if a slow client stalls longer than its Primary lock TTL, a concurrent reader could roll back its Primary lock before it sends Commit—which is why commit_primary() MUST verify lock(start_ts) still exists before committing!
Stateless Tx Client <-> Raft TSO + Region Raft Shards (Data/Lock/Write)
Client 1 prewrites Primary A and Secondary B (start_ts=10, ttl=5s), then suffers a 10-second GC pause before calling commit_primary(A). Meanwhile, Client 2 cleans up the expired lock on A and rolls it back. What happens when Client 1 wakes up and calls commit_primary(A)?
Practice: Make the trade-off yourself.
In Percolator, which single operation determines whether a distributed multi-shard transaction is committed or aborted?