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

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 added this to the 6.1.0 milestone Feb 20, 2020
@eileencodes eileencodes force-pushed the add-sharding-api branch 2 times, most recently from ada5c8e to 0b167e4 Compare February 20, 2020 16:42
@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 add-sharding-api branch 2 times, most recently from 0bc6b8c to 1ebffba Compare February 21, 2020 19:36
@rafaelfranca
Copy link
Member

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>

```ruby
ActiveRecord::Base.connected_to(shard: :default) do
@id = Record.create! # creates a record in shard one
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 22584d0

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

Successfully merging this pull request may close these issues.

None yet

4 participants