Keep Edge and Cloud CouchDB Databases in Sync with Continuous Replication
Use CouchDB continuous replication to sync local and remote databases over HTTP, with checkpoints, conflict handling, and operational limits to watch.
13 Aug 2025, 16:03 UTC

The problem: updates that disappear when the connection drops
You have a CouchDB database on a field device or browser client, and another in the cloud. A one-time replication copies what exists right now, but any write made while the network is down never reaches the other side unless someone remembers to push it again. For offline-first applications, that gap is the problem.
Continuous replication changes that. It opens an HTTP changes feed from the source database and streams document updates to the target as they happen. If the connection breaks, CouchDB resumes from a checkpoint document stored in the target database, so it does not resend every document from the beginning.
Set up a continuous replication job
Replication jobs are submitted to the _replicator database as JSON documents. CouchDB picks them up automatically. This behavior is consistent across recent CouchDB 3.x releases.
Run this from a terminal with network access to both CouchDB instances. You need write access to _replicator on the node that schedules the job, typically admin or replicator privileges.
curl -X PUT "http://admin:password@localhost:5984/_replicator/edge-to-cloud" \
-H "Content-Type: application/json" \
-d '{
"_id": "edge-to-cloud",
"source": "http://admin:password@edge-device:5984/local_db",
"target": "http://admin:[contact removed]:5984/central_db",
"continuous": true
}'
Replace the placeholder URLs, credentials, and database names with your own. The _id field names the job so you can find it later; if you omit it, CouchDB generates one.
Expected check: the response includes "ok":true. Because continuous keeps the feed open, the job does not finish quickly. You can confirm it is active with:
curl -s "http://admin:password@localhost:5984/_active_tasks" | jq '.[] | select(.type=="replication")'
Look for "state":"running" or "state":"completed" with a checkpoint value. A missing task or repeated error fields usually mean credentials, DNS, or CORS settings are blocking the source or target.
Risk: because this job runs continuously, it also replicates document deletions. A deleted document on the source becomes a deleted document on the target. Continuous replication is a synchronization tool, not a backup. To stop the job, delete its document from _replicator; CouchDB halts the feed on the next cycle.
Handle conflicts when both sides write
If the same document ID is updated on both databases before replication catches up, CouchDB stores both revisions as competing leaf revisions. The target keeps one as the visible winner and marks the other as a conflict. Your application must decide how to merge them.
To see all revisions for a document on the target:
curl "http://admin:[contact removed]:5984/central_db/my-doc?open_revs=all"
A common resolution pattern is to read all revisions, merge the fields your application cares about, write the merged document as a new revision, and delete the losing revisions. Until you resolve conflicts, old revisions remain on disk.
Trade-offs and limits
- Unresolved conflicts accumulate as multiple leaf revisions and can slow reads and increase storage until compaction removes old revisions.
- Many replication jobs writing checkpoints to the same target database can create write contention on that node.
- Replication filters run on the source node. Complex JavaScript filters increase CPU usage there and should be load-tested before production use.
- Replication propagates corrupt documents and deletes just as readily as valid writes, so keep separate backups.
If you only need a subset of documents, add a filter or doc_ids field to the replication document to reduce network load.
Verify and close
Write a test document to the source database and confirm it appears on the target. Then inspect _active_tasks to verify the replication job is running and checkpointing. Continuous replication is useful for edge-to-cloud sync, but it works best when you plan for conflicts, monitor active tasks, and keep backups separate from the replication stream.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.