When should I use a NoSQL database instead of a relational database? Is it okay to use both on the same site?

Viewed 73154

What are the advantages of using NoSQL databases? I've read a lot about them lately, but I'm still unsure why I would want to implement one, and under what circumstances I would want to use one.

8 Answers

Martin Fowler has an excellent video which gives a good explanation of NoSQL databases. The link goes straight to his reasons to use them, but the whole video contains good information.

  1. You have large amounts of data - especially if you cannot fit it all on one physical server as NoSQL was designed to scale well.

  2. Object-relational impedance mismatch - Your domain objects do not fit well in a relaitional database schema. NoSQL allows you to persist your data as documents (or graphs) which may map much more closely to your data model.

Handling A Large Number Of Read Write Operations

Look towards NoSQL databases when you need to scale fast. And when do you generally need to scale fast?

When there are a large number of read-write operations on your website & when dealing with a large amount of data, NoSQL databases fit best in these scenarios. Since they have the ability to add nodes on the fly, they can handle more concurrent traffic & big amount of data with minimal latency.

Flexibility With Data Modeling

The second cue is during the initial phases of development when you are not sure about the data model, the database design, things are expected to change at a rapid pace. NoSQL databases offer us more flexibility.

Eventual Consistency Over Strong Consistency

It’s preferable to pick NoSQL databases when it’s OK for us to give up on Strong consistency and when we do not require transactions.

A good example of this is a social networking website like Twitter. When a tweet of a celebrity blows up and everyone is liking and re-tweeting it from around the world. Does it matter if the count of likes goes up or down a bit for a short while?

The celebrity would definitely not care if instead of the actual 5 million 500 likes, the system shows the like count as 5 million 250 for a short while.

When a large application is deployed on hundreds of servers spread across the globe, the geographically distributed nodes take some time to reach a global consensus.

Until they reach a consensus, the value of the entity is inconsistent. The value of the entity eventually gets consistent after a short while. This is what Eventual Consistency is.

Though the inconsistency does not mean that there is any sort of data loss. It just means that the data takes a short while to travel across the globe via the internet cables under the ocean to reach a global consensus and become consistent.

We experience this behaviour all the time. Especially on YouTube. Often you would see a video with 10 views and 15 likes. How is this even possible?

It’s not. The actual views are already more than the likes. It’s just the count of views is inconsistent and takes a short while to get updated.

Running Data Analytics

NoSQL databases also fit best for data analytics use cases, where we have to deal with an influx of massive amounts of data.

I designed and implemented solutions with NoSQL databases and here is my checkpoint list to make the decision to go with SQL or document-oriented NoSQL.

DON'Ts

SQL is not obsolete and remains a better tool in some cases. It's hard to justify use of a document-oriented NoSQL when

  • Need OLAP/OLTP
  • It's a small project / simple DB structure
  • Need ad hoc queries
  • Can't avoid immediate consistency
  • Unclear requirements
  • Lack of experienced developers

DOs

If you don't have those conditions or can mitigate them, then here are 2 reasons where you may benefit from NoSQL:

  • Need to run at scale
  • Convenience of development (better integration with your tech stack, no need in ORM, etc.)

More info

In my blog posts I explain the reasons in more details:

Note: the above is applicable to document-oriented NoSQL only. There are other types of NoSQL, which require other considerations.

Related