Apache CouchDB

Apache CouchDB is a document database whose defining feature is replication. Every CouchDB database can synchronize with any other — another server, a cluster in another region, or a browser or phone running the compatible PouchDB library — in either direction, continuously or on demand, surviving long disconnections. Documents are JSON, everything is accessed over a plain HTTP REST API, and every change carries a revision that lets replicas detect when the same document was edited in two places.

That design makes CouchDB a strong fit for offline-first applications — field workers, point-of-sale systems, mobile apps in areas with poor connectivity — and for data that must flow between many sites. The trade-off is that applications must handle conflicts explicitly and model queries around indexes rather than ad hoc joins.

TL;DR

Quick Example

Create, update, and query documents over HTTP:

Offline-first sync from a browser with PouchDB, plus conflict resolution:

Core Concepts

Documents and Revisions

Each document has an _id (chosen by you or generated) and a _rev like 2-a1b2c3...: a generation number and a hash. Updates must include the revision they modify; if someone else changed the document first, CouchDB rejects the write with 409 Conflict, giving optimistic concurrency on a single node. Documents can carry attachments (binary files) alongside JSON.

Querying

Without a matching index, Mango falls back to scanning the whole database — fine in development, disastrous at scale. Design document IDs and indexes around the queries you need; there are no joins.

The Changes Feed

_changes lists every document change since a given sequence, in order, optionally as a long-poll or continuous stream with filters. Replication, search indexing, and integrations with other systems are built on it.

Replication and Conflicts

Replication copies changes from a source database to a target — one-shot or continuous, one direction or both, optionally filtered to a subset of documents. Any number of replicas can accept writes (multi-master). When the same document is edited on two replicas before they sync, both revisions survive: CouchDB picks a deterministic winner so every replica shows the same current version, and lists the others in _conflicts. The application must detect conflicts and merge them — CouchDB never silently discards one.

Clustering

A CouchDB cluster splits each database into shards (q) with replicas (n, typically 3) across nodes, with tunable read and write quorums. Partitioned databases group documents by a partition key for efficient queries within a partition.

Security and Access

When CouchDB Fits

Compare with MongoDB for general-purpose document storage and Cloud Firestore for managed real-time sync. See Local-First for the broader offline-first approach.

Operations

Best Practices

Design for Conflicts From the Start

Decide how each document type merges — last edit per field, append-only events, or user choice — and run conflict resolution in the application or a background worker.

Prefer Small, Focused Documents

Documents edited by many people at once conflict more often. Split them (for example, one document per comment or order line) to reduce conflicts.

Index Every Production Query

Create Mango indexes or views for each query pattern and check _explain output so no request scans the database.

Partition Access by Database

Use per-user or per-team databases when users must only see their own data, and replicate shared reference data into them.

Common Mistakes

Ignoring _rev

Assuming Replication Prevents Conflicts

Shared Databases for Private Data

FAQ

What is CouchDB used for?

Storing JSON documents accessed over HTTP, especially when data must replicate between servers, sites, and offline devices — for example offline-first mobile and web applications with PouchDB.

How does CouchDB handle conflicts?

When the same document is changed on different replicas, CouchDB keeps all versions, chooses a deterministic winning revision, and lists the others in _conflicts. The application reads the conflicting revisions, merges them, and deletes the losing ones.

What is the difference between CouchDB and MongoDB?

Both store JSON-like documents. CouchDB emphasizes an HTTP API, multi-master replication, and offline sync with conflict handling. MongoDB emphasizes rich queries, aggregation pipelines, drivers, and primary-replica replication within a cluster.

What is PouchDB?

A JavaScript database for browsers and Node.js that implements the CouchDB replication protocol, so apps can store data locally and sync with CouchDB when online.

Does CouchDB support queries without indexes?

Mango queries without a suitable index fall back to scanning the database, which is slow for large datasets. Production queries should always be backed by indexes or views.

Related Topics

References