Saturday, October 1, 2011

Few thoughts on CouchDB

 

// EDIT: See my post with tutorial of CouchDB + relax-net usage to build simple tagged gallery in ASP.NET MVC

Apache CouchDB is one of the most popular NoSQL document database engines out there. In this post I would like to make a quick comparison to RavenDb as seen with .NET developer eyes.

Architecture

Again, we’re talking about document database, which means, that structure of data stored is very elastic and there is no referential integrity. If you want to keep your data pieces in some particular relation to each other, it’s up to your code to enforce this relations. Yet the data you store is not a blob understandable only to your application, but rather has document semantics, which means you can access particular properties of a document at database level. As with RavenDb, database can’t tell your entities apart, it’s up to client to store entity metadata in some understandable form.

CouchDB exposes REST API and is heavily javascript based. Documents are transferred as JSON objects. All map-reduce queries, validation functions and show functions are written in javascript. Javascript is not my favourite language, but I must say, in case of CouchDB it serves its purpose well. Still, using javascript in your database is something a DBA has to get used to :)

CouchDB doesn’t support transactions. It uses optimistic concurrency model which allows to avoid some cases when your database might enter inconsistent state, but if your code has a lot of places where multiple db operations have to either pass or fail as a group, then working with CouchDB will require a lot of extra work (for example implementing compensation code). This is a sacrifice CouchDB makes to gain performance and scalability.

What’s quite interesting, CouchDB allows you to write so called validation functions (validate data before data operations) and show functions (format a return data in user readable form – ex. HTML). Thanks to that, you could implement whole application using nothing more than a CouchDB itself. I guess that would rarely be the case if you are implementing something more than a simple blog, because you’d want to use a solid framework like ASP.NET MVC or Rails to make things easier.

The database implements some basic authentication mechanisms and you can extend it by implementing more. As for authorization, you can grant a user read-only or full access to your database. If you want something more sophisticated, I guess you have to use validation functions.

Performance

A NoSQL solution like CouchDB is built with performance in mind. Builtin replication mechanism allows to scale the application. What’s more you can create hierarchies of database instances which will operate on subtrees of a indexed structure of your documents and by that you gain extra scalability possibilities. Unfortunately there is no builtin sharding support, but there are external frameworks to support just that (see Gizzard).

Ease of use

Usage of CouchDB is made easier by some tools and projects that make up the Couch's ecosystem. This is a big advantage over RavenDb, even for .NET developers. Couchbase is a free bundle of CouchDB and it’s management interface Futon, that is easy to install and use on a Windows machine. As for .NET clients you have a choice, but in my opinion, relax-net  seems to have best design and is easies to use. You won’t get Linq support as with RaveDb, but it’s still bearable.

Documentation

There is documentation on project’s page available, but if you’re new to this, I suggest you start with this free online ebook.

Final thoughts

CouchDB seems to be a solid piece of software used by really big players and what’s more – it’s free. It’s well developed community guarantees that you will get the support and tools you need to build your great apps. Yet, lack of transaction support will make your life harder.

1 comment:

Skitsanos said...

Just to let you know there is free IDE for CouchDB, and it definitely can do much more than Futon, you can grab it here: http://kanapeside.com/

Share