CouchDB – New Features in Replication

CouchOne – Blog – What’s new in Apache CouchDB 0.11 — Part Three: New Features in Replication.

Afficher l'image en taille réelle

Without a doubt, replication is CouchDB’s coolest feature. It allows you to synchronise any two databases, local or remote. There is no role-distinction between databases, no master-slave limitations, everybody is a master (if you want master-slave, simply don’t write to one master and thus make it a slave).

This allows you to build a replication infrastructure that fits your application and deployment needs best: two offices with an ocean in between, no problem; large server cluster in one or more data centres, no problem. And anything in between really.

Replication is not new, it has been baked into CouchDB from the beginning. Today, I’ll show you some of the nifty features we added to the 0.11 replicator to make your life a little easier.

To recap, here is how you trigger replication from CouchDB running on your local machine to synchronise a local database with a remote database:

curl -X POST http://127.0.0.1:5984/_replicate 
-d '{"source":"database", 
     "target":"http://example.com:5984/database"}

Note: The backslash means “keep reading the command on the next line” to make things more readable and so you can copy and paste the commands into your command line.

Update 05.12.2010: From CouchDB 1.0 on you need to send the correct content type, just add -H 'Content-Type: application/json' to the command line.

If you want to read up on CouchDB replication, check out the chapters Replication and Conflict Management from our book, CouchDB: The Definitive Guide.

Implicitly Create Target Database

This is a small yet useful tip. Prior to 0.11, if you wanted to replicate, the target database would have to exist. This can be a mild annoyance at times, so we added a replicator option to implicitly create target databases, if they don’t exist.

Using it is easy, here’s the curl command:

curl -X POST http://127.0.0.1:5984/_replicate 
-d '{"source":"database", 
     "target":"http://example.com:5984/database", 
     "create_target":true}'
  • Cool example!

    I was getting errors with the curl command you specified. To fix it, I just had to add: -H “Content-Type: application/json”

  • Yes you’re right, this finally was an article which I found and used to replicate my opscode chef-server database. As real world example I used it to create a filter on the source database:

    {
    "_id": "_design/filters",
    "filters": {
    "roles": "function(doc, req) { if (doc.chef_type == \"role\") { return true; } else { return false; }}"
    }
    }

    and afterwards start the replication on the target database like that:

    curl -X POST http://localhost:5984/_replicate
    -H "Content-Type: application/json"
    -d '{
    "source":"http://src_host:5984/chef",
    "target":"http://localhost:5984/chef",
    "filter":"filters/roles"
    }'

    and clearly the Content-Type is necessary.

    Thanks for your comment.