CouchDB Load Balancing and Replication using HAProxy.

Joe Williams - - January 27, 2009

Last night, I decided to dig into CouchDB a bit more than I have in the past and setup a simple load balanced and replicated setup using HAProxy. In the end it was a pretty easy feat and seems to work fairly well. Here’s what I had to do.

First, I setup three instances of CouchDB on the same machine using different configuration files, PIDs and loopback addresses for each. This can certainly be exchanged for three different machines. Running them on the same machine make sure you adjust the DbRootDir, BindAddress, LogFile in the configuration file and use a command like the following to start things up. This will make sure the non-default configuration and PID location are used.

./couchdb -c SOME_PATH/couchdb2.ini -p SOME_PATH/couchdb2.pid

As you may already know CouchDB has a nice web interface called futon, http://HOSTNAME:5984/_utils/ Using futon I created a database with the same name on all three. I then chose which instance would be my “master”, couchdb1 and couchdb2 and 3 will be “slaves”. I put master and slave in quotes because there isn’t this type of relationship in CouchDB as far as I can tell. All instances can replicate to each other as long as they can connect to each other, so master-slave replication is simply the type of configuration I am enforcing with HAProxy and my replication POST commands. More on these bits later. I then created created a document on my master node and using futon’s replicator replicated the changes to the other nodes. I then wanted to find a way to automate or schedule this. You can initiate replication simply by sending a POST request to couchdb so I wrote a simple curl script to do just that.

First I created the replication POST body in a file:

{”source”:”test_rep”,”target”:”http://couchdb2:5984/test_rep”}

When run against the master this will replicate the master to couchdb2. I wrote a similar file for couchdb3 as well.

Then using curl I can send this body to the master:

curl -X POST –data @couchdb1_2_rep http://couchdb1:5984/_replicate
curl -X POST –data @couchdb1_3_rep http://couchdb1:5984/_replicate

After running you should see some output that starts with {”ok”:true,”session_id …} this means things went well. You should also see some output in the logs on both instances. These commands can be put in a cron to run a specific intervals to keep the slaves updated. You can also create a script and configure DbUpdateNotificationProcess to replicate after each update. The later is probably a nicer solution but a cron and curl should get you started.

I then moved on to setting up HAProxy to load balance between the nodes. Since I wanted a master-slave relationship between the nodes I needed to set HAProxy to only send POSTs, PUTs and DELETEs to the master and GET requests to the two slaves. After checking the docs and playing with a couple different ACL configurations I didn’t find a solution. I then contacted the mailing list for some advice and conveniently a solution was sent back to me quickly. They also told me about another piece of documentation I didn’t find initially. My configuration for HAProxy is pretty basic but it shows what needs to be done.

global
maxconn 4096
nbproc 2

defaults
mode http
clitimeout 150000
srvtimeout 30000
contimeout 4000
balance roundrobin
stats enable
stats uri /haproxy?stats

frontend couchdb_lb
bind localhost:8080

acl master_methods method POST DELETE PUT
use_backend master_backend if master_methods
default_backend slave_backend

backend master_backend
server couchdb1 couchdb1:5984 weight 1 maxconn 512 check

backend slave_backend
server couchdb2 couchdb2:5984 weight 1 maxconn 512 check
server couchdb3 couchdb3:5984 weight 1 maxconn 512 check

The part that enforces where the PUTs, DELETEs and POSTs go is the ACL definition and it basically says that if HAProxy receives a POST, DELETE or PUT then use the master node otherwise use a slave.

Once done I started up HAProxy and tested it out and found that it worked out nicely with GETs going to the slaves in roundrobin fashion and PUTs, DELETEs and POSTs going to the master. I then made a slight change to my curl command from earlier to have the replication POSTs go through HAProxy just to make sure.

curl -X POST –data @couchdb1_2_rep http://localhost:8080/_replicate
curl -X POST –data @couchdb1_3_rep http://localhost:8080/_replicate

If things are working properly you should find that the replication POST commands only go to the master node and the GET commands got to the two slaves.

CouchDB is pretty easy to get going and fun to work with. Hopefully this will help you get going.



Categories: Blogs  Joe Williams  

Comments

anonymous avatar

You have all inspired me to focus on providing more informative and resource type link building posts in the future, so stay tuned for more soon.Ooty

Posted by Ooty on 24 Jul 2010 at 12:57



 
anonymous avatar

This posting is awesome. I have a wealth of knowledge with numerous SQL functions and am clueless when it comes to couch. This should definitely help out!

Posted by Ronald Nike Shoes on 03 Aug 2010 at 12:51



 
anonymous avatar

nice reed!
cheers

Posted by wholesale fishing tackle on 25 Aug 2010 at 20:10



 
anonymous avatar

nice reed!
cheers!
wholesale fishing tackle

Posted by wholesale fishing tackle on 25 Aug 2010 at 20:12



 
anonymous avatar

Very nice Site number one topic Thanks you..
[url=“http://www.designscopia.com/products/outdoor-media/mobile-ads.asp”]mobile ads design
[/url] | [url=“http://www.designscopia.com/products/print-material/stickers.asp”]stickers design
[/url]

Posted by Simon cadbury on 09 Sep 2010 at 07:27



 
anonymous avatar

Traditionally, the infrastructure and rolling stock are owned and operated by the same company. , rodata.str1.1, rodata.str1.1, http://xkykanw.lohuwel.co.cc/rodata.str1.1.html rodata.str1.1,  :-DD,

Posted by Qlnvukxc on 11 Nov 2010 at 01:09



 
anonymous avatar

Recently I came across an interesting article that shared insight into the construction of replication concept can refer to anime Solr.U / for details.

Posted by Alex on 16 Nov 2010 at 12:30



 
anonymous avatar

Procurement generally involves making buying decisions under conditions of scarcity. , [url=http://www.staroperations.com]http://www.staroperations.com[/url], [url=http://www.staroperations.com]http://www.staroperations.com[/url], http://ucrypvx.racykyr.co.cc/www.staroperations.com.html http://www.staroperations.com,  >:P,

Posted by Ijfsnapq on 05 Dec 2010 at 10:04



 
anonymous avatar

The second has to do with the directory and file property if it is running under the CouchDB CouchDB like me (and as the default start script does), the directory containing the database and the view is that be read / write for the power user CouchDB.
http://www.turbochargerpros.com/chevy_turbocharger.html

Posted by eavedrop44 on 19 Jan 2011 at 06:48



 
anonymous avatar

Dresses For Prom Wedding Gowns Bridal Gowns Girls Dress Dresses 2011 Party dresses Cosplay Costumes Ball Gowns womens Dresses black evening dresses red evening dresses blue evening dresses
Party dresses

Posted by cosplay on 21 Jan 2011 at 08:28



 
anonymous avatar

If you get a affiliation banned bulletin haproxy may not be running. If you get a 503 absurdity accomplish abiding solr jetty tomcat is active on the solr nodes. pass4sure 642-873 If you get some html achievement which mentions Solr, pass4sure 350-029 again it should be alive properly. For Drupal’s apachesolr bore to use this configuration, pass4sure 642-456 artlessly set the hostname to localhost and the anchorage to 8080 in the bore agreement page.

Posted by Chung21 on 11 Feb 2011 at 09:19



 
anonymous avatar

nice reed!
cheers!

why cant i get pregnant

Posted by Seth Griffin on 30 Apr 2011 at 23:27



 
anonymous avatar

Got some great ideas for making my images more interesting and using flicker. Digg it too, Tricks are helpful. Thanks
penny auction sites

Posted by penny auction sites on 28 May 2011 at 16:17



 
anonymous avatar

Thanks for sharing such a useful stuff!
law research paper

Posted by Jean on 13 Jun 2011 at 10:27



 
anonymous avatar

It’s amazing, looking at the time and effort you put into your blog and detailed information you provide. I’ll bookmark your blog and visit it weekly for your new posts.

Anam

Brazos Lofts for Sale

Posted by Brazos Lofts for Sale on 26 Jun 2011 at 14:59



 
anonymous avatar

Topic discuss is really nice and surely i will continue here.seo

Posted by seo on 20 Jul 2011 at 09:54



 
anonymous avatar

Thank you admin. gOOD SİTES.

Posted by oyun on 09 Nov 2011 at 10:57



 

 <  1 2


Add comment

Name:

Email:

URL:

Smileys

Remember my personal information

Notify me of follow-up comments?