forked from schoefmann/multi_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
93 lines (71 loc) · 3.16 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
multi_db
========
-- This Plugin was inspired by Rick Olson's "masochism"-Plugin
multi_db uses a connection proxy, which sends read queries to slave databases,
and all write queries to the master database (Read/Write Split).
Within transactions, while executing ActiveRecord Observers and
within "with_master" blocks (see below), even read queries are sent to the
master database.
Install
-------
Just drop the multi_db directory into your application's vendor/plugins dir.
Setup
-----
In your database.yml, add sections for the slaves, e.g.:
production: # that would be the master
adapter: mysql
database: myapp_production
username: root
password:
host: localhost
production_slave_database: # that would be a slave
adapter: mysql
database: myapp_production
username: root
password:
host: 10.0.0.2
production_slave_database2: # another slave
...
production_slave_database_some_server: # yet another one
...
NOTE: multi_db identifies slave databases by looking for "slave_database"
somewhere in the database name!
To enable the proxy globally, add this to your environment.rb, or some file in
config/initializers:
MultiDb::ConnectionProxy.setup!
If you only want to enable it for specific environments, add this to
the corresponding file in config/environments:
config.after_initialize do
MultiDb::ConnectionProxy.setup!
end
Differences to "masochism":
---------------------------
* Support for multiple slave connections (round robin)
* It sends anything except "select ..." queries to the master, instead of
sending only specific things to the master and anything "else" to the slave,
which is a lot more dangerous (e.g. "execute" wasn't sent to the master in
earlier versions of masochism)
* It sends everything coming from AR-Observers to the master, to avoid race
conditions (idea from one of the commenters on a blog entry about masochism)
* It uses its own query cache (with masochism, the slave's cache isn't emptied
when there are changes on the master)
* It supports immediate failover for slave connections
* It will wait some time before trying to query a failed slave database again
* It supports nested "with_master"-blocks (in masochism, nesting such blocks
would unexpectedly switch you to the slave again)
* It schedules a reconnect on the master connection to avoid problems
with virtual, migrating IPs for the master (e.g. multi-master HA setups)
* It's possible to specify slave_database instead of master_database which
makes migration between with and without multi_db less dangerous
* It allows environment specific settings for different slave setups
* It doesn't come with set_to_master! and set_to_slave!, as these are
considered dangerous (and make no sense) in a multi-slave setup. Instead of
set_to_master!, use with_master { code }
Running tests
-------------
If you haven't already, install the rspec gem, then create an empty database
called "multi_db_test" (you might want to tweak the spec/config/database.yml).
From the plugin directory, run:
spec spec
Copyright (c) 2008, Max Schöfmann <max (a) pragmatic-it de>
Released under the MIT license