The Rain and The Shade

July 27, 2011

Why I like RavenDb as a .Net developer

Filed under: NoSQL Databases,RavenDb — ovaisakhter @ 1:00 pm

We often have discussion about amazing new NoSQL databases like CouchDB, Casendra and Redis.

Usually when I am asked about my choice for a NoSQL database I always say “I will use RavenDb”. I have used RavenDb in one of my projects and I am quite happy from the results, and in my experience if you do it right RavenDb performs amazingly well under extreme load scenarios (If not done right you can seriously screw up things but that’s a talk of another time).

As I mentioned I am generally very happy with the performance provided by RavenDb but I have an other reason for reason to use RavenDb,  that I am a .Net developer, and RavenDb is a Database build on top of .Net. So the learning curve for .Net developer to start using it is negligibly small.

Here I will describe the process of starting a new RavenDb endeavor

“Installing” RavenDb

To start using RavenDb download the binaries from http://ravendb.net/download unzip the file go to the server directory and hit the Raven.Server.exe and here you go you are up and running with a brand new Instance of RavenDb.

Try to navigate to http://localhost:8080/ in your browser and you will be able to see the All new Management Studio for RavenDb built in Silverlight. (The interface provides you can option to create sample data if you don’t want to get your hands dirty in code right now, neat idea I say?)

Using RavenDb

RavenDb server interacts with the client using RESTful web services and the response is in the form of JSON. For .Net developers a very extensive .Net client API is also available, which makes your life a lot easier. Using the client API it is extremely simple to get started.

Now you have you server up and running all you need to do is to write the following code in your application

    var documentStore1 = new DocumentStore { Url = “http://localhost:8080″ }.Initialize();

            using (var session1 = documentStore1.OpenSession())
{
session1.Store(new User { Id = “users/ayende”, Name = “Ayende” });
session1.SaveChanges();
}

and here you go you just saved your first record or document as it is called in this neck of wood into the database, and here is what you need to do to get it back.

using (var session1 = documentStore1.OpenSession())
{
var myUserBackFromRaven = session1.Load<User>(“users/ayende”);

                Console.WriteLine(myUserBackFromRaven.Name);
}

Simple ?

and if you need to query your documents all you need to know if how to write a LINQ statement.

var users = session1.Query<User>().Where(x => x.Name.StartsWith(“A”));

             foreach (var user in users)
{
Console.WriteLine(user.Name);
}

Ravendb provides you a possibility to be able to index your documents for faster retrieval of information. The best part is that all the RavenDb index definitions are written using LINQ which makes the learning curve extremely small.

Here is how you define an index on the User Name in side “Raven Management Studio” web interface.

image

or alternatively you can create an index from inside the code.

public class UsersNameIndex : AbstractIndexCreationTask<User>
{
public  UsersNameIndex()
{
Map = users => from user in users
select new { user.Name };
}

    }

and then query this index from the code like

var users = session1.Query<User>(“UsersNameIndex”).Where(x => x.Name.StartsWith(“A”));

                foreach (var user in users)
{
Console.WriteLine(user.Name);
}

Conclusion

The purpose of this blog is not to give you a tutorial of RavenDb rather it is to show you that how terribly simple it is for a .Net developer to start using RavenDb. I believe if you are a .Net developer you will be up and running with RavenDb in the matter of hours and that is why I like RavenDb as a .Net developer.

Links

www.Ravendb.net

Advertisements

July 6, 2011

The keys are Kool again

Filed under: NoSQL Databases,Windows Azure — ovaisakhter @ 11:35 pm

(This post is highly inspired by my mail correspondence with Thomas Jespersen who works at www.spiir.dk)

In the good old days when SQL was the king (it kind of still is) we were in love with the “identity column” and key for the record was insignificant in the design. So normally you will design a database where all the tables will have one primary key whose value will be either a Guid or a auto incrementing number (identity in MSSQL).

With the popularity of the NoSQL and high performance databases where many other things were revolutionized the record Key got its due importance back.Here is what tutorial on the Redis site says about the Redis

“Redis is what is called a key-value store, often referred to as a NoSQL database. The essence of a key-value store is the ability to store some data, called a value, inside a key. This data can later be retrieved only if we know the exact key used to store it”

This kind of seems to be the theme all around in the in most of the NoSQL databases e.g Redis, Azure Table Storage,RavenDB, Cassandra and many more use key to access huge amount of data. These systems index the keys for very fast retrieval of information. I am not saying that querying of the data is not possible(for example RavenDB provides amazing possibility for creating indexes on the data but more on that later) but still the fastest way to get or set the data in these systems is “if you can some how know the key”. 

Now the question is how do you know the keys without getting them from the store, an answer to that could be that you should be able to generate the keys based on the context and the type of query you want to do. let us take an example of twitter, a request comes in and says “who follows me” based on the context we know the current user (ovaisakhter in my case). So the user  ovaisakhter and he wants to know who follows him so we can have  a list in the database against a key “ovaisakhter-followers”. So now we can get all this information in one request.

Let us take another example. We need to save user’s tweets. One way of doing that could be that we maintain one list of tweets per month (depends on how the data will be accessed) so the key can  “UserId-mmyyyy-Tweets” so now if some one comes and asks for the tweets you exactly know where to find tweets quickly.

Azure Table Storage is a bit different then the other NoSQL offerings. It provides less opportunity to play with the structure of the document as the document has to be name-value pairs(maximum 255) with a total size of 1MB. They provide you with an further categorization possibility. You have two keys to play with PartitionKey and RowKey. PartitionKey has very important role to play in the scaling of your datastore (more on that later). Partition key also be used as a Categorization point for the fast data retrieval. Let see how our twitter example can look like if modeled on TableStorage. We can use PartitionKey as “UserId-mmyyyy-Tweets” and then all the Tweets can be stored with this PartitionKey. You can use Reverse ticks(reverse time stamp) as plus some identifier as RowKey for better sorting.  Remember once you know how to generate the “Key” you can use the Parallel.Foreach to get tweets for multiple months in parallel).

So in the new era of software development the Keys are back in fashion so due time should be spent on designing what your keys should be, based on things like your Data Structures, Data Retrieval Requirements,scaling requirements and other things.  

Blog at WordPress.com.