Rust in TiKV
tl@pingcap.com
About me
● Tang Liu
● Chief Architect of PingCAP
● Worked at Kingsoft and Tencent
● Open sources: LedisDB, go-mysql, etc.
Agenda
● Why do we use Rust in TiKV
● TiKV architecture introduction
● Key technology
● Future plan
What’s TiKV?
A distributed Key-Value database with:
● Geo-Replication
● Horizontal scalability
● Consistent distributed transaction
● Coprocessor for distributed computing
● Working with TiDB like Spanner with F1
We need a language with:
● Fast speed
● Memory safety
● Thread safety
● Binding C efficiency
Why not C++?
● Memory leak, dangling pointer, etc.
● Data race
● No official package manager
● Development cycle will be long...
Why not Go?
● GC
● Data race
● Goroutine runtime expenses
● CGO expenses
So we turned to Rust...
But Rust...
● Steep learning curve
● Long compiling time
● Lack of libraries and tools
● Few programmers know it in
China
Time
Rust
Learning Curve
Why Rust?
● Memory safety
● Thread safety
● No GC
● Calling C is fast
● Official package manager
We made a hard but great decision:
Use Rust!
TiKV Timeline
Start
2016-01-01
Open Source
2016-04-01
Production
2016-10
Beta4
2016-11
RC1
2016-12
GA
2017-11
RC3
2017-04
RC2
2017-02
TiKV Architecture
KV API Coprocessor
Transaction
MVCC
Raft KV
RocksDB
Multi-Raft
Region 1
Region 3
Region 5
Region 4
Region 3
Region 5
Region 2Region 1
Region 3
Region 2
Region 4
Region 1
Region 5
Region 2
Region 4
TiKV node 1 TiKV node 2 TiKV node 3 TiKV node 4
Raft
Group
Region 1
Region 3
Region 1
Region 2
Scale-out (initial state)
Region 1*
Region 2 Region 2
Region 3Region 3
Node A
Node B
Node C
Node D
Region 1
Region 3
Region 1^
Region 2
Region 1*
Region 2 Region 2
Region 3Region 3
Node A
Node B
Node E
1) Transfer leadership of “region 1” from Node A to Node
B
Node C
Node D
Scale-out (add new node)
Region 1
Region 3
Region 1*
Region 2
Region 2 Region 2
Region 3
Region 1
Region 3
Node A
Node B
2) Add Replica on Node E
Node C
Node D
Node E
Region 1
Scale-out (balancing)
Region 1
Region 3
Region 1*
Region 2
Region 2 Region 2
Region 3
Region 1
Region 3
Node A
Node B
3) Remove Replica from Node A
Node C
Node D
Node E
Scale-out (balancing)
Client
TiKV
KV Thread
Raft Thread
RocksDB
Request
Request
Request
Log TiKV Node
Log TiKV Node
Log TiKV Node
Data
Parse the
protocol
Apply the log
A simple
write flow
Executes the
transaction logic
Key technology
● Networking
○ Protocol Buffers (protobuf)
○ mio
○ gRPC
● Asynchronous framework
○ mio + callback
○ tokio-core + futures
○ thread pool + futures - cpupool
Key technology
● Storage
○ rust-rocksdb
● Monitoring tool
○ rust-prometheus
● Profile tool
○ jemallocator with prof feature
● Lint tool
○ clippy
TODO...
● Make TiKV more fast, like removing Box
● Make TiKV more stable, like introducing Rust sanitizer
● Contribute more Rust open source modules, like raft library, open-tracing, etc.
● Participate in other Rust projects more deeply, like rust-gRPC
● Write more articles about Rust on Chinese social media and organize more
Rust meetups
● Be a strong advocate of Rust in China
Thank you!

Rust in TiKV

  • 1.
  • 2.
    About me ● TangLiu ● Chief Architect of PingCAP ● Worked at Kingsoft and Tencent ● Open sources: LedisDB, go-mysql, etc.
  • 3.
    Agenda ● Why dowe use Rust in TiKV ● TiKV architecture introduction ● Key technology ● Future plan
  • 4.
    What’s TiKV? A distributedKey-Value database with: ● Geo-Replication ● Horizontal scalability ● Consistent distributed transaction ● Coprocessor for distributed computing ● Working with TiDB like Spanner with F1
  • 5.
    We need alanguage with: ● Fast speed ● Memory safety ● Thread safety ● Binding C efficiency
  • 6.
    Why not C++? ●Memory leak, dangling pointer, etc. ● Data race ● No official package manager ● Development cycle will be long...
  • 7.
    Why not Go? ●GC ● Data race ● Goroutine runtime expenses ● CGO expenses
  • 8.
    So we turnedto Rust...
  • 9.
    But Rust... ● Steeplearning curve ● Long compiling time ● Lack of libraries and tools ● Few programmers know it in China Time Rust Learning Curve
  • 10.
    Why Rust? ● Memorysafety ● Thread safety ● No GC ● Calling C is fast ● Official package manager
  • 11.
    We made ahard but great decision: Use Rust!
  • 12.
  • 13.
    TiKV Architecture KV APICoprocessor Transaction MVCC Raft KV RocksDB
  • 14.
    Multi-Raft Region 1 Region 3 Region5 Region 4 Region 3 Region 5 Region 2Region 1 Region 3 Region 2 Region 4 Region 1 Region 5 Region 2 Region 4 TiKV node 1 TiKV node 2 TiKV node 3 TiKV node 4 Raft Group
  • 15.
    Region 1 Region 3 Region1 Region 2 Scale-out (initial state) Region 1* Region 2 Region 2 Region 3Region 3 Node A Node B Node C Node D
  • 16.
    Region 1 Region 3 Region1^ Region 2 Region 1* Region 2 Region 2 Region 3Region 3 Node A Node B Node E 1) Transfer leadership of “region 1” from Node A to Node B Node C Node D Scale-out (add new node)
  • 17.
    Region 1 Region 3 Region1* Region 2 Region 2 Region 2 Region 3 Region 1 Region 3 Node A Node B 2) Add Replica on Node E Node C Node D Node E Region 1 Scale-out (balancing)
  • 18.
    Region 1 Region 3 Region1* Region 2 Region 2 Region 2 Region 3 Region 1 Region 3 Node A Node B 3) Remove Replica from Node A Node C Node D Node E Scale-out (balancing)
  • 19.
    Client TiKV KV Thread Raft Thread RocksDB Request Request Request LogTiKV Node Log TiKV Node Log TiKV Node Data Parse the protocol Apply the log A simple write flow Executes the transaction logic
  • 20.
    Key technology ● Networking ○Protocol Buffers (protobuf) ○ mio ○ gRPC ● Asynchronous framework ○ mio + callback ○ tokio-core + futures ○ thread pool + futures - cpupool
  • 21.
    Key technology ● Storage ○rust-rocksdb ● Monitoring tool ○ rust-prometheus ● Profile tool ○ jemallocator with prof feature ● Lint tool ○ clippy
  • 22.
    TODO... ● Make TiKVmore fast, like removing Box ● Make TiKV more stable, like introducing Rust sanitizer ● Contribute more Rust open source modules, like raft library, open-tracing, etc. ● Participate in other Rust projects more deeply, like rust-gRPC ● Write more articles about Rust on Chinese social media and organize more Rust meetups ● Be a strong advocate of Rust in China
  • 23.