When designing a TiKV-scale transactional store, pair 3s TSO leases with 1-RTT Async Commit and Safe-Point `CompactionFilter` garbage collection.
TL;DR30-second simplified summary▾
Leased TSO eliminates per-timestamp fsyncs, Async Commit halves 2PC critical-path RTTs, and Safe-Point GC prunes obsolete MVCC versions inline during RocksDB compaction.
data/lock/write MVCC columns, Prewrite conflict checks, and lazy lock resolution.R & E · Requirements, Multi-Raft Region Sizing & TSO Math.
100 TB logical data / 96 MB per Region = ~1,041,667 Regions! At RF=3, that is ~3.12M Raft replicas spread across ~100–200 NVMe storage nodes (~15,000–30,000 Regions/node).
Illustrative Sizing: At 500K TPS (each transaction needs start_ts + commit_ts = 1M TSO calls/sec), a single TSO server packing (46-bit physical_ms, 18-bit logical) into a 64-bit integer can issue 2^18 = 262,144 timestamps per millisecond = 262M/sec theoretically, easily serving 1M+/sec via gRPC batching! On latency, standard Percolator takes ~4 sequential steps (~12–16 ms); Async Commit cuts client-visible commit to Prewrite + local min_commit_ts = ~6–8 ms (<10 ms).
Standard 2-RTT Percolator + Per-TS Fsync vs. Batched TSO + Async Commit
- TSO fsync caps at ~5K TPS
- 4 sequential RTTs (~15+ ms)
- 15K/node heartbeat storm
- 1M+ TSO/sec from 3s Raft lease
- 1-RTT Prewrite returns OK (<8 ms)
- Coalesced store-level heartbeats
S · Storage: Multi-Raft Regions & RocksDB Column Families.
Running 15,000 separate RocksDB instances on one server would exhaust file descriptors and MemTable RAM.
Each storage node runs one (or two) shared RocksDB instances partitioned into three Column Families: Write CF, Lock CF, and Data CF (plus a separate Raft-Log RocksDB). Keys are prefixed by region_id or sorted lexicographically so Region splits (96 MB -> 2 x 48 MB) are pure metadata updates in Placement Driver (PD) without moving bytes on disk!
1 Shared KV RocksDB (Write/Lock/Data CF) + 1 Raft WAL RocksDB
H & A · TSO Lease, Placement Driver & 1-RTT Async Commit.
In standard Percolator, the client waits for Prewrite, then fetches commit_ts, then waits for commit_primary().
In Async Commit, each shard tracks max_ts (the highest timestamp any reader has ever used on that shard). During Prewrite, shards return max_ts + 1 as a lower bound, and the client picks commit_ts = max(returned_max_ts) + 1! The Primary lock records use_async_commit = true and the list of all secondary keys, and the client can return OK to the user immediately after Prewrite finishes—committing Primary & Secondaries asynchronously in the background!
TSO(start_ts) -> Parallel Prewrite (All Shards) -> Return OK -> Async Commit
D · Detailed Design: MVCC Safe-Point Garbage Collection.
Every UPDATE and DELETE in Percolator appends a new MVCC version. Without GC, Write CF and Data CF grow forever and point reads slow down scanning tombstones.
A cluster-wide GC Safe-Point is computed by Placement Driver (PD): safe_point = min(now - gc_lifetime, min_active_tx_start_ts, min_backup_ts). Before pruning any key, the GC worker first resolves all locks older than safe_point, then deletes all obsolete versions strictly older than the latest committed version <= safe_point.
Compute min(safe_points) -> Resolve Old Locks -> RocksDB CompactionFilter
- Problem
- How to scale TSO, minimize 2PC latency, and clean MVCC history safely across 1,000+ Raft Regions.
- Naive Solution
- Fsync Raft on every TSO call, run 2-RTT synchronous Percolator, and delete old versions blindly.
- Why It Fails
- TSO fsync bottlenecks at ~5K TPS, 2-RTT 2PC misses <10 ms SLAs, and blind GC breaks active snapshot reads.
- Alternatives
- Per-TS Raft fsync vs. 3s TSO Lease Window; Sync 2PC vs. Async Commit; Blind TTL GC vs. Safe-Point CompactionFilter.
- Engineering Decision
- Use 3s Raft-persisted TSO leases, Async Commit for small multi-shard transactions, and RocksDB
CompactionFilterGC behind PD'ssafe_point. - Trade-Offs
- Async Commit requires checking all secondary keys during rare crash-recovery reads in exchange for 1-RTT commit latency.
A data-export job starts a read-only Snapshot Isolation query at start_ts = 10:00 (taking 25 minutes), while the cluster's default gc_lifetime is 10 minutes. If the GC worker ignores min_active_tx_start_ts and advances safe_point to 10:15, what happens?
E & D · Evaluation, Assumptions, Limitations & Glossary.
TSO failover takes ~2–3s if the PD/TSO Raft leader crashes, because the new TSO leader must wait out or advance past leased_until_ms before issuing timestamps.
Assumptions & Limitations: Async Commit only activates for transactions touching <=256 keys (so the Primary lock doesn't bloat); larger bulk transactions fall back to standard 2-RTT Percolator. Cross-continental deployments (>100 ms RTT to TSO) suffer on get_ts() latency unless TSO is placed near the primary compute tier or replaced by bounded-uncertainty physical clocks (TrueTime/HLC).
SQL/Tx Clients -> PD + Leased TSO -> Multi-Raft Regions (3 CFs) -> CompactionFilter GC
Practice: Make the trade-off yourself.
How does a Raft-backed Timestamp Oracle (TSO) serve 1M+ timestamps/sec without `fsync`ing Raft on every request?