<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: George Maharjan</title>
    <description>The latest articles on DEV Community by George Maharjan (@george_maharjan_8c8aa7f69).</description>
    <link>https://dev.to/george_maharjan_8c8aa7f69</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1931536%2Fdeebcbae-83f0-45d8-9c24-4b5b32137e5f.png</url>
      <title>DEV Community: George Maharjan</title>
      <link>https://dev.to/george_maharjan_8c8aa7f69</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/george_maharjan_8c8aa7f69"/>
    <language>en</language>
    <item>
      <title>Scaling Pains? How Rails Makes Database Sharding Surprisingly Simple</title>
      <dc:creator>George Maharjan</dc:creator>
      <pubDate>Fri, 27 Jun 2025 09:43:52 +0000</pubDate>
      <link>https://dev.to/george_maharjan_8c8aa7f69/scaling-pains-how-rails-makes-database-sharding-surprisingly-simple-410h</link>
      <guid>https://dev.to/george_maharjan_8c8aa7f69/scaling-pains-how-rails-makes-database-sharding-surprisingly-simple-410h</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Database Sharding?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sharding&lt;/strong&gt; 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.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Why Rails Makes Sharding Simple...&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;connects_to&lt;/code&gt;&lt;br&gt;
This is how you define which databses a moder or application connects to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationRecord&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;connects_to&lt;/span&gt; &lt;span class="ss"&gt;shards: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;shard_one: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;writing: :shard_one_primary&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="ss"&gt;shard_two: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;writing: :shard_two_primary&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;connected_to&lt;/code&gt;&lt;br&gt;
This lets you scope a block of code to a specific shard at runtime&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connected_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;shard: :shard_one&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"George"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why Shard Database?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As useful as &lt;strong&gt;Sharding&lt;/strong&gt; 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 &lt;strong&gt;Sharding&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Multi-tenant SaaS apps: Isolate each tenant’s data by database for better performance and security.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;High-scale applications: When your single database starts choking on reads/writes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data locality: Want to keep US data in US servers and EU data in EU? Sharding makes it possible.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion: Sharding does not have to be intimidating&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;And best of all.. &lt;strong&gt;You stay in Rails Minset&lt;/strong&gt; fast, elegant,and &lt;strong&gt;Convention Over Configuration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;em&gt;&lt;strong&gt;Horizontally&lt;/strong&gt;&lt;/em&gt; and keep thing runnnning&lt;/p&gt;

</description>
      <category>rails</category>
      <category>database</category>
    </item>
  </channel>
</rss>
