CouchDB Load Balancing and Replication using HAProxy.
Joe Williams - - January 27, 2009Last 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 2defaults
mode http
clitimeout 150000
srvtimeout 30000
contimeout 4000
balance roundrobin
stats enable
stats uri /haproxy?statsfrontend couchdb_lb
bind localhost:8080acl master_methods method POST DELETE PUT
use_backend master_backend if master_methods
default_backend slave_backendbackend master_backend
server couchdb1 couchdb1:5984 weight 1 maxconn 512 checkbackend 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
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:57This 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:51nice reed!
cheers
nice reed!
cheers!
wholesale fishing tackle
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]
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:09Recently 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:30Procurement 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:04The 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
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
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:19Got some great ideas for making my images more interesting and using flicker. Digg it too, Tricks are helpful. Thanks
penny auction sites
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
Posted by Brazos Lofts for Sale on 26 Jun 2011 at 14:59Topic discuss is really nice and surely i will continue here.seo
Posted by seo on 20 Jul 2011 at 09:54Thank you admin. gOOD SİTES.
Posted by oyun on 09 Nov 2011 at 10:57Add comment
Erlang on Twitter
» dooridho (Ridho Septiansyah): Dtglah boy? Kau ngapo dak pernah les lg? “@doni_erlang: @dooridho : kw td datang dak?”
» dooridho (Ridho Septiansyah): Apo boy? “@doni_erlang: Boy @dooridho”
» xHamidR (Hamid): Frans maakt mij echt boos altijd moet k erlang voor leren
» takabow (takabow♨): RT @bestjobsonline: Senior Erlang Engineer - relo to SF available - http://t.co/BaKJm1J3 #jobs #CyberCodersEngineering #NewYork
» kuenishi (UENISHI Kota): RT @bestjobsonline: Senior Erlang Engineer - relo to SF available - http://t.co/BaKJm1J3 #jobs #CyberCodersEngineering #NewYork
» obin94 (Muhhamad obin): saya dan dewa erlang sedang menuju ke langit untuk bertemu dewa hujan.
» hnakamur2 (Hiroaki Nakamura): Erlang/OTPは“a true dream technology”とのことです。まだ仕事で使ったことはないけど、私も同感だなー。
[erlang-questions] The future of Erlang and BEAM http://t.co/QRR5w029
» setyawanSH (setyawan): Mbok ra koyok cah cilik ndra RT @Harindraa: Dewa erlang, dewi kuan’im, paman pikolo, paman kweceng, bibi lung, mbak yoona, mbak seohyun podo
» Harindraa (Haryndra Nugraha): Dewa erlang, dewi kuan’im, paman pikolo, paman kweceng, bibi lung, mbak yoona, mbak seohyun podo ning endi? Aku butuh sandaran :’(
Statistics
Number of aggregated posts: 10456
Number of comments: 1446
Most recent article: February 06, 2012
Latest comments
» vindisesl on Pretend This Optimization Doesn't Exist: I completely agree with you. I really like this article. It contains a lot of useful information. I can set…
» simple smile on Scale means Skills: Very informative article. Pretty sure people would love to go to that place for shopping. Specially to those who are…
» simplesmile on 27 January 2012: Erlang Solutions embarks on an Erlang Embedded KTP: Your article will make the world better. Thanks again and good luck to you in your life. See you next time.simplesmile