pingcap/tidb
Architecture
TiDB is a stateless SQL frontend that translates MySQL-protocol requests into transactions and coprocessor requests against a distributed key-value store. This page walks through how a query flows through the server in this repository, names the subsystems involved, and points at the files that own each step.
Cluster topology
graph LR Client[MySQL client / app] -->|MySQL protocol| TiDB[TiDB server] TiDB -->|TSO + meta| PD[PD - Placement Driver] TiDB -->|2PC, coprocessor| TiKV[TiKV - row store] TiDB -->|MPP / coprocessor| TiFlash[TiFlash - column store] TiKV -.->|Raft log| TiKV TiKV -.->|Multi-Raft Learner| TiFlash
TiDB is stateless: any cluster metadata that needs to survive restart lives in TiKV (system tables) or PD (cluster topology, region routing, TSO). Multiple TiDB servers in front of the same TiKV/PD form a horizontally scalable SQL tier; clients can be load-balanced across them.
The TiDB server side of that topology lives entirely in this repository. The TiKV and PD components live in github.com/tikv/tikv and github.com/tikv/pd respectively, and TiDB talks to them via the Go client github.com/tikv/client-go/v2 (pinned in go.mod).
Inside a TiDB server
graph TD
subgraph net[Network]
proto[Server / MySQL protocol<br/>pkg/server/]
end
subgraph sql[SQL layer]
sess[Session<br/>pkg/session/]
parser[Parser<br/>pkg/parser/]
plan[Planner<br/>pkg/planner/]
exec[Executor<br/>pkg/executor/]
expr[Expression<br/>pkg/expression/]
end
subgraph schema[Schema and metadata]
domain[Domain<br/>pkg/domain/]
info[Infoschema<br/>pkg/infoschema/]
ddl[DDL<br/>pkg/ddl/]
meta[Meta<br/>pkg/meta/]
stats[Statistics<br/>pkg/statistics/]
end
subgraph storage[Storage client]
kv[KV interface<br/>pkg/kv/]
distsql[DistSQL<br/>pkg/distsql/]
store[Store / TiKV driver<br/>pkg/store/]
end
proto --> sess
sess --> parser
parser --> plan
plan --> exec
plan -.->|stats lookup| stats
plan -.->|schema lookup| info
exec --> expr
exec --> distsql
distsql --> store
store --> kv
ddl --> meta
ddl --> info
domain --> info
domain --> stats
exec -.->|DDL job| ddlLifecycle of a SELECT
- Accept the connection.
pkg/server/server.go(Server.Run) listens on the configured TCP/Unix socket; each connection gets a*clientConndefined inpkg/server/conn.go. This is also where MySQL-protocol authentication, packet framing, and capabilities negotiation happen. - Establish a session. A connection drives a
session.session(pkg/session/session.go). Sessions carry the transaction context (pkg/session/txn.go), the system-variable view (pkg/sessionctx/variable/), and access to the currentDomain(the per-instance schema and metadata cache frompkg/domain/domain.go). - Parse. The session asks the parser (
pkg/parser/) to turn the SQL text into an AST defined inpkg/parser/ast/. The grammar lives inpkg/parser/parser.yand is generated into the (very large)pkg/parser/parser.go. A separate hint grammar lives inpkg/parser/hintparser.y. - Compile to a plan.
pkg/executor/compiler.goandpkg/planner/optimize.gobuild an initial logical plan (pkg/planner/core/logical_plan_builder.go), apply rewrite rules (pkg/planner/core/rule_*.go), and pick physical operators by cost (pkg/planner/core/find_best_task.goandpkg/planner/core/plan_cost_ver1.go/plan_cost_ver2.go). For prepared statements the plan can be served frompkg/planner/core/plan_cache*.go. A newer Cascades-style optimizer is being developed in parallel inpkg/planner/cascades/. - Execute.
pkg/executor/adapter.goadapts the chosen plan into runtime executors built bypkg/executor/builder.go. Operators (joins, aggregations, projections, sorts, etc.) live next to it (pkg/executor/join/,pkg/executor/aggregate/,pkg/executor/sortexec/, …). Expressions are evaluated bypkg/expression/using vectorized chunks frompkg/util/chunk/. - Read data. Most reads go through
pkg/distsql/andpkg/store/copr/coprocessor.go, which pushes down filters, projections, aggregations, and TopN to TiKV/TiFlash via the coprocessor protocol. Point-gets and direct KV access usepkg/kv/and the TiKV driver inpkg/store/driver/. - Return results. Result chunks flow back up through the executor tree, get encoded into the MySQL result-set format by
pkg/server/, and are written to the connection.
Lifecycle of a write and DDL
- DML. Writes (
INSERT,UPDATE,DELETE) go throughpkg/executor/insert*.go,pkg/executor/update.go,pkg/executor/delete.go. They buffer KV mutations in a transactional memory buffer; when the SQL transaction commits,pkg/session/txn.goruns the two-phase commit via the TiKV client. - DDL.
CREATE TABLE,ALTER TABLE, and similar statements are submitted as DDL jobs bypkg/ddl/job_submitter.go. A scheduler (pkg/ddl/job_scheduler.go) picks up jobs across the cluster, and a worker (pkg/ddl/job_worker.go) drives them through state machines (e.g.,pkg/ddl/column.go,pkg/ddl/index.go,pkg/ddl/partition.go). Online schema change is staged through schema versions (pkg/ddl/schema_version.go) and the infoschema cache in each TiDB instance reloads accordingly. The DDL framework documentation source-of-truth isdocs/agents/ddl/README.md.
Background and cross-cutting
- Domain (
pkg/domain/domain.go) owns long-running per-instance work: schema reloads, statistics handle, plan cache eviction, slow-query top-N, the SQL bind manager, runaway query controller, and the TTL job scheduler. - Statistics (
pkg/statistics/) maintains histograms, count-min sketches, and TopN sketches used by the optimizer;pkg/executor/analyze*.godrivesANALYZE TABLEandpkg/statistics/handle/persists and refreshes them. - DXF (
pkg/dxf/) is the Distributed eXecution Framework used by heavy background tasks like distributedADD INDEX,IMPORT INTO, and statistics gathering. Tasks are partitioned across TiDB nodes and tracked in cluster system tables. - TTL (
pkg/ttl/), bindings (pkg/bindinfo/), timer (pkg/timer/), plugin (pkg/plugin/), resource group (pkg/resourcegroup/,pkg/resourcemanager/), and telemetry (pkg/telemetry/) all hook into Domain. - Server-side HTTP and metrics:
pkg/server/http_status.goexposes/status, Prometheus metrics frompkg/metrics/, slow log ingestion, profiling, and admin endpoints documented indocs/tidb_http_api.md.
Companion tools in this repo
- BR (
br/) — distributed full and incremental backup/restore, CDC log backup/restore. Talks directly to TiKV via gRPC plus a TiDB "glue" for SQL state. - Lightning (
lightning/,pkg/lightning/) — high-throughput data importer with a "physical" mode (writes SST files for TiKV ingestion) and a "logical" mode (tidbbackend that writes via SQL). - Dumpling (
dumpling/) — logical export to SQL/CSV; designed as amysqldump/mydumperreplacement that understands TiDB.
These three tools share infrastructure under br/pkg/ (storage backends, encryption, checksum, region tracking, retry/backoff). Lightning and BR also share the pkg/lightning/backend/ import pipeline.
Where to read next
- Systems overview lists each major package with a one-line description and a link to its dedicated page.
- Applications overview covers
tidb-server, BR, Lightning, and Dumpling as deployable binaries. - The repo's own
docs/agents/architecture-index.mdis the source of truth for path-to-subsystem mapping that contributors and agents follow.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.