跪拜 Guibai
← All articles
Backend

Sharding Is Dying? Why TiDB Pulls Distribution Back Into the Database

By 苏三说技术 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams that have hit MySQL's single-node ceiling can skip the sharding maintenance treadmill entirely: no shard rules to tune, no cross-shard JOIN stitching, no T+1 sync pipelines, because a distributed database handles all of that internally. The catch is a different operational model and missing MySQL features, so adoption hinges on whether your workload depends on stored procedures or full-text search.

Summary

Traditional MySQL sharding layers routing on top of single instances, forcing developers to maintain shard rules, hand-stitch cross-shard queries, and orchestrate migrations. TiDB removes that work by making distribution a database-internal concern: data splits into ~96MB Regions, PD schedules them across TiKV nodes, and Raft guarantees strong consistency with automatic failover.

TiDB also brings HTAP to the same entry point. TiKV handles OLTP row reads and writes while TiFlash, a columnar engine, syncs in real time via Raft Learner, so analytical queries run without a separate Hive or ClickHouse pipeline. The migration path stays short: JDBC and most SQL carry over, the port changes from 3306 to 4000, and cross-database JOINs just work.

The trade-offs are real. Stored procedures, triggers, full-text indexes, and spatial types are unsupported, operational concepts differ (PD, Region, Raft), and resource consumption beats MySQL for small workloads. For teams already feeling sharding's pain, the article argues TiDB's auto-scaling and online DDL outweigh those costs.

Takeaways
— TiDB moves distribution from the application layer to the database layer: you write SQL against a logically complete database and it decides how to split, compute, and merge.
— Data automatically splits into Regions of roughly 96MB each; PD splits hot or large Regions and migrates them to different TiKV nodes without manual sharding rules.
— TiKV uses the Raft protocol with three replicas per Region, requiring a majority acknowledgment for writes, which gives RTO in seconds and zero data loss on node failure.
— TiFlash replicates from TiKV in real time via Raft Learner, letting the optimizer route OLTP queries to TiKV and OLAP queries to TiFlash through one SQL entry point.
— Migration from MySQL mostly means changing the JDBC port from 3306 to 4000; the protocol and most SQL syntax carry over unchanged.
— Use AUTO_RANDOM instead of AUTO_INCREMENT for large write volumes to avoid write hotspots; AUTO_INCREMENT shares an ID allocator with _tidb_rowid and can cause gaps.
— TiDB supports online DDL that doesn't lock tables, so altering hundred-million-row tables doesn't block reads and writes.
— Unsupported MySQL features include stored procedures, triggers, events, user-defined functions, full-text indexes, and spatial data types.
— TiDB is not cost-effective for small data and low concurrency because a minimal production deployment needs multiple nodes.
— Kimi.ai uses TiDB Cloud to create isolated databases in under a second for AI agents; Flipkart handles 1 million queries per second on TiDB on Kubernetes.
— TiDB's slow query log and EXPLAIN output can be fed to an AI to suggest composite or partial indexes, but the final decision on adding an index remains a human trade-off.
Conclusions

The article frames sharding as an application-layer burden, and the real argument is that a distributed database eliminates an entire class of operational toil rather than just moving it elsewhere.

HTAP is the strongest differentiator: running analytical queries fresh on the same data store removes the latency and consistency problems that T+1 pipelines introduce, but at the cost of more infrastructure.

MySQL compatibility is both a selling point and a trap: teams that lean on stored procedures, triggers, or full-text search face a nontrivial migration, and the port change is the easy part.

The AI-assisted slow-query workflow demonstrates that AI's practical value here is in explaining execution plans and suggesting indexes, while the human still weighs write overhead against query gains.

Resource consumption makes TiDB the wrong default for small workloads; the decision hinges on data volume and operational capacity, not on liking distributed systems.

Concepts & terms
Region
TiDB's internal unit of data partitioning: data is divided into chunks by key range, each roughly 96MB by default. Regions split automatically when they grow or become hot, and PD schedules them across TiKV nodes for load balancing.
Raft protocol
A consensus algorithm used by TiKV to keep replicas consistent. A leader replica coordinates writes, and a write is durable only after a majority of replicas (e.g., 2 of 3) acknowledge. It provides automatic leader election and failover.
HTAP
Hybrid Transactional and Analytical Processing: running OLTP (row-store) and OLAP (column-store) workloads on the same system. TiDB uses TiKV for transactions and TiFlash for analytics, with real-time data sync between them.
AUTO_RANDOM
A TiDB-specific auto ID generation strategy that produces random, non-sequential primary key values to avoid write hotspots on a single Region when inserting large volumes of data.
PD (Placement Driver)
The scheduling and management component of a TiDB cluster that handles metadata, load balancing, region distribution, and timestamp allocation.
Raft Learner
A Raft role that asynchronously replicates data from a leader without participating in voting. TiFlash uses Raft Learner to maintain a real-time, strongly consistent copy of TiKV data.
Partial index
An index that only covers a subset of rows matching a filter condition, such as indexing only rows with status = 'PENDING'. It reduces index size and write overhead when a column has low selectivity.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗