A guide to Apache CouchDB: Replication and Offline-First Design

How to work through this course

Seven modules, about sixty-five hours. It is the shortest course in the database programme and the one with the most unusual world view, so a few words on how to approach it.

Use curl -i throughout, not a client library. CouchDB is an HTTP API, and the status codes carry meaning: 409 for a stale revision, 201 against 202 for write quorum, the ETag carrying the current revision. A library that returns a Boolean for "did it work" has thrown away the information you need. Once you understand what the wire looks like, use a library by all means - but learn it at the HTTP level first.

You need two databases from Module 3 onwards. A single CouchDB cannot produce a conflict, which is the central subject of the course. Set up a second server, or install PouchDB in Node, early - in Module 1 if you can - because every interesting thing in this course happens between two replicas.

Do one demonstration properly and the course will change how you think. Create a conflict, then GET the document and look at the completely ordinary-looking response. Then GET it with ?conflicts=true and see the other revision - containing data somebody deliberately entered - sitting there invisible. That five-minute exercise is why this course exists. Most CouchDB production incidents are a variation of nobody having done it.

Treat Module 2's conflict-surface measurement as the main modelling lesson. In MongoDB you split documents for size and query shape. Here you also split them for who edits them, because the document is the unit of conflict. Two devices editing different fields of one document still conflict; the same two edits on two documents never do. Count the conflicts in both models and the design argument makes itself.

Monitor the conflict count from your first deployment. If you take one operational habit from this course, take that one. A view that emits documents with _conflicts, a count, and an alert. Everything else CouchDB can go wrong about is loud. This one is silent.

Compaction, early and in the runbook. Not because it is interesting, but because the failure mode is unrecoverable without adding hardware: a disk too full for compaction to run stays full. Watch sizes.file / sizes.active and act at 2, not at 10.

What to skip on a first pass. Writing a custom JavaScript reduce - learn _count, _sum and _stats and move on. Show and list functions, which are a legacy templating layer. Attachments beyond knowing the trade-off. The Erlang native query server. Clustering internals beyond n, q, r and w. And the _purge endpoint, which permanently removes documents and breaks replication in ways that are hard to reason about - know it exists and leave it alone.

Where to look things up. The official documentation is thorough, and the Replication and Conflicts chapters are the two to read properly. The Definitive Guide (the older O'Reilly book, free online) is dated on specifics and still the best explanation of the design philosophy. And the PouchDB documentation is excellent on the offline-first patterns, which is where most people actually use this.

One last thing, about when to choose this. CouchDB is not a general-purpose database and it is not trying to be. It is slower than its peers, its query layer is weaker, and it hands you a conflict resolution problem that other databases hide. What it does that nothing else does as well is let an unreliable, disconnected, untrusted client be a full database peer and reconcile honestly when it returns. If that is your problem, CouchDB is close to unbeatable. If it is not, you are paying the bill for a feature you will never use - and knowing which situation you are in is the most valuable thing this course can give you.

couchdb database