class ShardSelector

Ruby on Rails 7.1.6

Since v7.0.10

Available in: v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Shard Selector Middleware

The ShardSelector Middleware provides a framework for automatically swapping shards. Rails provides a basic framework to determine which shard to switch to and allows for applications to write custom strategies for swapping if needed.

The ShardSelector takes a set of options (currently only lock is supported) that can be used by the middleware to alter behavior. lock is true by default and will prohibit the request from switching shards once inside the block. If lock is false, then shard swapping will be allowed. For tenant based sharding, lock should always be true to prevent application code from mistakenly switching between tenants.

Options can be set in the config:

config.active_record.shard_selector = { lock: true }

Applications must also provide the code for the resolver as it depends on application specific models. An example resolver would look like this:

config.active_record.shard_resolver = ->(request) {
  subdomain = request.subdomain
  tenant = Tenant.find_by_subdomain!(subdomain)
  tenant.shard
}

Inherits from

Object

Attributes

Methods (defined here)

Private methods

(2) Implementation detail — not part of the public API.

Methods (inherited)

From Object (17)

Type at least 2 characters to search.

↑↓ navigate · open · esc close