A gentle introduction to CouchDB for relational practitioners
CouchDB is a document-oriented database written in Erlang that addresses a particular “sweet spot” in data storage and retrieval needs. This blog post is an introduction to CouchDB for those of us who have a relational database background.
A CouchDB database doesn’t have tables. It has a collection of documents, stored in a B+Tree. A document is a collection of attributes and values. Values can be atomic, or complex nested structures such as arrays and sub-documents. When you add a document to a database, CouchDB stores it in the B+Tree, indexed by two attributes with special meaning: _id and _rev.
CouchDB lets you store related data together even if it isn’t all the same type of data; you can store documents representing blog posts, users, and comments — all in the same database. This is not as chaotic as it sounds. To get your data back out of CouchDB in sensible ways, you define views over the database. A view stores a subset of the database’s documents. You can think of them as materialized partial indexes. You can create a view of blog posts, and a view of comments, and so on. Each view is another B+Tree. It stays up-to-date with the changes you make to the database.
You can structure your documents any way you want. There is no fixed schema. If you decide after a while that you want to add tags to your blog posts, you can simply write new posts with a collection of tags and save them into the database. Old posts won’t have tags, but that’s OK; if your application code can read the old format and write the new format, you have an application that doesn’t need a fixed schema.
Updates are never done in-place. Everything is copy-on-write. New revisions are saved into the database as new documents, obsoleting old ones, and CouchDB increments the _rev property each time. To update a document, you fetch it, change it, and send it back, specifying the _id and the most recent _rev. If someone else changed the document in the meantime, your _rev is stale, and your update fails. You must re-fetch and re-save; you can’t lock a document.
CouchDB runs on HTTP and JSON. All of its operations, such as store and retrieve, are standard HTTP requests. The documents themselves are represented in JSON. You can talk directly to CouchDB with curl, Ajax, and anything else that can speak HTTP. There is no “protocol” other than this. CouchDB isn’t just Web-friendly, it is actually made of the same technologies that the Web is made of. You query CouchDB by specifying the database, document ID, view name, and so forth directly in the URL. For example, to fetch a blog post document from the “blog” database, you might issue a GET /blog/helloworld. Queries against views and other objects have simple clean URLs, too.
CouchDB uses special documents, called “design documents,” to store JavaScript code in the database. The code defines the views I mentioned earlier. Another thing you can store is validation functions. This is code that CouchDB executes when you save a document to the database. It accepts a document as input, and can reject it, so you do have control over the schema of documents — it doesn’t have to be a free-for-all. In the blog application, you can have a validation function that starts by enforcing “every document must have a ‘type’ property, and its content must be one of (post,user,comment).” Then you can have separate validation logic for each type of document.
Design documents can also contain something called “show functions.” CouchDB will execute the function’s code in response to HTTP requests to that URL, and send the resulting data back as an HTTP response (as usual). With show functions, you can store entire applications inside the database. Your browser might never even know that it’s talking to a database directly, instead of a web server with a database behind it.
CouchDB isn’t designed for arbitrary queries at runtime. You can only query one view, show function, or database at a time. You can’t do joins. You can’t do arbitrary GROUP BY and ORDER BY. You have to decide in advance what operations you’re going to need, and build views for them. You can then issue requests to those views, essentially the equivalent of key lookups and range scans with a few basic options such as an offset, limit, and reverse order. Now, having said that, you can define views that reduce the database down to aggregates, create a custom ordering, and so on. You can define the equivalent of the relational “project” operation inside your view code.
Here’s how: a view is a map-reduce operation. A view is defined in two parts, the map and the reduce. The map is not optional; it generates the contents of the view. It is a JavaScript function. CouchDB iterates over the database and feeds each document into the function, collects the results, and inserts them into the view’s B+Tree index. Inside the view function’s code, you emit key-value 2-tuples.
- The key will identify the tuple in the index that’s built to store this view. It can be simple or complex, so you can create a view that’s keyed by [this,that,the_other_thing]. The view will be ordered by the same thing; that’s how B+Trees work.
- The value you emit is whatever you want the B+Tree to store at its leaf nodes, and can also be complex (it’s a document, like any other).
The “reduce” part of the operation is optional. It computes what is stored in the non-leaf nodes of the B+Tree index. For example, you can use it to create aggregates, such as summing up counts of comments. In addition to the reduce part of the code, there is a “rereduce”. The rereduce is called as the operation is invoked on higher and higher non-leaf nodes, all the way to the root of the tree. CouchDB knows how to take advantage of the data that’s stored by these reduce and rereduce operations, so for example, it doesn’t necessarily have to descend all the way to the leaf nodes and scan in order to count how many documents match a particular query.
An important thing to know about all this code is that nothing is allowed to have side effects. You can’t modify the database in a view definition, for example. Documents are immutable; it’s all copy-on-write. You get input; you can specify output; that’s it, period. It’s a form of functional programming. Why do we care? Because it keeps things simple and elegant, and enables all kinds of nice properties and functionality, such as replication and eventual consistency and cache expiry and scaling to multiple nodes and so on.
The database file is append-only. Old versions don’t automatically get cleaned up. The database grows forever until you compact it. This process builds a new database and then does a swap-and-discard. The append-only, copy-on-write design makes backups easy, and data corruption unlikely.
CouchDB comes with a “graphical user interface” called Futon. It’s built right into the database, and surprise! — it works through HTTP and Ajax. You just fire up CouchDB, point your Web browser to /_utils, and go. It’s a fun way to explore CouchDB.
With all that in mind, why would you want to use CouchDB instead of a relational database? For most things I’m involved with, I want a relational database. But I got asked recently to help with a database that’ll store records about people. Although nobody has implemented anything yet, it’s a terrible match for a relational database, and an excellent fit for a document-oriented one. The inputs are going to be arbitrary documents with different structures, such as census records, birth records, tax records, estate and probate records, marriage records, and so on. Nobody knows what it’s going to store in the future. When people build “flexible schemas” in relational databases, they usually go for the so-called EAV or EBLOB models. In other words, they aren’t using the database relationally at all, and it simply doesn’t work well. This type of project needs a document-oriented database.
I’ve left out a lot of important details, but the point of this post is to understand the high-level CouchDB concepts and how they’re implemented, so you can reason for yourself about it. If you’ve read this far and you think that CouchDB might be a good fit for your needs, I encourage you to take a look at CouchDB, The Definitive Guide.
Update: CouchOne adapted the above post as a white paper and guest-posted it on their own blog.



Wonderful overview!
Thanks :)
Roland Bouman
8 Sep 10 at 9:11 pm
@Baron:
Do consider couchdb to be “stable enough” and “fast enough” on non-trivial data sets?
Rob Wultsch
12 Sep 10 at 5:35 am
I am struggling to answer that without getting into a lot of fine details that I intentionally didn’t talk about in the post. So I hope it’s OK if I say “no comment” for now.
Xaprb
12 Sep 10 at 7:27 am
I’ve prototyped a few systems in couchdb and hopefully one day will run a production couch, but 2 things usually pop up that send me back to relational land.
1) Views are powerful, but all the data elements need to be in a single doc (record/row). So if you have a bunch of people docs (with tags) and a bunch of invoice docs, it gets real hard to do things like: list all invoices over $1,000 from people with this tag, unless you copy the tags from the person doc to the invoice doc, or record the invoices on the person doc, etc.
2) Even though you can aggregate data via views, you can not get a sorted view of the values, which can be an issue when the result of your aggregate has several thousand values.
Troy
22 Sep 10 at 5:30 pm
This is the first article I’ve read that explains with great detail where a document-oriented database makes more sense that trying to use a relational.
it seems that the whole “noSQL” zealots don’t realize that the need is different and it’s not a relational-killer.
kudos!
Alex
22 Sep 10 at 8:24 pm
typo:
s/the is a “rereduce”/there is a “rereduce”/
randomreader
22 Sep 10 at 10:01 pm
Fixed, thanks!
Xaprb
22 Sep 10 at 10:32 pm
“The inputs are going to be arbitrary documents with different structures, such as census records, birth records, tax records, estate and probate records, marriage records, and so on.”
If the data is not parseable and cannot be stored in a structured way, you might as well store them in files on disk. Or in file cabinets on paper. No-SQL needed :-)
I agree storing documents as BLOBs in a RDBMS is a crap solution, I fail to see why storing it in CouchDB, magically makes it a better solution.
Björn
23 Sep 10 at 6:54 am
Bjorn: unlike the file system, CrouchDB handles revisions for you really nicely. You never delete, always create a new document. That specific requirement has lead me, in the past, to using BLOBs in a relational db instead of the file system.
REST is a big plus, but there’s another big one: security.
Using the file system, you’re letting the OS manage the rights. Using CouchDb, you’re keeping the rights management within the datastore. Meaning you can easily scale, and easily back stuff up etc.
Note: I’m assuming CouchDB implements some form of rights :)
Ryan
23 Sep 10 at 9:25 am
This is nice; it has a somewhat familiar feel to it, though, like the beginnings of an open source Lotus Notes database engine: it has a lot of the attractions, but with cleaner interfaces (like HTTP) which weren’t around in Notes’s early days.
Interesting also, however, that it is missing some of the complications that were added in later versions of Notes – e.g. attribute-level updates, record locking etc. – to compromise with real world problems of volume, performance and functionality.
Jumma
23 Sep 10 at 11:42 am
CouchOne adapted the above post as a white paper and guest-posted it on their own blog.
Xaprb
6 Oct 10 at 9:03 am