How pingcap/tikv works
This project is TiKV – an open‐source, distributed, transactional key–value database built in Rust with a strong emphasis on consistency and scalability via the Raft consensus algorithm. Both the file structure and the README point to a highly modular system with many components dealing with storage engines (e.g., RocksDB, in-memory engines), replica management via Raft groups, a coprocessor framework for distributed computation, and external integrations (e.g., with the Placement Driver, or PD, and even client drivers).
Main Components
• Frontline Client APIs: Interfaces via which TiKV exposes both raw key–value and transactional APIs.
• Distributed Transactional Engine: This includes subcomponents tackling transaction scheduling, concurrency management, snapshot isolation (as seen in the “txn” and “mvcc” directories within the storage subproject).
• Storage Layer: Illustrate how each node (Store) embeds its own storage engine (typically RocksDB) and exposes regions – the basic unit of data distribution. Show that each region is part of a Raft group.
• Raft Consensus Layer: Emphasize the role of Raft for replicating Regions. This is spread out in components such as “raftstore” and “raftstore-v2” that manage log replication, leader election, and cluster configuration.
• Coprocessor Framework: Represent the distributed computing support that allows query execution near the data. This includes both the legacy “coprocessor” layer as well as the “coprocessor_v2” module.
• Placement Driver (PD) Interface: Although PD is a separate project, indicate its importance in cluster metadata, region scheduling, and auto-sharding. The README makes clear that PD and TiKV work in tandem.
• Auxiliary and Supporting Components: Include modules for backup, encryption, resource and error management, performance metrics, logging, and testing frameworks. Many directories in the “components” folder (e.g., backup-stream, cloud, encryption, profiler, etc.) show these roles.
Relationships and Interactions
• Data flow: Clients issue key–value requests or transactional commands that are routed to the appropriate TiKV node via a network/API layer.
– Request routing from the client to the relevant Region replica.
– Consensus communication between Raft peers (including leader selection and log replication).
– Periodic reporting and configuration tasks with PD.
– Internal calls between storage management, transaction processing (including the MVCC layer), and coprocessor execution.
Architectural Patterns and Design Principles
• Decentralized Data Storage & Consensus: Each node is autonomous in storage management but synchronized via Raft.
• Layered design: At the bottom, emphasize physical storage (RocksDB/in-memory engines) and above that, the transaction, Raft, and API layers.
• Integration points: Mark the extension interfaces such as plugin APIs (coprocessor plugin API) and interfaces for cloud functionalities.
Technologies, Frameworks, and Libraries
• Rust as the primary language.
• RocksDB as a key storage engine, along with other engines (in-memory or hybrid).
• Raft as the core consensus mechanism.
• Mention various libraries used for metrics, logging, and error handling (for example, the “clippy” style, test harness, and logging wrappers).
Final Assembly
• Draft an initial sketch that slices the entire system into as many components as possible (as indicated by the extensive “components” directory and multiple module folders).