Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSql Referential Data

Disclaimer: by referential Data, i do not mean referential integrity

I am learning nosql and would like to understand how data should be modeled. In a typical relational database for an CMS application, for example, you may have two tables: article and author, where article have an reference to the author.

In nosql system, you may create an article document this way since they are just disguised object graph

{
title: "Learn nosql in 5 minutes",
slug: "nosql_is_easy", 
author: {firstName: "Smarty"
          lastName: "Pants"
}

{
title: "Death to RDBMS",
slug: "rdbms_sucks", 
author: {firstName: "Smarty"
          lastName: "Pants"
}

and so on...

Say one day Mr. Smarty Pants decided to change his name to Regular Joe because nosql has become ubiquitous. In such uses case, every article would need to be scanned and the author's name updated.

So my questions is, how should the data be modeled in nosql to fit the basic use cases for an CMS so that the performance is on par or faster than RDBMS? mongodb, for example, claims CMS as an use-case ...

Edit:

Few people have already suggested normalizing the data like:

article 
{
title: "Death to RDBMS",
slug: "rdbms_sucks", 
author: {id: "10000001"}
}

author
{
name: "Big Brother",
id: "10000001"
}

However, since nosql, by design, lack joins, you would have to use mapreduce-like functions to bring the data together. If this is your suggestion, please comment on the performance of such operation.

Edit 2:

If you think nosql is not suitable solution for any kind of data that requires referential data, please also explain why. This would seem to make the use case for nosql rather limited since any reasonable application would contain relational data.

Edit 3:

Nosql doesn't mean non-relational

like image 727
ltfishie Avatar asked Sep 29 '11 02:09

ltfishie


1 Answers

Your data is clearly relational: an article has an author. You can model your data in a NOSQL store like MongoDB in just the same way as you would in a relational store BUT because there are no joins in the database you have to make two calls to the database so you haven't gained anything.

BUT ... what you CAN do with a NOSQL store is to denormalize the data somewhat to get improved performance (a single round trip to get everything you need to display the article) BUT at the expense of immediate consistency: trading off always accurate author names for eventually accurate author names.

You might for example, use this in your article:

author: {firstName: "Smarty", lastName: "Pants", _id:DE342624EF }

Now you can display the article really fast and when someone does change their name you can either kick off a background task to update all the existing articles or you can wait for a periodic consistency sweep to fix it.

Many major web sites no longer give you immediate consistency. There are changes that you make that are only eventually seen by the other users on the site.

like image 55
Ian Mercer Avatar answered Sep 28 '22 05:09

Ian Mercer