▣datahall
← back to gallery
client-serverC / Erlang / Goest. 2011BSL-1.1 / Apache-2.0 (community)

Couchbase

Memory-first document database: the cache IS the database, persistence is asynchronous.

Couchbase inverts the usual write path: writes land in a managed memory layer first and acknowledge immediately, then queue asynchronously to append-only storage files. Documents are JSON, queries speak N1QL (SQL for JSON), and the cluster distributes documents by vBucket with masterless replication.

Model
JSON documents in vBuckets
Write path
memory-first, async persistence
Query
N1QL — SQL for JSON
Topology
vBuckets, masterless replicas

Memory-first architecture

The managed cache (memcached lineage) is the system of record on write: mutations acknowledge from RAM, then a background writer appends to Couchstore files. Sets are O(1) hot-path.

vBucket distribution

The keyspace splits into 1024 virtual buckets mapped to nodes; each vBucket has one active and N replicas. Rebalancing moves vBuckets, not documents.

N1QL: SQL for JSON

Ad-hoc SQL queries with JOINs, GROUP BY, and indexes over JSON documents — secondary indexes (GSI) maintained separately from the data path.

XDCR cross-datacenter replication

Active-active multi-cluster replication with filtering — the disaster-recovery and geo-distribution story built into the product.

Cache-first is a different contract

Classical engines persist then acknowledge; Couchbase acknowledges from memory and persists in the background. Durability is tunable per operation (replicate-to / persist-to), but the default contract assumes the memory layer plus replication is enough — a design inherited from the memcached lineage.

The consequence: reads of active working sets never touch disk, and write latency is memory-speed. The cost is the persistence queue — crash before persistence loses the queue, which is why the bucket config, replicas, and the disk writer matter.

              UPSERT INTO orders (KEY, VALUE)
VALUES ('order-42', { 'customer': 'ann', 'total': 42 });

SELECT o.total
FROM orders o
WHERE o.customer = 'ann';
            

N1QL: joining the document and SQL worlds

N1QL treats arrays and nested objects as first-class: UNNEST flattens arrays, ANY/EVERY quantify them, and indexes cover nested paths. Secondary indexes (GSI) live on query nodes and are updated asynchronously from the data path.

Diagrams

pipelineWrite path: SDK → vBucket active → memory + replication queue → disk writerclick nodes for description
stage 1executionplaceholderstage 2storageplaceholdermode: write

[caption] Durability knobs are per-operation — the default is memory + replication.

Writes acknowledge from the managed memory layer, replicate to vBucket replicas, and queue to the asynchronous disk writer (Couchstore append-only files).

index-statsvBucket distribution across nodesclick nodes for description

1024 vBuckets mapped onto cluster nodes — active plus replica copies, rebalanced on topology change.

L0
1 nodes
L1
3 nodes
L2
1.0k nodes
Cardinality
1.0M
distinct indexed values
Selectivity
50.0%
fraction of rows matching a predicate

[caption] Illustrative: 3 nodes × 1024 vBuckets (active + replicas). Rebalance moves vBuckets, not keys.

1024 vBuckets mapped onto cluster nodes — active plus replica copies, rebalanced on topology change.