TiDB Architecture
TiDB is designed with a distributed architecture that separates compute from storage. Each component can be scaled independently.
Architecture Diagram
Application (MySQL Client)
│
┌────▼────┐
│ TiDB │ SQL Layer (Stateless)
│ Server │
└────┬────┘
│
┌────▼────┐
│ PD │ Metadata & Scheduling
│ Server │
└────┬────┘
│
┌────▼────┐ ┌──────────┐
│ TiKV │◄───►│ TiFlash │
│ Cluster │ │ Cluster │
└─────────┘ └──────────┘
TiDB Server
The TiDB Server is a stateless SQL layer responsible for:
- Accepting connections from applications (MySQL protocol)
- Parsing SQL statements
- Query optimization (cost-based optimizer)
- Executing execution plans
- Sending read/write requests to TiKV or TiFlash
The TiDB Server stores no data. You can add many TiDB Servers to share the connection load. Load balancers such as HAProxy, ProxySQL, or TiProxy are used to distribute connections.
Placement Driver (PD)
PD is the "brain" of the TiDB cluster. Its functions:
- Storing data distribution metadata (Region mapping)
- Providing the Timestamp Oracle (TSO) for transactions
- Scheduling Region migration between nodes
- Providing the TiDB Dashboard for monitoring
- Automatic load balancing based on load and capacity
PD requires at least 3 nodes for high availability. The number of PD nodes must be odd (3, 5, 7) to form a Raft quorum.
TiKV Server
TiKV is a distributed transactional key-value storage engine. TiKV stores data in units called Regions (96 MB per Region by default, growing up to 256 MB). Each Region is replicated to at least 3 nodes using the Raft protocol.
TiFlash Server
TiFlash is a columnar storage engine designed for analytical queries. TiFlash replicates data from TiKV asynchronously using the Multi-Raft Learner protocol.
Query Flow
- The application sends SQL to the TiDB Server via the MySQL protocol
- The TiDB Server parses and optimizes the query
- The TiDB Server sends read/write requests to TiKV/TiFlash
- TiKV/TiFlash processes the data and returns the results
- The TiDB Server returns the results to the application
Scalability
Each component can be scaled independently:
- TiDB Server — add nodes to increase connection capacity and SQL compute
- TiKV — add nodes to increase storage capacity and throughput
- TiFlash — add nodes to increase analytical capacity
- PD — add nodes for redundancy (maximum 7)
In the next chapter, we will study TiKV in more detail.