Database sharding: horizontal
Horizontal sharding splits the rows of a table across several databases that share one schema. The course compares shards with replicas and explains the two problems sharding solves: write throughput and data volume. It covers choosing a shard key from access patterns, declaring shards with connects_to, and picking a shard at runtime with connected_to or the shard middleware. It then lists what sharding removes: joins, transactions, global uniqueness and cheap aggregates. It closes with the numbers that keep this application unsharded and a group_by_shard challenge.
What you'll learn
- ✓How a shard differs from a replica and which problems each one solves
- ✓Choosing a shard key that balances even distribution and query alignment
- ✓Declaring shards in Active Record with connects_to shards:
- ✓Selecting a shard per block or per request, including the shard swapping lock
- ✓What sharding removes: joins, transactions, global uniqueness and cheap aggregates
- ✓The alternatives to try before sharding, in order
Splitting rows across databases
Horizontal sharding splits the rows of a table across several databases, each holding a different subset. Every shard has the same schema, and none of them has all the data.
Read the full lesson free →Curriculum
7 lessonsSplitting rows across databases
Compares shards with replicas, names the two problems sharding solves, and shows that Rails treats shards as first-class.
The shard key decides everything
Explains why the shard key comes from access patterns and why user_id fits this application better than course_id.
connects_to shards
Shows how connects_to shards: maps shard names to writing and reading roles, and why it rejects database:.
Choosing a shard at runtime
Covers connected_to with role and shard, the shard middleware with its resolver, and the default shard swapping lock.
What you give up
Describes the four guarantees lost across shards: joins, transactions, unique constraints and aggregates that combine cheaply.
Why this application would not shard
Uses measured numbers to show why this 43 MB application should not shard and what to try first.
Putting it together
Reviews the course in five points and sets a group_by_shard challenge that groups ids into one query per shard.