TiKV — Distributed Transactional Key-Value Store on Raft
A CNCF-graduated distributed key-value store written in Rust that powers TiDB. Provides horizontal scaling, strong consistency via Raft, geo-replication, and ACID transactions with Percolator-style MVCC.
What it is
TiKV is a distributed transactional key-value store written in Rust. It is a CNCF-graduated project that powers TiDB (a distributed SQL database). TiKV provides horizontal scaling, strong consistency via the Raft consensus protocol, geo-replication, and ACID transactions with Percolator-style MVCC.
It targets infrastructure teams building distributed systems that need a reliable storage layer with transactional guarantees. TiKV runs as a standalone key-value store or as the storage engine behind TiDB.
How it saves time or tokens
Building a distributed, strongly consistent storage layer from scratch is a multi-year effort. TiKV provides this as a ready-to-deploy component. The Raft consensus protocol handles leader election and data replication automatically, and the Percolator transaction model provides distributed ACID without manual coordination.
TiKV's RawKV API is simple enough for key-value workloads, while the TxnKV API supports multi-key transactions across shards.
How to use
- Install TiUP (PingCAP's cluster manager):
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
- Start a local TiKV cluster for testing:
tiup playground --mode tikv-slim
- Interact with TiKV using the Rust, Go, Java, or Python client:
from tikv_client import RawClient
client = RawClient.connect(['127.0.0.1:2379'])
client.put(b'key1', b'value1')
value = client.get(b'key1')
print(value) # b'value1'
Example
// Go client example with transactions
package main
import (
"context"
"github.com/tikv/client-go/v2/txnkv"
)
func main() {
client, _ := txnkv.NewClient([]string{"127.0.0.1:2379"})
txn, _ := client.Begin()
txn.Set([]byte("account:alice"), []byte("1000"))
txn.Set([]byte("account:bob"), []byte("500"))
err := txn.Commit(context.Background())
// Both writes are atomic -- either both succeed or neither does
}
Related on TokRepo
- AI Tools for Database -- Distributed database tools and storage engines
- AI Tools for DevOps -- Infrastructure tools for distributed systems
Common pitfalls
- TiKV requires PD (Placement Driver) for cluster coordination. Running TiKV without PD is not supported in production.
- Raft consensus requires an odd number of replicas (3, 5, 7) for proper leader election. A 2-node cluster cannot tolerate any node failure.
- The Percolator transaction model adds latency compared to single-node databases. Expect 2-5ms per transaction in a well-configured cluster.
Frequently Asked Questions
TiDB is a distributed SQL database that uses TiKV as its storage engine. TiKV can also run standalone as a pure key-value store without TiDB. They are separate projects maintained by PingCAP.
TiKV provides strong consistency via the Raft consensus protocol. Every write is replicated to a majority of nodes before being acknowledged. Reads can be served from the leader for linearizable consistency or from followers for slightly stale reads.
Official clients are available for Rust, Go, Java, and Python. The Go and Rust clients are the most mature. Community clients exist for other languages.
TiKV automatically splits data into regions (default 96MB each). When a region grows too large, it splits. PD balances regions across TiKV nodes to distribute load evenly.
TiKV can store time-series data but is not optimized for it. Dedicated time-series databases like InfluxDB or TimescaleDB provide better compression and query performance for time-series workloads.
Citations (3)
- TiKV GitHub Repository— TiKV is a CNCF-graduated distributed key-value store
- TiKV Documentation— Uses Raft consensus protocol for strong consistency
- Percolator Paper (Google)— Percolator-style MVCC for distributed ACID transactions
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.