Horizontally scaling the Rails backend of Shop app with Vitess

Shop app horizontally scaled a Ruby on Rails app with Vitess. This blog describes Vitess and our detailed approach for introducing Vitess to a Rails app.

Hammad Khalid
31 min readadvanced
--
View Original

Overview

This article discusses the horizontal scaling of the Rails backend for the Shop app using Vitess, a database system abstraction over MySQL. It details the challenges faced due to rapid growth, the strategies implemented for scaling, and the eventual migration to a sharded database architecture.

What You'll Learn

1

How to implement sharding in a MySQL database using Vitess

2

Why query verifiers are essential for migrating to a sharded database

3

When to use Sequences for auto-incrementing primary IDs in a sharded environment

Prerequisites & Requirements

  • Understanding of database sharding concepts
  • Familiarity with Vitess and MySQL

Key Questions Answered

What are the benefits of using Vitess for database scaling?
Vitess allows for sharding and re-sharding MySQL databases, making it easier to manage schema migrations across shards. It also provides connection pooling and is mostly compatible with SQL, which helps in maintaining performance and reliability during scaling.
How did Shopify approach the migration to Vitess?
Shopify migrated to Vitess by first Vitessifying their existing MySQL database, then gradually implementing sharding based on user IDs. They utilized query verifiers to ensure compatibility and prevent issues during the transition.
What challenges did Shopify face during the scaling process?
Shopify encountered issues such as slow schema migrations, database throttling, and the complexity of managing multiple databases. They needed to ensure that queries included sharding keys to avoid cross-shard transactions, which could lead to data inconsistencies.
What types of verifiers were implemented for query validation?
Shopify implemented several types of verifiers, including those for missing sharding keys, cross-database transactions, and cross-shard transactions. These verifiers helped ensure that queries were compatible with the new sharded architecture.

Key Statistics & Figures

Disk size growth
Many Terabytes
As the app grew, the size of the MySQL database increased significantly, leading to performance issues.
Schema migration time
Weeks
Schema migrations on large tables took weeks, prompting the need for a more efficient solution.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Key Actionable Insights

1
Implement query verifiers early in the migration process to catch compatibility issues with Vitess.
By validating queries before they hit production, teams can prevent costly errors and ensure a smoother transition to a sharded database architecture.
2
Plan your sharding strategy in advance to avoid complex data migrations later.
Choosing a sharding key early can simplify the data model and reduce the time spent on backfilling large tables during the migration process.
3
Utilize staging environments to practice critical migration steps before going live.
Thorough testing in a staging environment can help identify potential issues and bugs, ensuring that the production rollout is as seamless as possible.

Common Pitfalls

1
Failing to include sharding keys in queries can lead to performance issues and data inconsistencies.
Without the sharding key, Vitess cannot route queries correctly, resulting in cross-shard queries that can degrade performance.
2
Underestimating the complexity of managing schema migrations in a sharded environment.
Each shard may complete migrations at different times, which can lead to inconsistencies if not managed properly.

Related Concepts

Database Sharding
Query Optimization
Data Migration Strategies