Couchbase data files: the bucket directory holds 1,024 vBucket files, master.couch.1 and a cm directory
| |

Inside database data files (7/3+4): Couchbase

How Couchbase data files work on disk: one file per vBucket, 4 KB blocks, append-only updates, tombstones and compaction. Measured on Rocky Linux 10.

Couchbase hides its storage behind a key-value API: you ask for a key and you get a JSON document. On disk, however, the story looks like the other engines of this series: files, blocks, and records that never change in place. This article opens the Couchbase data files on a lab node, with real commands and real byte counts. It is the fourth extension of the series “Inside database data files”, which already covered InnoDB, PostgreSQL and Oracle.

Tested on Rocky Linux 10.2, Couchbase Server 8.0.2 Community Edition (build 5503), WSL2. I installed it from Couchbase’s yum repository (couchbase-server-community) on a disposable lab machine, and I accepted the licence terms for that test only. The documentation lists Rocky Linux 10.x as a supported platform. The node ran the Data service alone, with one 128 MB bucket stored by Couchstore.

The Couchbase data files on disk

Couchbase data files: the bucket directory holds 1,024 vBucket files, master.couch.1 and a cm directory
The data directory of a Couchstore bucket on the lab node: one file per vBucket.

On Linux, buckets live under /opt/couchbase/var/lib/couchbase/data/. Each bucket gets a directory named after its UUID, not after its name: the REST API returns the same value in the uuid field. Inside, there are 1,024 files named N.couch.1, one per vBucket, plus master.couch.1 (34 bytes) and a small cm directory that holds bucket.metadata (59 bytes).

An empty bucket is not free. Each vBucket file already weighs 8,297 bytes, so the directory took 8,512,605 bytes while the bucket held three documents. The .1 suffix is a revision number. Keep it in mind, because it changes during compaction.

Two limits apply to the Couchbase data files. First, the documentation says Couchstore is the only storage engine in the Community Edition. When I tried to create a Magma bucket, the server answered Magma is supported in enterprise edition only. Second, the documentation gives 1,024 vBuckets for Couchstore on Linux and Windows, and 64 on macOS. The bucket information of the lab node showed 1,024.

From a key to a vBucket

How a Couchbase key such as user::alice reaches its vBucket file through CRC32 and a modulo 1024
Four keys, their CRC32 and their vBucket, checked against the cbc hash command.

A client never asks a node for a file. It hashes the key, and the documentation says Couchbase uses CRC32 for that. The exact arithmetic is not spelled out in the pages I read, so I tested one formula against the cbc hash command: take the CRC32, shift it right by 16 bits, keep 15 bits, and divide by 1,024 to keep the remainder. It matched all four keys I tried.

$ cbc hash user::alice user::bob order::1001 -U couchbase://127.0.0.1/lab_cs -u Administrator -P <password>
user::alice: [vBucket=339, Index=0] Server: 127.0.0.1:11210, CouchAPI: http://127.0.0.1:8092/lab_cs
user::bob: [vBucket=779, Index=0] Server: 127.0.0.1:11210, CouchAPI: http://127.0.0.1:8092/lab_cs
order::1001: [vBucket=112, Index=0] Server: 127.0.0.1:11210, CouchAPI: http://127.0.0.1:8092/lab_cs

As a result, the file that holds user::alice is 339.couch.1. When I created the first three documents, only three vBucket files grew: 112, 339 and 779. Each went from 8,297 to 12,393 bytes, which is exactly one more block of 4,096 bytes.

One file per vBucket: blocks and headers

Couchbase data files are cut into 4 KB blocks; the block map of 339.couch.1 and its header offset
The first byte of each 4,096-byte block in 339.couch.1 before compaction, and the same file after.

The tool couch_dbinfo ships with the server and reads these files. It reports a header at file offset 12,288, which is three blocks of 4,096 bytes. In other words, the header starts at a block boundary.

$ couch_dbinfo /opt/couchbase/var/lib/couchbase/data/f96a4814…/339.couch.1
DB Info (…/339.couch.1) - total disk size: 12.1 kB
   crc: CRC-32C
   Encryption key: unencrypted

Header at file offset 12288
   file format version: 14
   update_seq: 4
   purge_seq: 0
   doc count: 4
   deleted doc count: 0
   data size: 1.2 kB
   B-tree size:       1.1 kB
   └── by-id tree:    182 bytes
   └── by-seqno tree: 188 bytes
   └── local size:    718 bytes

The file holds four documents: user::alice and three internal system events that record the creation of the default scope and collections. It also holds two B-trees, one indexed by document ID and one by sequence number. Each commit writes its data first, then a new header at the next block boundary. Older headers stay in the file.

I looked at the first byte of every 4,096-byte block. In 339.couch.1 before compaction (45,161 bytes, 12 blocks), the first bytes were 1 1 1 1 1 0 1 0 1 1 1 1. After compaction, the three blocks of 339.couch.2 started with 0 0 1. My reading is that 1 marks a header block and 0 a data block. It matches the offsets couch_dbinfo prints, for example 45,056 before and 8,192 after, but I did not read the format specification to confirm it.

A document record

The companion tool couch_dbdump prints what the file holds for one key. First, the key carries the collection it belongs to: (collection:0x0:default) is the default collection. Then come the sequence number of the change in this vBucket, the revision, the CAS value, the expiry, the flags and the data type. Finally the body appears, which the tool labels as Snappy-compressed.

$ couch_dbdump --key user::alice 339.couch.1
  Doc ID: (collection:0x0:default)user::alice
     seq: 5
     rev: 2
     content_meta: 0x80
     size (on disk): 47
     cas: 1793652077192478720, expiry: 0, flags: 0, datatype: 0x01 (json)
     size: 37
     data: (snappy) {"name":"alice","country":"FR","n":1}

This record is 37 bytes of JSON and 47 bytes on disk. The CAS value is a 64-bit number that changes with each mutation. It plays the role that row locks and version columns play elsewhere.

An update changes nothing in place

Each update of one Couchbase document adds 4,096 bytes to the vBucket file and moves the header
One document updated three times: the file grows by one block each time.

To see how an update lands, I changed one document three times and measured the file after each change. Every update added 4,096 bytes and moved the header forward by one block. The live data size stayed at 4.8 kB. Yet the Couchbase data files kept all four versions of the document, readable with strings, while couch_dbdump showed only the latest one.

# file 339.couch.2, after a compaction: 8297 bytes, header at 8192
$ cbc create user::alice -M upsert -V '{"name":"alice","country":"FR","n":2}' ...
after update n=2: 12393 bytes   Header at file offset 12288   update_seq: 82
after update n=3: 16489 bytes   Header at file offset 16384   update_seq: 83
after update n=4: 20585 bytes   Header at file offset 20480   update_seq: 84

$ couch_dbdump --key user::alice 339.couch.2     # one version is reachable
     seq: 84
     rev: 5
     data: (snappy) {"name":"alice","country":"FR","n":4}

$ strings -n 8 339.couch.2 | grep '"n":'          # four copies are on disk
{"name":"alice","country":"FR","n":1}
{"name":"alice","country":"FR","n":2}
{"name":"alice","country":"FR","n":3}
{"name":"alice","country":"FR","n":4}

So Couchbase works like PostgreSQL in one respect: a new version goes elsewhere. The difference is the unit. PostgreSQL writes a new tuple in a page. Couchstore appends a new record and a new header at the end of the file, and a later compaction removes what is no longer reachable.

Deletes, tombstones and a directory that only grows

Size of the Couchbase data files directory after load, update, delete and compaction
du -sb of the bucket directory at each step of the lab.

For a bigger test, I loaded 20,000 small documents with cbimport, rewrote the same 20,000 keys, and deleted 10,000 of them. I turned the fragmentation threshold of auto-compaction up to 90 % for the test, and I put it back to 30 % afterwards. The directory kept 1,025 files all along, but its size did not follow the amount of live data.

Loading 20,000 documents took the directory from 8.5 MB to 14.9 MB. Rewriting the same keys, with the same amount of live data, raised it to 21.3 MB. Deleting 10,000 documents raised it again, to 29.7 MB, because a delete is an append too. In vBucket 339, the file went from 24.1 kB to 32.1 kB to 44.1 kB, while its data size stayed near 5 kB.

A deleted document leaves a tombstone. It has a delete time, no body, and it keeps its sequence number so that other nodes and tools can learn about the delete.

$ couch_dbdump --key doc::5 688.couch.1
  Doc ID: (collection:0x0:default)doc::5
     seq: 22
     rev: 3
     content_meta: 0x3
     size (on disk): 0
     cas: 1793652115159711744, delete_time: 1793652114, flags: 0, datatype: 0x00 (raw)
     doc deleted (explicit)
     could not read document body: document not found

The cluster setting purgeInterval was 3, which I read as days. I did not wait for it to pass. Right after compaction, the tombstone was still in the new file, as expected. Note that the REST statistics lag behind the disk: right after the load, diskUsed still showed 8.5 MB while du showed 14.9 MB. I therefore used du -sb for all the figures.

Compaction rewrites the Couchbase data files

The default threshold in the lab was 30 % database fragmentation. On top of this, the documentation says that data files keep growing even when data is deleted, and that compaction rewrites them. In the lab, I started a compaction of the Couchbase data files by hand. It took about nine seconds for 1,025 files, and the directory fell from 29.7 MB to 6.4 MB.

$ couchbase-cli bucket-compact -c 127.0.0.1:8091 -u Administrator -p <password> --bucket lab_cs
SUCCESS: Bucket compaction started

before: 29,713,501 bytes, 1025 files named *.couch.1
after:   6,403,165 bytes, 1025 files named *.couch.2, none named *.couch.1
339.couch.2 : 8297 bytes, header at 8192, doc count 21, deleted doc count 14

Every file was rewritten under a new name, .couch.2, and the old ones disappeared. The compacted files are smaller than the empty ones: 688.couch.2 weighs 4,201 bytes against 8,297 for an empty file. The server also ships couch_compact, couch_check and couch_dbck. I did not use them here.

What about Magma?

I could not test Magma, because the Community Edition refuses it. The documentation gives the main facts. It recommends Magma for most use cases since 8.0, and it targets datasets that do not fit in memory. A Magma bucket can have 128 or 1,024 vBuckets, chosen at creation, and the minimum quota per node is 100 MiB with 128 vBuckets and 1 GiB with 1,024. The cluster settings of the lab node also showed a separate Magma fragmentation threshold of 50 %. I did not open a Magma file, so this article says nothing about its layout.

The write path in memory

The documentation explains that a new item is saved both in memory and on disk. Couchbase puts a compressed copy on a disk queue, and another on a replication queue. Rarely used items can leave memory later, without blocking requests. In the lab, the bucket used the value-only eviction policy. cbstats showed the flusher running with nothing left to do (ep_flusher_todo: 0), and a memory quota of 134,217,728 bytes, which is my 128 MB. It also lists many ep_dcp_* settings for the streaming protocol DCP, which I did not explore.

Couchbase next to the other engines

InnoDBPostgreSQLOracleCouchbase (Couchstore)
Unit on disk16 KB page8 KB page8 KB (2 to 32 KB) block4 KB blocks, one file per vBucket
Table organisationprimary key B+treeheapheap by default, IOT optionaltwo B-trees per file: by ID and by sequence number
Row addressprimary keyctid (page, slot)ROWID (object, file, block, row)key, CRC32, vBucket, then the by-ID tree
Old versionsundo login the tableundo tablespacein the same file until compaction
Cleanuppurge threadVACUUMundo reusecompaction, default trigger 30 % fragmentation
Row locksin memory (lock system)xmax in the tuplelock byte, then ITL in the blocknone seen in the file; a CAS value in the metadata

Couchbase data files are the only ones here that give each shard its own file. That choice makes compaction a per-vBucket job, and it explains why a bucket directory can hold 1,025 files for a handful of documents. As with PostgreSQL, a delete or an update never frees space by itself. Knowing that tells you why your disk can fill up while your dataset shrinks, and why compaction settings matter more than they first appear.


This post is part of a series of sysadmin notes (tag “sysadmin notebook”).
Series “Inside database data files”, part 7/3+4.

Sources

Related articles

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *