Databases 16 min read

How Zhihu Scaled to Trillions of Rows Using TiDB: A Real‑World Database Case Study

This article details how Zhihu’s Moneta service tackled massive data growth—over a trillion rows—by migrating from MySQL sharding to the HTAP‑capable TiDB platform, describing architectural changes, performance gains, migration lessons, and future expectations with TiDB 3.0.

21CTO
21CTO
21CTO
How Zhihu Scaled to Trillions of Rows Using TiDB: A Real‑World Database Case Study

Zhihu, China’s largest knowledge‑sharing platform with 220 million users, stores about 1.3 trillion rows of read‑post data in its Moneta service, generating roughly 1 trillion new rows each month and requiring millisecond‑level query latency.

In this article we explore how to maintain sub‑millisecond query response on such massive data, and how TiDB—a MySQL‑compatible NewSQL HTAP database—provides real‑time insights, best practices, and future expectations.

Our Pain Points

The Moneta architecture faces challenges in database scalability, high availability, massive write throughput, long‑term storage of historical data, and strict response‑time limits (≤90 ms) while tolerating occasional false positives.

System Architecture Requirements

High availability: missing read‑post data degrades user experience.

Outstanding performance: support high write throughput and strict latency.

Easy scalability: accommodate rapid business growth.

Exploration

We integrated three key components into the previous architecture:

Proxy : forwards user requests to available nodes, ensuring HA.

Cache : handles requests in memory to reduce database load.

Storage : replaced a standalone MySQL cluster (sharding + MHA) with TiDB, which scales horizontally and avoids the pitfalls of MySQL sharding and MHA.

MySQL Sharding and MHA Drawbacks

Application code becomes complex and hard to maintain.

Changing the shard key is cumbersome.

Upgrading logic impacts availability.

Requires custom scripts or third‑party tools for virtual IP configuration.

MHA monitors only the master, lacks read‑load balancing, and needs password‑less SSH, posing security risks.

What Is TiDB?

TiDB is a NewSQL database with HTAP capabilities, composed of several core components.

TiDB architecture diagram
TiDB architecture diagram

TiDB Server : stateless MySQL‑compatible SQL layer.

TiKV Server : distributed transactional key‑value store using Raft for strong consistency.

TiSpark : Apache Spark plugin for complex OLAP queries.

PD Server : etcd‑backed metadata manager for TiKV.

TiDB also provides tooling such as Ansible deployment scripts, Syncer for MySQL‑to‑TiDB migration, and TiDB Binlog for incremental backup.

TiDB Main Features

Horizontal scalability.

MySQL‑compatible syntax.

Strongly consistent distributed transactions.

Cloud‑native architecture.

HTAP‑enabled ETL.

Fault‑tolerant Raft recovery.

Online schema changes.

How We Use TiDB

We deployed TiDB in Moneta with a three‑layer architecture:

Top layer : stateless, scalable client APIs and proxies.

Middle layer : soft‑state components and layered Redis cache; can recover from TiDB data.

Bottom layer : TiDB cluster storing all stateful data with self‑healing capabilities.

TiDB architecture in Moneta
TiDB architecture in Moneta

TiDB Performance Metrics

After production deployment, we observed significant improvements:

Peak write throughput: 40 k rows/second.

Peak query load: 30 k queries/second across 12 million posts.

99th‑percentile response time ≈ 25 ms; 99.9th‑percentile ≈ 50 ms.

Write rows per second
Write rows per second
Query latency percentiles
Query latency percentiles

What We Learned

Faster Data Import

Using TiDB Data Migration (DM) and TiDB Lightning, we imported 1.1 trillion records in four days—a task that would have taken a month with naïve writes.

Reducing Query Latency

Isolated latency‑sensitive queries to a dedicated TiDB instance.

Added SQL hints for better execution plans.

Leveraged low‑precision TSO timestamps and prepared statements to cut round‑trip time.

Evaluating Resources

TiDB’s Raft replication requires at least three replicas, so we provisioned more hardware than the single‑master MySQL setup, but the horizontal scalability ultimately reduced the total number of nodes needed.

Expectations for TiDB 3.0

Titan Reduces Write Amplification

Enabling Titan in TiDB 3.0 dramatically lowered both write and query latency compared with RocksDB, as shown in our benchmark charts.

Titan vs RocksDB latency
Titan vs RocksDB latency

Table Partition Improves Query Performance

Partitioning tables by time allowed queries to scan only relevant partitions, greatly boosting performance in our anti‑spam service.

TiDB 3.0 Features in Moneta

Batch Raft messages & multithreaded Raftstore : increase write throughput beyond 40 k TPS.

SQL Plan Management : bind queries to optimal plans without modifying SQL text.

TiFlash : column‑store engine enabling efficient analytical queries on massive data without separate ETL pipelines.

TiDB 3.0 in Anti‑Spam Application

With Titan and table partitioning, we reduced node count from seven to five while handling 80 billion new records daily and maintaining low latency for recent‑data queries.

What’s Next

TiDB’s MySQL compatibility and horizontal scalability let us freely expand beyond a trillion records, and we plan to continue contributing to the open‑source ecosystem and collaborating with PingCAP to further strengthen the platform.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data MigrationPerformance OptimizationTiDBHTAPzhihudatabase scalabilityMoneta
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.