Scaling Pains? How Rails Makes Database Sharding Surprisingly Simple



This content originally appeared on DEV Community and was authored by George Maharjan

What is Database Sharding?

Think of your database like a giant filling cabinet. As your app grows, that cabinet gets stuffed with millions of records, making it harder and slower to find what you need.

Sharding is like splitting that giant cabinet into smaller ones. Instead of storing everything in one place, you separate your data-often by customer, region, or ID range into different databases(also known as shards). Each shard handles a smaller load of the total load which speeds things up.

Imagine a library with one checkout counter. As more people come in, the line gets longer. Now imagine 10 smaller checkout counters, each serving a specific floor. That’s sharding in action.

Why Rails Makes Sharding Simple…

connects_to
This is how you define which databses a moder or application connects to.

class ApplicationRecord < ActiveRecord::Base
  connects_to shards: {
    shard_one: { writing: :shard_one_primary },
    shard_two: { writing: :shard_two_primary }
  }
end

connected_to
This lets you scope a block of code to a specific shard at runtime

ActiveRecord::Base.connected_to(shard: :shard_one) do
  User.create(name: "George")
end

Why Shard Database?

As useful as Sharding may seem, you do not need to shard every app. But for some cases it is a life saver. The rule of thumb is-If your database has millions of rows and you notice performance bottlenecks, it’s time to consider Sharding

  • Multi-tenant SaaS apps: Isolate each tenant’s data by database for better performance and security.

  • High-scale applications: When your single database starts choking on reads/writes.

  • Data locality: Want to keep US data in US servers and EU data in EU? Sharding makes it possible.

Conclusion: Sharding does not have to be intimidating

Rails’ native support for sharding is a game changer as it works without the need for adding any extra gems or Architecture change. Instead we get, clear syntax, builtin support for replicas, and flexibility for multi-tenant or global apps.

And best of all.. You stay in Rails Minset fast, elegant,and Convention Over Configuration

So if the app is suffering under the weight of growth, know this-It is not the end. No need to jump the ship. With a bit of setup and smart shard design, you can scale Horizontally and keep thing runnnning


This content originally appeared on DEV Community and was authored by George Maharjan