Rails has powerful sharding features available right within ActiveRecord.
It is possible to define either vertical or horizontal shards
connected_to to switch between them.
When execution happens inside the method,
all database connections follow the configuration specified in the
To configure our shards, we use the
Here’s how ours looks.
Then we initiate the connection in ApplicationRecord.
primary_shard_two are not replicas of each other.
Rather, they are independent databases which share the same schema.
primary_shard_two_replica are replicas.
Originally two parameters were required for a
connected_to block – the name of the shard
and the role.
This was done to make it less syntactically confusing to use.
Let’s look at an example.
However, if we don’t specify the
Rails throws an ArgumentError.
Changes to this PR remove the need for role to be passed to every
This was done to prevent the need for a
role parameter from being passed
when execution was only concerned with the shared.
It also paves the way for more complex multi-tenancy work to be done later on while also simplifying calls for applications
that don’t use roles,
rather only have writer shards.