How to shard only specific tables using vitess
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Sharding is an effective strategy to scale databases by distributing data across multiple machines while ensuring that related data remains on the same shard. In larger applications, however, sharding all tables may not be necessary, and sometimes it's more efficient to shard only specific, heavily used tables. This approach helps manage growing data volumes and user loads while minimizing complexity. Vitess, a powerful database clustering system for horizontal scaling of MySQL, supports such selective table sharding. Below we discuss the procedure and technical considerations for implementing sharding of specific tables in Vitess.
Understanding Vitess and Sharding
Vitess extends MySQL by providing a horizontal scaling solution using what it calls "keyspaces". A keyspace in Vitess is a logical database containing one or more tables. Each keyspace can be sharded or unsharded. Sharding involves splitting a keyspace into multiple chunks called “shards”, where each shard holds a subset of the data. Typically, sharding relies on a sharding key, a column used to determine how the data is distributed across the shards.
Key Concepts
- Keyspace: A grouping of one or more related tables that is either sharded or unsharded.
- Shard: A partition of a keyspace.
- VSchema: The vitess schema (
VSchema) defines how tables in a keyspace are related, and the sharding strategy.
Sharding Specific Tables
To shard specific tables in Vitess, you need to define a sharding schema that specifies which tables are sharded and how they are sharded. The steps typically include:
- Designing a Sharding Key: Identify the most suitable column in your table that can act as a sharding key. This key should be such that it distributes queries and data evenly across all shards.
- Configuring the
VSchema: Modify theVSchemato define which tables are sharded and specify the sharding key for each. Tables not defined in theVSchemaas sharded will remain unsharded.Here’s an example of a modifiedVSchemafor a keyspace called 'customer':
- Implementing the Schema Changes: Deploy the changes to your test environment, run integration tests to ensure that the schema has been distributed correctly and that the application logic aligns with the new database structure.
- Data Rebalancing: Once the
VSchemais in place, use Vitess tools for resharding to redistribute existing data across the new shards. This typically involves extensive backend processing and might affect the application's performance temporarily. - Monitoring and Adjustments: After implementing sharding, closely monitor the system and query performance to identify potential bottlenecks or uneven data distribution, adjusting shards and sharding keys as necessary.
Key Considerations
When selecting tables for sharding, consider their size, query load, and growth rate. Highly transactional tables or tables with large amounts of frequently accessed data are prime candidates. It is also essential that all foreign key relationships are maintained within the same shard to avoid cross-shard joins, which are costly in terms of performance.
| Factor | Impact on Sharding Decision |
| Table Size | Larger tables might benefit more from sharding |
| Query Load | High query loads may necessitate sharding |
| Growth Prediction | Fast-growing tables are ideal candidates |
| Data Distribution | Even distribution ensures all shards are utilized efficiently |
Conclusion
Sharding specific tables in Vitess allows for a fine-grained approach to database scaling. By strategically sharding only those tables that most need it, you can enhance performance and manageability without the overhead of a fully sharded database. As with any complex system change, planning, thorough testing, and monitoring are crucial to success.
Related reading
- How to shard using OrientDB
- How to share a single static class code among different applications
- How to share faust table between multiple agents or faust timers?
- How to share rate limiting state between traefik instances?
- How to show a MySQL warning that just happened?
- How to show the last queries executed on MySQL?
- How to solve Timeout FeignClient
- How to solve two generals issue between event store and persistence layer?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.