Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to `connected_to` and `connects_to` for horizontal sharding #38531

Merged
merged 1 commit into from Feb 24, 2020

Conversation

@seejohnrun
Copy link
Member

@seejohnrun seejohnrun commented Feb 20, 2020

Applications can now connect to multiple shards and switch between
their shards in an application. Note that the shard swapping is
still a manual process as this change does not include an API for
automatic shard swapping.

Usage:

Given the following configuration:

production:
  primary:
    database: my_database
  primary_shard_one:
    database: my_database_shard_one

Connect to multiple shards:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to shards: {
    default: { writing: :primary },
    shard_one: { writing: :primary_shard_one }
  }

Swap between shards in your controller / model code:

  ActiveRecord::Base.connected_to(shard: :shard_one) do
    # Read from shard one
  end

The horizontal sharding API also supports read replicas. See
guides for more details.

This PR also moves some no-doc'd methods into the private namespace as
they were unnecessarily public. We've updated some error messages and
documentation.

cc / @eileencodes @tenderlove @rafaelfranca @matthewd

@eileencodes eileencodes force-pushed the seejohnrun:add-sharding-api branch from ddfc85f to 836b107 Feb 20, 2020
@eileencodes eileencodes added this to the 6.1.0 milestone Feb 20, 2020
@eileencodes eileencodes force-pushed the seejohnrun:add-sharding-api branch 2 times, most recently from ada5c8e to 0b167e4 Feb 20, 2020
@eileencodes eileencodes changed the title Add support for horizontal sharding Add support to `connected_to` and `connects_to` for horizontal sharding Feb 20, 2020
@eileencodes eileencodes self-assigned this Feb 21, 2020
@eileencodes eileencodes force-pushed the seejohnrun:add-sharding-api branch 2 times, most recently from 0bc6b8c to 1ebffba Feb 21, 2020
@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Feb 24, 2020

Great work on this!

Applications can now connect to multiple shards and switch between
their shards in an application. Note that the shard swapping is
still a manual process as this change does not include an API for
automatic shard swapping.

Usage:

Given the following configuration:

```yaml
production:
  primary:
    database: my_database
  primary_shard_one:
    database: my_database_shard_one
```

Connect to multiple shards:

```ruby
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to shards: {
    default: { writing: :primary },
    shard_one: { writing: :primary_shard_one }
  }
```

Swap between shards in your controller / model code:

```ruby
  ActiveRecord::Base.connected_to(shard: :shard_one) do
    # Read from shard one
  end
```

The horizontal sharding API also supports read replicas. See
guides for more details.

This PR also moves some no-doc'd methods into the private namespace as
they were unnecessarily public. We've updated some error messages and
documentation.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
@eileencodes eileencodes force-pushed the seejohnrun:add-sharding-api branch from 92aed8f to 384e7d1 Feb 24, 2020
@eileencodes eileencodes merged commit bbec38b into rails:master Feb 24, 2020
2 checks passed
2 checks passed
build
Details
buildkite/rails Build #67202 passed (17 minutes, 14 seconds)
Details
@eileencodes eileencodes deleted the seejohnrun:add-sharding-api branch Feb 24, 2020
```ruby
ActiveRecord::Base.connected_to(shard: :default) do
@id = Record.create! # creates a record in shard one

This comment has been minimized.

@yogeshjain999

yogeshjain999 Nov 3, 2020

Should the comment mention # creates a record in default shard ?

This comment has been minimized.

@eileencodes

eileencodes Nov 3, 2020
Member

Fixed in 22584d0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.