ScriptsApr 15, 2026·3 min read

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.

TL;DR
TiKV is a Rust-based distributed key-value store that provides ACID transactions and strong consistency via the Raft consensus protocol.
§01

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.

§02

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.

§03

How to use

  1. Install TiUP (PingCAP's cluster manager):
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
  1. Start a local TiKV cluster for testing:
tiup playground --mode tikv-slim
  1. 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'
§04

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
}
§05

Related on TokRepo

§06

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

What is the relationship between TiKV and TiDB?+

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.

What consistency model does TiKV use?+

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.

What programming languages have TiKV clients?+

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.

How does TiKV handle horizontal scaling?+

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.

Is TiKV suitable for time-series data?+

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)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets