Sharding in Rails 7: A New Horizon in Database Management
Tags: database, ruby
Published: 2023-08-18 05:40:00

## Rails 7, released in 2021, has brought forward new horizons in database management with its advanced sharding capabilities. This revolutionary addition empowers developers to scale applications efficiently, offering a seamless approach to handling large datasets and multi-tenant configurations.

Photo of ‘The Shard’ by [Jamie Street]


Since its inception, Rails has been a platform that promotes flexibility, robustness, and ease of use. With the release of Rails 7 in 2021, the framework has taken a significant leap forward in database management with the introduction of innovative sharding features. This article aims to explore these new capabilities, detailing how they empower developers to handle database scaling in a more elegant and powerful way.

I looked at sharding back in 2020 with Rails 6. You can see that video on my [YouTube channel](https://youtu.be/lF7T49sph84) (please subscribe for great Rails content!), but things are different and better now!

Here are the critical points of the new features.

## **Horizontal Sharding: Splitting the Load**

The essence of sharding lies in the distribution of data across different databases to reduce the load on each server. Rails 7’s Horizontal Sharding allows developers to split their database while maintaining the same schema across shards. This method is revolutionary in that it enables applications to scale without the need for fundamental changes to the existing codebase.

With Horizontal Sharding, each shard functions as a self-contained unit with a similar schema, reducing the rows per server and improving overall performance. The transition to using shards is seamless, allowing developers to tackle challenges associated with large datasets.

**Vertical and Horizontal Sharding**: To implement vertical sharding in Rails 7, the `connects_to` method can be utilized. For horizontal sharding, a similar pattern is used, but with the addition of a shard key. Here are some examples:

```
# Vertical Sharding
production:
  primary:
    database: my_primary_database
    adapter: mysql2
  primary_replica:
    database: my_primary_database
    adapter: mysql2
    replica: true

# Horizontal Sharding
production:
  primary_shard_one:
    database: my_primary_shard_one
    adapter: mysql2
  primary_shard_one_replica:
    database: my_primary_shard_one
    adapter: mysql2
    replica: true
```

You can then connect models using the `connects_to` API:

```
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to shards: {
    default: { writing: :primary, reading: :primary_replica },
    shard_one: { writing: :primary_shard_one, reading: :primary_shard_one_replica }
  }
end
```

## **Automatic Shard Switching: Adapting to User Needs**

Automatic Shard Switching is another key feature introduced in Rails 7. Applications can now switch shards per request using the ShardSelector. This enables developers to create custom strategies or lock options for tenant-based sharding. By allowing for real-time changes to shard selection, Rails 7 has made database management more adaptable to specific use-cases.

This feature is particularly valuable for applications handling multiple tenants, where each tenant might have its data stored in a separate shard. Automatic Shard Switching empowers developers to ensure that each request is handled by the appropriate shard, optimizing query performance.

## **Granular Connection Management: Precision at Its Best**

In Rails 7, the ability to manage connections has reached new heights with the introduction of Granular Database Connection Switching. This feature allows developers to switch connections for one database without affecting others, thus achieving unprecedented precision in managing complex database setups.

Granular Connection Management builds on the existing connection switching capabilities, adding more nuanced control and flexibility. This is especially valuable for applications that require multiple connections to different databases, allowing developers to handle specific connections with finesse.

## **Emphasizing Application-Level Control**

While Rails 7 provides robust sharding features, it’s worth noting that it does not automate load balancing of replicas. Instead, it emphasizes application-level control, enabling developers to tailor load balancing to their specific needs.

This approach aligns with the philosophy of Rails that puts control in the hands of developers. It empowers them to manage load balancing based on unique requirements, thus ensuring that the system’s behavior is predictable and aligned with the intended architecture.

## **Conclusion**

Rails 7’s sharding features mark a significant advancement in the framework’s evolution. From Horizontal Sharding to Automatic Shard Switching to Granular Connection Management, these enhancements transform the way developers approach database management and scaling.

The introduction of sharding in Rails 7 opens doors to new possibilities, especially for applications dealing with large volumes of data or multiple tenants. It allows for more efficient distribution of data across servers and grants developers greater control over their connections.

As with any technology, understanding the intricacies and leveraging these features to their full potential requires time and experimentation. Yet, for those looking to harness the latest advancements in database management, Rails 7 stands as a testament to the continuous innovation that defines the Rails community.

It’s more than just a new release; it’s a new horizon in database management, affirming Rails as a modern, forward-thinking platform ready to meet the ever-changing demands of web development.

## Talk to me!

You can find me on [Twitter](https://twitter.com/psmy) where I share insights on Ruby on Rails, discuss my journey with [Zonmaster](https://public.zonmaster.com/), and explore various aspects of life. You can also check out my [YouTube](https://www.youtube.com/@philsmy) channel where I cover various topics related to web development, including Ruby on Rails.

And guess what? I’ve recently released my first guide, “[Getting Started with Ruby on Rails: A Step-by-Step Guide for Beginners](https://philsmy.gumroad.com/l/yfhoq)” on my [Gumroad](https://philsmy.gumroad.com/) store 📚🚀 It’s a ‘Fair Price’ ebook, so you can get it for free, but any payment is greatly appreciated as it helps support my work and future guides. Don’t miss out on this opportunity to level up your web development skills with Rails!

Drop me a note on Twitter or [LinkedIn](https://linkedin.com/in/philsmy) if you have any questions or need help with your Rails project. Happy coding! 😊🎉