1. Computing

Tutorials on writing Code with NoSQL databases

By

NoSQL

I've been using RDBMS relational databases for years now. The list includes Ms Access, SQL Server 6.5,7, 2005, 2008, Sybase ASE, MySQL, PostgreSQL, DB2 and Advantage but I have never dabbled in NoSQL. This is an alternative datastore technology that has sprung up in the last five years or so.

There were also non SQL RDBMS such as the original dBase, Foxbase etc that all used index sequential access methods but NoSQL solves different needs.

The traditional SQL based RDBMS have been around for 20-40 years since the mathematics was understood and formulated in the 70s.

Recently though particularly in the casual game playing sector in games like Zynga's FarmVille it was realized that a SQL based database was too slow for that.

Unlike most web applications which are read heavy, social games are write heavy. It's an exceedingly demanding environment where millions of players are making tens of changes to their game every minute. If a player selects 20 crops to harvest then that's 20 updates that needs to be applied.

Clearly you want to store those updates but holding them in ram before hand (and being able to read them quickly from there). NoSQL databases can do tens or hundreds of thousands of read/updates every second while RDBMS can't.

Zynga solved this with a database technology that they developed which you can read about in Building a scalabale game server. They partnered with Couchbase to develop Membase.

Simple put, this caches updates in memory and then persistes them to disk and is scalable by adding more servers. There's a good introduction to NoSQL whitepaper (PDF) from Couchbase.

If you're interested in Couchbase, this article gives a very good insight into the components that make up Couchbase.

Membase is now obsolete, with Couchbase server an open source replacement which has both a community license (free) and an Enterprise one. So it's the one I've decided to look at in C# and see if I can advance my knowledge and yours.

There are C, C++, C# (and PHP, Ruby and Java) compatible SDKs from their develop page which also made it a good choice. I used version 1.8 as version 2.0 is still in developer preview.

Getting Started

Installation of the server is pretty straightforward though my virus/firewall checker warned me that it was a P2P server but allowed me to install it anyway. Be careful where you install it as the server requires you to specify how much ram it can use. For my purposes I set it to a modest 512MB max out of the 6GB I have.

Couchbase servers can be clustered and the client library in the SDK is cluster aware. Once you've picked your ram size, other servers in the same cluster have to use the same amount.

Administration is through a web page on port 8091, with the url http://localhost:8091/index.html. When you setup your first server you have to specfy memory size, type of default bucket and whether replication is enabled. The two types of bucket are Memcached (useful for working with RDBMS servers) and Couchbase so pick the latter. As you'll most likely only have the one server replication isn't needed. It's a process that keeps data between different servers in sync.

Once you've gone through the settings, and setup the Administrator password etc, it will save it all out and start the server. So now I have a single server that's currently using up 65MB out of the 512Mb I've specified and it's time to start using it. In a RDBMS this is where the database tables would be created and populated but this is NoSQL so what comes next?

First install the client SDK for the language you will be using. As I decided I'd test it with C# on Windows, I installed the .NET 1.1 client and a local copy of the documentation.

Thinking Differently in NoSQL

I've been writing SQL etc for so many years that I can near enough create the set of tables, normalized for simple to middling applications without much thought. But NoSQL is different and needs you to move away from thinking about relational tables.

At it's simplest you have a key/value store which is roughly like a .NET Dictionary class except a lot more flexible.

A Couchbase bucket holds document data up to 20 Mbytes. So you might use it for shopping carts, user profiles, user sessions, timelines, game states, pages, conversations and product catalogs. A good rule of thumb is keep things small so more fits in memory. That gets you more speed.

Values can be simple things like an int, or a class. Just add the attribute [Seriazable] to any class to make it able to be saved.

In the second tutorial, I'll start storing and fetching data and showing how to use it in a small application.

  1. About.com
  2. Computing
  3. C / C++ / C#
  4. Pro. Development
  5. Learning to Program with NoSQL Databases

©2014 About.com. All rights reserved.