Introducing Couchbase: NoSQL for Windows

Scale your application to the sky with the high performance NoSQL Couchbase!

Introducing Couchbase: NoSQL for Windows

In the first article in this series, "NoSQL for the MSSQL Soul" you learned enough about NoSQL databases to know at a high level the type of NoSQL database you're looking for. Many NoSQL databases run either only on *nix systems or if they do run on Windows they aren't meant to do so in a production environment.

Coming from the Microsoft stack, Couchbase is a key/value database that runs on Windows and has excellent support for the .NET platform. It installs with an MSI installer, runs as a proper windows service and can run very well in a production environment on windows servers.

Installation

A common issue windows developers run into when installing Couchbase to run on their dev laptop is that it will automatically bind to your laptop's dynamically assigned IP address, then when you try to use the browser based administration tools it won't work. To correct this, after installing edit C:\Program Files\Couchbase\bin\service_register.bat and change the "set NS_NAME" line to:

set NS_NAME=ns_1@127.0.0.1

After making this change, open a command prompt on the same folder and run service_unregister.bat then service_register.bat.

Buckets?

When you launch the browser-based Couchbase tools for the first time, you will notice that instead of having lists of databases containing tables, there are just buckets with no tables at all. A bucket in Couchbase is a collection of key/value pair data.

Your first instinct coming from a relational SQL background might be to create a separate bucket for each type of data. For example, you might create a Customers bucket and an Organizations bucket. Although it is a valid approach, it will prove difficult to manage as you end up with hundreds of buckets with no real benefit to your application. From a design standpoint, the right grain of bucket should be a decision more tied to what grain at which you want to be able to control settings, replicas, and separation of unrelated applications.

Couchbase has two types of buckets, Couchbase and Memcached. Memcached is purely an in-memory database typically used for things like session state. If the server crashes or is restarted, the contents of a Memcached bucket are gone. A Couchbase bucket is a persistent bucket that will be stored to disk.

To get started create a bucket with the default settings to play with called, "ExampleBucket". Since this is a test bucket on your development laptop, I would recommend setting the memory quota to the smallest size permitted at 100mb. On a production server you would want to set the memory quota as high as possible.

Creating and Storing Data

Download the .NET client libraries from http://www.couchbase.com/develop and create a new web project referencing the DLLs you downloaded. Modify the web.config to include the Couchbase configuration shown below.

At the most base level that is it. Couchbase is now a giant persistent hashtable.

It is worth noting that similar to SqlConnections, there is overhead in creating and connecting with a CouchbaseClient so you will want to implement a mechanism such as a singleton to keep it persistently available and shared.

More Capable Data Types

Although you can store any type of value in Couchbase, Couchbase really starts coming alive when you start using it to store JSON objects. Fortunately, .NET has excellent built-in handling of JSON types. You can declare a POCO (Plain Old CLR Object) with no special markup at all like the one shown below and serialize it to or from JSON using the built in DataContractJsonSerializer.

Most developers will load and store their data in Couchbase in JSON but you can technically store any kind of string value in Couchbase using any encoding desired.

Conclusion

It is quick and easy to get up and running with Couchbase and start loading and saving objects. The next part of this series covers Map/Reduce, which is the functional equivalent in Couchbase of indexes and aggregations in Microsoft SQL Server.

About the Author:

David Talbot has over 14 years of experience in the software industry specializing in creating rich user interface web applications. He is also the author of Applied ADO.NET and numerous articles on technology.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.