Resolving CouchDB Revision Conflicts After Replication
CouchDB keeps conflicting document leaves after replication and expects your application to merge them. Here is how to detect and resolve siblings safely.
15 Jul 2025, 03:24 UTC

The missing update after replication
You update a customer record on CouchDB node A. A colleague updates the same record on node B. Replication runs. When you read the document, you see one version, but your colleague's changes are gone.
CouchDB has not lost the data. It has kept both revision leaves, marked one as the winner, and placed the other in the document's _conflicts array. The database did not merge the JSON fields, and it will not tell you which version is business-correct.
Takeaway: CouchDB chooses a deterministic winner, not the right winner. Your application must read every leaf, merge the surviving data, write a new revision, and delete the losing leaves by their exact _rev values.
What a conflict looks like in CouchDB
CouchDB uses multi-version concurrency control (MVCC): every write creates a new revision identified by _rev, and prior leaves remain until explicitly deleted. A _rev is not a timestamp, version number, or stable identifier; it changes on every write and is meaningful only within one database's revision history.
When two nodes update the same document ID independently and replicate, CouchDB preserves both leaf revisions. One leaf becomes the winner. The document's _conflicts array lists the revision IDs of the losing leaves, but it does not include their bodies.
A conflicted document might look like this:
{
"_id": "customer-123",
"_rev": "4-1a2b3c",
"name": "Ada",
"email": "[contact removed]",
"_conflicts": ["4-3d4e5f"]
}
In this example, 4-1a2b3c is the winner and 4-3d4e5f is a sibling you cannot see with a normal GET.
Detect every leaf before choosing a winner
Use open_revs=all to list all leaf revisions and their bodies:
curl -X GET "http://localhost:5984/orders/order-99?open_revs=all"
Where to run: any machine with network access to a CouchDB node. Required permissions: read access to the orders database. Placeholders: replace orders and order-99 with your database and document ID.
Expected check: the response is a JSON array. Each entry contains either the current winning leaf or a conflicting leaf with its _rev and body. If the array contains more than one entry, the document has unresolved siblings.
Risk: the response grows with the revision tree. Avoid polling frequently edited documents in a tight loop, and do not treat _conflicts alone as proof that you have seen all data.
Merge the leaves and delete the losers
Suppose the two leaves for cart-1 contain:
| Revision | Winner body | Conflict body |
|---|---|---|
3-abc | { "items": ["hat"], "note": "gift" } | — |
3-def | — | { "items": ["hat", "socks"] } |
Your application decides the merged document should keep both items and the note:
curl -X PUT "http://localhost:5984/carts/cart-1" -H "Content-Type: application/json" -d '{
"_id": "cart-1",
"items": ["hat", "socks"],
"note": "gift"
}'
Where to run: a client with write access to the carts database. Required permissions: write access. Expected check: CouchDB returns { "ok": true, "id": "cart-1", "rev": "4-..." }. Risk: writing without the correct revision context can create another sibling instead of resolving the existing one.
Then delete each losing leaf by its exact revision:
curl -X DELETE "http://localhost:5984/carts/cart-1?rev=3-def"
This operation changes state. There is no server-side undo for a deleted revision. Before deleting, copy the losing leaf's body to a safe location if you might need it for audit or rollback by hand. Confirm resolution with:
curl -X GET "http://localhost:5984/carts/cart-1?open_revs=all"
A resolved document returns a single leaf and an empty or absent _conflicts array.
Limits and operational habits
- Continuous replication can spread unresolved conflicts to other nodes before your application resolves them locally.
- Unresolved revision trees grow disk usage. Schedule resolution for documents edited by multiple users or services.
- CouchDB has no endpoint that automatically merges all siblings. Business logic must choose which fields survive.
- Deleted revisions cannot be restored. Treat
DELETE ...?rev=as permanent data removal.
Closing check
After replication, pick a sample of recently edited shared documents and run GET ?open_revs=all. If you see multiple leaves, add conflict resolution to your write path before the revision tree grows larger. Resolution is data repair, not a one-time migration.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.