On Wicked Easy NoSQL with Couchbase Server

NoSQL doesn’t have to be difficult. Generally speaking, it isn’t. But admittedly, and especially on Windows, it’s not always as clean as it could be. Each database has its challenges. Some are difficult to install. Some are difficult to configure. Some have poor server admin tools. Some lack strong client library support. One of the NoSQL offerings that really gets things right is Couchbase Server. OK, full disclosure, this is my first post as a Developer Advocate for Couchbase!

Couchbase was formed when Membase and CouchOne merged. Couchbase Server 2.0 is going to be a hybrid NoSQL database, combining features from both distributed key/value stores and document oriented databases. The 2.0 product will be released in 2012. In January 2012, an interim 1.8 release will be the first official release of the merged Couchbase Server, formerly Membase Server. As part of my responsibilities at Couchbase, I’m working on the .NET client library for Couchbase Server 1.8. Below is a preview of what’s coming. If you’ve used Membase Server with .NET, you should be familiar with the code below. If you’re new to Couchbase Server, I’m going to start from the beginning.

Installing Couchbase Server

Most NoSQL databases have Windows installers, though sometimes these aren’t kept up to date. In the case of CouchDB, there are a couple of different MSI packages available, but only one works (at least as of October 2011). MongoDB has a command line installer for its service. Couchbase Server, fortunately, has an officially supported Windows installer. You can download the latest installer here. As I write this, the latest server version is 1.7.2. Check back later in January for 1.8. Grab the Community Edition of Membase Server, which is appropriate for development purposes. Membase Server will be renamed Couchbase Server with the 1.8 release. You can also grab the 2.0 Developer Preview, which already sports the new name.

After you run the installer, you’ll be taken the web based admin console. The admin console is where you’ll be able to configure your cluster and manage the nodes within that cluster. In local development, you’ll likely have a single node cluster (e.g., your dev machine).

Once you’ve gotten the server up and running, it’s time to write some code. If you create a simple console app, the easiest way to include the Couchbase .NET client library in your app is to use Nuget. After you add the reference, add the following using statement:

Hello, Couchbase!

using Couchbase;

Then add the following line to your Main method:

static void Main(string[] args) {
    var client = new CouchbaseClient();

After you add these lines, compile the app. You’ll probably get a strange compilation error that the namespace ‘Couchbase’ can’t be found. The reason you’ll see this error is that Visual Studio 2010 (I’ve made the assumption that you’re using 2010) defaults console projects to use the .NET 4 Client Profile, which is a subset of .NET 4. You’ll need to update the .NET version to .NET 4.0 (or 3.5). After making this change, you’ll be able to build.

Next, you’ll need to add some configuration info to your app.config. The entire file should look as follows:


In the config section, you provide the client details on how to connect and to where data will be written. To tell the CouchbaseClient to use the app.config section, update the declaration as follows:

var client = new CouchbaseClient("membase");

Saving and reading primitive data types is just as easy as saving and reading user defined types. So I’ll create a couple of classes Brewery and Beer (intentionally simplified).

public class Brewery {
    public string Name { get; set; }
    public string City { get; set; }
    public string State { get; set; }
public class Beer {
    public string Name { get; set; }
    public Brewery Brewery { get; set; }
    public decimal ABV { get; set; }

These classes are just POCOs (Plain Old CLR Objects) that have been marked Serializable. I’ll new up an instance of each in my Main method.

var brewery = new Brewery { 

    City = "Hartford", 
    State = "CT", 
    Name = "Thomas Hooker Brewery" 
var beer = new Beer { 
    Brewery = brewery, 
    Name = "American Pale Ale", 
    ABV = 5.3m

Next, I’ll persist the Beer instance by calling the client’s Store method. Note that the StoreMode requires the additional using statement be added for Enyim.Caching.Memcached.

client.Store(StoreMode.Set, beer.Name, beer);

After storing, I’ll read the Beer back out and display its name.

var savedBeer = client.Get("beer");

And that’s it, wicked easy, right? Admitedly, this is a very simplified introduction to Couchbase Server. As I’m now taking over the .NET client library, I’ll be posting more detailed tutorials and samples.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>