All Projects → tsukasaoishi → ebisu_connection

tsukasaoishi / ebisu_connection

Licence: MIT license
EbisuConnection allows access to replica servers

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to ebisu connection

Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+20300%)
Mutual labels:  activerecord
attribute-depends-calculator
Automatically calculate a collection of depends attribute of ActiveRecord
Stars: ✭ 41 (+192.86%)
Mutual labels:  activerecord
departure
Percona's pt-online-schema-change runner for ActiveRecord migrations.
Stars: ✭ 86 (+514.29%)
Mutual labels:  activerecord
query-objects-example
Example of rails app using Query Objects
Stars: ✭ 37 (+164.29%)
Mutual labels:  activerecord
yii2-activerecord-inheritance
ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework
Stars: ✭ 18 (+28.57%)
Mutual labels:  activerecord
active snapshot
Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
Stars: ✭ 67 (+378.57%)
Mutual labels:  activerecord
Activerecord Postgres enum
Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
Stars: ✭ 227 (+1521.43%)
Mutual labels:  activerecord
sinatra-dev-cheatsheet
A quick-and-dirty cheat sheet for creating HTML/CSS websites, and developing using Sinatra and ActiveRecord.
Stars: ✭ 44 (+214.29%)
Mutual labels:  activerecord
activerecord-debug errors
An extension of activerecord to display useful debug logs on errors
Stars: ✭ 23 (+64.29%)
Mutual labels:  activerecord
active dynamic
Gem that allows to add attributes to your active models dynamically
Stars: ✭ 61 (+335.71%)
Mutual labels:  activerecord
activerecord-migrations
A gem to simplify activerecord migrations in non-rails projects.
Stars: ✭ 15 (+7.14%)
Mutual labels:  activerecord
embedded
Rails/Activerecord plugin to make value objects embedded into active record objects
Stars: ✭ 48 (+242.86%)
Mutual labels:  activerecord
blade-jdbc
🐜 move to https://github.com/biezhi/anima
Stars: ✭ 36 (+157.14%)
Mutual labels:  activerecord
n1 loader
Loader to solve N+1 issues for good. Highly recommended for GraphQL API.
Stars: ✭ 182 (+1200%)
Mutual labels:  activerecord
activerecord-crate-adapter
Ruby on Rails ActiveRecord adapter for CrateDB
Stars: ✭ 27 (+92.86%)
Mutual labels:  activerecord
Occams Record
The missing high-efficiency query API for ActiveRecord
Stars: ✭ 240 (+1614.29%)
Mutual labels:  activerecord
sinatra-bootstrap
My opinionated Sinatra base application
Stars: ✭ 14 (+0%)
Mutual labels:  activerecord
dice-fairlink
JDBC Driver for read-only connections on AWS RDS Clusters
Stars: ✭ 33 (+135.71%)
Mutual labels:  replica
no querying views
No more querying views in your Rails apps
Stars: ✭ 60 (+328.57%)
Mutual labels:  activerecord
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (+121.43%)
Mutual labels:  activerecord

EbisuConnection

Gem Version Build Status Code Climate

EbisuConnection allows access to replica servers.
You could assign a performance weight to each replica server.

Rails ------------ Master DB
             |
             | 
             +---- Replica1 DB (weight 10)
             |
             |
             +---- Replica2 DB (weight 20)

If you could put a load balancer in front of replica servers, should use FreshConnection.

Usage

Access to Replica

Read query goes to the replica server.

Article.where(:id => 1)

Access to Master

If read query want to access to the master server, use read_master.
In before version 0.3.1, can use readonly(false).

Article.where(:id => 1).read_master

In transaction, All queries go to the master server.

Article.transaction do
  Article.where(:id => 1)
end

Create, Update and Delete queries go to the master server.

article = Article.create(...)
article.title = "FreshConnection"
article.save
article.destory

Support ActiveRecord version

EbisuConnection supports ActiveRecord version 5.0 or later. If you are using Rails 4.2, you can use EbisuConnection version 2.4.2 or before.

Support DB

EbisuConnection supports MySQL and PostgreSQL.

Installation

Add this line to your application's Gemfile:

gem 'ebisu_connection'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ebisu_connection

Config

config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:

production:
  <<: *default
  database: blog_production
  username: master_db_user
  password: <%= ENV['MASTER_DATABASE_PASSWORD'] %>
  host: master_db

  replica:
    username: replica_db_user
    password: <%= ENV['REPLICA_DATABASE_PASSWORD'] %>

replica is the configuration used for connecting read-only queries to the database replica. All other connections will use the database master settings.

Config of each replica server fill out to config/replica.yml

production:
  - "replica1, 10"
  - "replica2, 20"
  -
    host: "replica3"
    weight: 30
"hostname, weight"

String format is it. You can write config with hash.

use multiple replica servers group

If you may want to user multiple replica group, write multiple replica group to config/database.yml.

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:

production:
  <<: *default
  database: blog_production
  username: master_db_user
  password: <%= ENV['MASTER_DATABASE_PASSWORD'] %>
  host: master_db

  replica:
    username: replica_db_user
    password: <%= ENV['REPLICA_DATABASE_PASSWORD'] %>

  admin_replica:
    username: admin_replica_db_user
    password: <%= ENV['ADMIN_REPLICA_DATABASE_PASSWORD'] %>

Config of each replica server fill out to config/replica.yml

production:
  replica:
    - "replica1, 10"
    - "replica2, 20"
    -
      host: "replica3"
      weight: 30
  admin_replica:
    - "replica4, 10"
    - "replica5, 20"

The custom replica stanza can then be applied as an argument to the establish_fresh_connection method in the models that should use it. For example:

class AdminUser < ActiveRecord::Base
  establish_fresh_connection :admin_replica
end

The child (sub) classes of the configured model will inherit the same access as the parent class. Example:

class Parent < ActiveRecord::Base
  establish_fresh_connection :admin_replica
end

class AdminUser < Parent
end

class Benefit < Parent
end

The AdminUser and Benefit models will access the database configured for the admin_replica group.

Master-only Models

It is possible to declare that specific models always use the DB master for all connections, using the master_db_only! method:

class SomethingModel < ActiveRecord::Base
  master_db_only!
end

All queries generated by methods on the CustomerState model will be directed to the DB master.

Using EbisuConnection With Unicorn

before_fork do |server, worker|
  ...
  ActiveRecord::Base.clear_all_replica_connections!
  ...
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Test

I'm glad that you would do test! To run the test suite, you need mysql installed. How to setup your test environment.

./bin/setup

This command run the spec suite for all rails versions supported.

./bin/test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].