June 2024
Intermediate to advanced
456 pages
11h 34m
English
Let’s look at the basics of automatic shard switching.
To switch automatically, Active Record uses a ShardSelector class. A shard selector runs as a middleware and maps a HTTP request to a shard.
To set this up, you’ll need a ShardSelector and a Resolver class. Following is an example Resolver class for the shard selector, with a lock parameter value.
The lock parameter indicates whether the shard can be changed within the block where it’s used or not.
The Resolver class processes the request host to identify a tenant and then finds the shard for that tenant.
| | Rails.application.configure do |
| | config.active_record.shard_selector = { lock: true } |
| | |
| | config.active_record ... |
Read now
Unlock full access