# DB load balancing

**URL:** <https://www.ruby-forum.com/t/db-load-balancing/65543>\
**Category:** Rails\
**Created:** [August 19, 2006, 1:25am UTC](https://www.ruby-forum.com/t/db-load-balancing/65543 "2006-08-19T01:25:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 19, 2006, 1:25am UTC](https://www.ruby-forum.com/t/db-load-balancing/65543/1 "2006-08-19T01:25:47Z")

</div>

Hey all,

Are any of you folks running rails apps on a MySQL Master/Slave  
configurations?

If so, do you do splits of reads/writes, or any type of load balancing  
from rails?

I’ve developed several applications in the past with PHP and other  
languages where I did some software load balancing. For example, my  
previous application would parse the query and figure out if it was a  
read or a write. Then it would send off the query to the appropriate db  
server for it to execute (ie, a select to a MySQL slave, and a write to  
a MySQL Master). It’s pretty basic, but provided pretty decent  
scalability from the DB side of the application.

From what I’ve seen, I don’t think Rails can do this, unless you  
specifically tell the model to connect to the slave when doing Finds,  
and connect to the master when doing the update/delete/creates. But  
this approach forces the developer to remember to connect to the db.

Anyone have any solutions for this? Possibly a 3rd party  
application/load balancer?

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 19, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/db-load-balancing/65543/2 "2006-08-19T15:02:49Z")

</div>

Peter Bui wrote:

> previous application would parse the query and figure out if it was a  
> Anyone have any solutions for this? Possibly a 3rd party  
> application/load balancer?

I would suggest overridding the save method in ActiveRecord or using  
some type of hook that would force writes to the master server.

I also suggest checking out memcached, it’s a caching deamon for caching  
DB queries.  
I recall that someone wrote a memcached backend for ActiveRecord  
caching.

Jason
