Databases 16 min read

Sharding and Database Refactoring for High‑Volume Train Ticket Orders at Ctrip

This article describes how Ctrip's senior backend engineer designed and implemented horizontal database sharding, a service‑level proxy, dual‑read/write mechanisms, and a staged migration process to overcome order‑database bottlenecks, improve scalability, and ensure high availability for the rapidly growing international train‑ticket business.

Ctrip Technology
Ctrip Technology
Ctrip Technology
Sharding and Database Refactoring for High‑Volume Train Ticket Orders at Ctrip

With the rapid growth of Ctrip's international train‑ticket business, order volume surged and the single‑database architecture began to show performance bottlenecks, prompting a complete redesign of the order database.

The team identified two main categories of challenges: generic sharding issues (how to split, shard key selection, routing, middleware choice, cross‑database transactions, data expansion) and business‑specific problems (direct application‑to‑database connections, smooth migration, historical data migration).

After evaluating vertical and horizontal splitting, the team chose horizontal database and table sharding because vertical splitting could not meet the required scalability. The shard key was selected based on business characteristics, using the order ID for the order system to avoid hotspot concentration.

Three routing strategies were examined: mapping tables, group routing, and hash routing. Mapping tables offered flexibility but introduced massive mapping data; group routing was simple but prone to hotspot issues; hash routing provided uniform distribution but made expansion difficult. The final solution combined group routing with hash routing (group‑hash routing) to gain easy expansion and balanced data distribution.

For middleware, several options were compared, and Sharding‑Sphere was selected due to its Java ecosystem compatibility, lightweight nature, customizability, active community, and performance that matched the team's requirements.

A dedicated service proxy named Dal‑Sharding was built to encapsulate all sharding logic, providing a unified API for all order‑related services and shielding them from sharding complexity.

To ensure a smooth transition, the team implemented dual‑read and dual‑write mechanisms. Dual‑read first attempts to read from the new database and falls back to the old one if necessary, while dual‑write writes to the new database and asynchronously replicates to the old one using a combination of aspect‑oriented programming, order‑event triggers, and scheduled tasks.

The migration was performed in three stages: first migrating low‑traffic new suppliers, then stable low‑volume online services, and finally the high‑traffic core services, each step verified by the dual‑read/write safeguards.

Data migration was carried out at the order level rather than table‑by‑table, extracting complete order information from the old database, transforming it to the new schema, and writing it to the new tables in a scheduled batch process.

After the refactor, the order database has been running stably in production, delivering benefits such as centralized service access, unified API management, flexible customization, transparent sharding routing, and comprehensive table‑level monitoring.

The article concludes with a FAQ covering typical sharding questions, including cross‑database operations, pagination, complex statistics, hotspot mitigation, and the rationale behind using multiple dual‑write techniques.

BackendMigrationDatabaseshardingHigh Availabilitymiddlewareorder system
Ctrip Technology
Written by

Ctrip Technology

Official Ctrip Technology account, sharing and discussing growth.

0 followers
Reader feedback

How this landed with the community

login 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.