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
Hello webmaster
I would like to share with you a link to your site
write me here .(JavaScript must be enabled to view this email address)
er running you should see some output that starts with {”ok”:true,”sessioVidivodon_id …} this means things went well. Youklip should also see some output in the logs on boVidivodoth 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 an
Posted by Vidivodo on 30 Aug 2009 at 15:47Wow this looks like it took a great deal of time to come up with. Thanks for the info.
Posted by Josh on 04 Sep 2009 at 16:02thanks for that post, love this site
gelinlik
oyunlar
lida fx15 biber hapı ikibindokuz seo yarışması
Very nice article. href=“http://www.babytoddlerdresses.com”>Baby Toddler Dresses</a>
Posted by Baby on 02 Oct 2009 at 06:31Some great material here which all helps the user to establish the requisite requirements for implimentation so that you Garage Floor Plans
Posted by Garage Floor Plans on 25 Oct 2009 at 17:58It’s a good feature to manage a lot of things from you. Such software will really help you out with all the troubles that you get in doing things manually.
Dina
Tell A Friend
Its a nice post regarding law and its values.I think its necessary to each and individual to follow the law and order.
promotional caps
Jumping Castle
It is really a nice post, its always great reading such posts, this post is good in regards of both knowledge as well as information. Thanks for the post.
Personal massager
Wow, I wished I had found your blog a few years ago. This is a battle I have been having for most of my life….I’m going to go grab a cup of tea and continue reading some more….
US auto insurance directory
It is really helpful blog.I just couldn’t leave your website before saying that I really enjoyed the quality information you offer to your visitors.It is useful for a person he don’t know anything about Muslim religion.Keep posting.
stubby holders
Thank you so much for this new feature. This is a great resource for us and the reviewers have done a great job of reviewing the product and writing their critiques!
drink coasters
You’re really thankful for this post, I’ve been really enjoying checking up your posts from time to time. Looking forward to see your future posts !!
placemat
post, I’ve been really enjoying checking up your posts from time to time. Looking forward to see your future posts !! mobile ads design
Posted by crespo on 24 Mar 2010 at 11:20I’ve been really enjoying checking up your posts from time to time. thank you
Posted by mp3 dinle on 25 Apr 2010 at 15:21All 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.
Posted by Mijin on 05 May 2010 at 00:52I’m well versed in SQL but not couch, didn’t even know what it was I’m checking it out thanks to this blog.
Posted by Jade on 23 Jun 2010 at 03:59Add comment
Erlang on Twitter
» Fedora_Updates (====================): Fedora 17 Update: erlang-misultin-0.9-1.fc17 http://t.co/u3ABUDkz
» HornedKavu (Max Riveiro): RT @shwars: Вчера в докладе career.ru на #itedu были озвучены новые востреб.профессии на IT-рынке: Haskell-, Lisp- и Erlang-программист!
» phyrexianengine (Vasily K.): RT @shwars: Вчера в докладе career.ru на #itedu были озвучены новые востреб.профессии на IT-рынке: Haskell-, Lisp- и Erlang-программист!
» Erlang_ABNIC (Erlangga .A): @cjerikho829 selamat shooting ♈o kak.. Nitip salam buat kak @Bellaudya829.. ☺ Semangat (งˆヮˆ)ง
» dessyrosalia (♡pesek mancung♥ ): Erlang ke rumahku donk kangen nih
» si_erlang (Erlangga Adhitya): 75% dalane jahanam
» GeekDani (Dani Kim): @charsyam 그렇군용. :-) 여긴 서늘한데. 크크. Erlang Meetup 준비는 잘 하시나요. ㅋㅋ
» syahlafatimahA (LalaTik(ʃ⌣ƪ) ): Waaa?! Si erlang suka cherrybelle(?) wkwkwkwk ngakak aih xD
» yosukehara (Yosuke Hara): I’ll be a simple test for benchmarking JSX and Jiffy together. #erlang
» Debbyvheumen (Debby van Heumen): @elisaaa15 @kleingeld_ haha okee succes :) blijven jullie erlang
Statistics
Number of aggregated posts: 10498
Number of comments: 2115
Most recent article: May 15, 2012
Latest comments
» cheap soccer jerseys on Memory Models in Erlang vs Java: Nice discussion here,you are doing a great job. i was looking for this information. i found it on your page…
» mandesejohn on Couchbase Meetup at new HQ: Thanks for sharing experience. It should be really a great post. It should be knowledgeable and informative. Keep it up. flower delivery columbus ohio
» vermaseo on Scale means Skills: I’m surprised people are still commenting about this. George has been moved on to bigger and better things with the president for awhile now.ledikanten