Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Design a Distributed Transactional Key-Value
Store (TiKV / Percolator Scale).

Partition 100 TB into 96 MB Multi-Raft Regions with RocksDB Write/Lock/Data CFs, run a Raft-leased TSO dispensing batched (physical, logical) timestamps, cut 2PC latency with Async Commit, and prune old versions via a cluster Safe-Point GC.
Start the journey5 stages· Last modified Nov 28, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Percolator 2PC & Primary Locks
Know the data/lock/write MVCC columns, Prewrite conflict checks, and lazy lock resolution.
Multi-Raft Range Partitioning
Understand splitting a 100 TB keyspace into 96 MB Raft Regions (each with RF=3) across storage nodes.
Full walkthrough · 5 stages
01
L4 · FUNDAMENTALS

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).

Latency & TSO Scaling

Standard 2-RTT Percolator + Per-TS Fsync vs. Batched TSO + Async Commit

Before · Naive Percolator + Fsync TSO
  • TSO fsync caps at ~5K TPS
  • 4 sequential RTTs (~15+ ms)
  • 15K/node heartbeat storm
After · Leased TSO + Async Commit
  • 1M+ TSO/sec from 3s Raft lease
  • 1-RTT Prewrite returns OK (<8 ms)
  • Coalesced store-level heartbeats
02
L4 · FUNDAMENTALS

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!

Node Storage Layout

1 Shared KV RocksDB (Write/Lock/Data CF) + 1 Raft WAL RocksDB

03
L5 · APPLIED DESIGN

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!

1-RTT Async Commit

TSO(start_ts) -> Parallel Prewrite (All Shards) -> Return OK -> Async Commit

Tx ClientTSO (Batched)Primary RegionSecondary Region1. Batch get start_ts2. Prewrite(async=true,secondaries=[B])3. Prewrite(primary=A) -> max_ts+14. Return OK! Background commit A & B
04
L5 · APPLIED DESIGN

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.

MVCC GC Safe-Point

Compute min(safe_points) -> Resolve Old Locks -> RocksDB CompactionFilter

advance safe_pointall old locks clearedPD ComputeSafe-PointResolve Locks <Safe-PtCompactionFilterPrune
Engineering Decision Framework
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 CompactionFilter GC behind PD's safe_point.
Trade-Offs
Async Commit requires checking all secondary keys during rare crash-recovery reads in exchange for 1-RTT commit latency.
Decision Exercise · Make the Call

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?

05
L6 · SCALE & TRADE-OFFS

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).

TiKV / Percolator Architecture

SQL/Tx Clients -> PD + Leased TSO -> Multi-Raft Regions (3 CFs) -> CompactionFilter GC

Stateless TxClientsingressPD + Leased TSOcontrolMulti-Raft Regions(RF=3)computeRocksDB (W/L/D CFs+ GC)storageTSO batch + RegionroutePrewrite / AsyncCommitRaft apply +Safe-Point GC
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

How does a Raft-backed Timestamp Oracle (TSO) serve 1M+ timestamps/sec without `fsync`ing Raft on every request?

Recommended Learning Flow