CouchDB naked
Few months ago I spent sometime browsing through the entire CouchDB source code because this seemed like the best way to learn how large-sized Erlang (and specifically OTP) apps get written. Other choices were to look at ejabberd or RabbitMQ but they were a bit intimidating because you’ve the added complexity of understand the XMPP and AMQP protocols. I’ll be doing a series of blog posts covering some interesting aspects of the CouchDB implementation. I’m going to assume that you know a little bit of Erlang and you’re familiar with CouchDB itself.
So in this first part of the series dubbed “CouchDB naked”, I’m going to show you what CouchDB looks like if you strip off all the bells and whistles. At the heart of CouchDB is a B-Tree implementation (file-based, append-only) with one extra feature - the ability to stash “reduction” values along with the pointers to child nodes. CouchDB b-trees have two types of nodes - key-pointer nodes (kp-node) and key-value nodes (kv-node). The key-value nodes are the leaf nodes and store the actual
{Key, Value}pairs. The key-pointer nodes store a bunch of{Key, Pointer, Reduction}tuples (one each for child nodes that they point to). The Pointer is just an offset in the file where that child node has been stored. Reduction is the result of applyingreduce()to all the KV pairs in that child sub-tree.
