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

70,849

Solution 1

Relational databases enforces ACID. So, you will have schema based transaction oriented data stores. It's proven and suitable for 99% of the real world applications. You can practically do anything with relational databases.

But, there are limitations on speed and scaling when it comes to massive high availability data stores. For example, Google and Amazon have terabytes of data stored in big data centers. Querying and inserting is not performant in these scenarios because of the blocking/schema/transaction nature of the RDBMs. That's the reason they have implemented their own databases (actually, key-value stores) for massive performance gain and scalability.

NoSQL databases have been around for a long time - just the term is new. Some examples are graph, object, column, XML and document databases.

For your 2nd question: Is it okay to use both on the same site?

Why not? Both serves different purposes right?

Solution 2

NoSQL solutions are usually meant to solve a problem that relational databases are either not well suited for, too expensive to use (like Oracle) or require you to implement something that breaks the relational nature of your db anyway.

Advantages are usually specific to your usage, but unless you have some sort of problem modeling your data in a RDBMS I see no reason why you would choose NoSQL.

I myself use MongoDB and Riak for specific problems where a RDBMS is not a viable solution, for all other things I use MySQL (or SQLite for testing).

If you need a NoSQL db you usually know about it, possible reasons are:

  • client wants 99.999% availability on a high traffic site.
  • your data makes no sense in SQL, you find yourself doing multiple JOIN queries for accessing some piece of information.
  • you are breaking the relational model, you have CLOBs that store denormalized data and you generate external indexes to search that data.

If you don't need a NoSQL solution keep in mind that these solutions weren't meant as replacements for an RDBMS but rather as alternatives where the former fails and more importantly that they are relatively new as such they still have a lot of bugs and missing features.

Oh, and regarding the second question it is perfectly fine to use any technology in conjunction with another, so just to be complete from my experience MongoDB and MySQL work fine together as long as they aren't on the same machine

Solution 3

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.

Solution 4

NoSQL is database system where data is organised into the document (MongoDB), key-value pair (MemCache, Redis), graph structure form(Neo4J).

Maybe here are possible questions and answer for "When to go for NoSQL":

  1. Require flexible schema or deal with tree like data?
    Generally, in agile development we start designing system without knowing all requirement in upfront, where later on throughout development database system may need accommodate frequent design changes, showcasing MVP (Minimal Viable product). Or you are dealing with data schema which is dynamic in nature. e.g. System logs, very precise example is AWS cloudwatch logs.

  2. Data set is vast/big?
    Yes NoSQL database are the better candidate for applications where database needs to manage million or even billions of records without compromising over performance.

  3. Trade off between scaling over consistency
    Unlike RDMS, NoSQL database may lose small data here and there(Note: probability is .x%), but its easy to scale in terms of performance. Example: This may good for storing people who are online in instant messaging app, tokens in db, logging web site traffic stats.

  4. Performing Geolocation Operations: MongoDB hash rich support for doing GeoQuerying & Geolocation operations. I really loved this feature of MongoDB.

In nutshell, MongoDB is great fit for applications where you can store dynamic structured data at large scale.

Solution 5

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.

Share:
70,849

Related videos on Youtube

smfoote
Author by

smfoote

Updated on June 03, 2020

Comments

  • smfoote
    smfoote about 4 years

    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.

  • smfoote
    smfoote almost 14 years
    Thanks for the answer. Your examples of when to use NoSQL are vague at best. I was hoping for a more specific use case so I can decide if any of my data would be better stored in a NoSQL database.
  • Asaf
    Asaf almost 14 years
    I try not to answer the same question twice, look at my previous answer to a very similar question stackoverflow.com/questions/3621415/…
  • hansvb
    hansvb almost 14 years
    I don't think ACID is exclusive to relational databases. You can have durability guarantees, transactions, view consistency in non-relational databases.
  • Jo Smo
    Jo Smo about 9 years
    I agree with Asaf's great answer, there are really only a few scenarios when you should be needing a NoSQL over a RDBMS. I see NoSQL more as a backup db or "add-on db" than a main db. I haven't seen a good system yet, where the core db was a NoSQL.
  • Jay Q.
    Jay Q. almost 9 years
    "NoSQL database may lose small data here and there" WTF!? Now who in their right mind would want to risk that? This must be false.
  • Rachael
    Rachael almost 9 years
    @RamshVel could you give an example of a key-value store type database? Thanks.
  • RameshVel
    RameshVel almost 9 years
    @Rachael, some examples are redis, leveldb and riak.. there are tons around, you can google it
  • Hrishikesh
    Hrishikesh over 8 years
    @JayQ. Yes, it may be false. That's why I said *maybe. Then why can't we use NpSQL DB's for transactional operations?
  • Gopherine
    Gopherine over 5 years
    seems correct but i dont think its also right to compare the how much time it has spent if that were the case everyone would be using assembly language in all of their application i would rather say it always comes down to your application and usecase
  • mtraceur
    mtraceur about 2 years
    Everything may lose data. If you're not shielding end-to-end from gamma ray events, it's only a matter of probability before a bit magically flips on you, which could be a sign bit, or the bit being used as the boolean flag in a CPU register at the wrong moment causing a condition to literally take the wrong branch, and so on. And that's just one example. Plus, there's no certainty that your entire tech stack is big free. The only difference is how high the probability of loss/error is, if the system has enough redundancy to notice that it happens, and if the system tells you when it notices.
  • mtraceur
    mtraceur about 2 years
    Ultimately, when people say a transactional DB "can't lose data", that's shorthand for specific ways of losing data being made impossible, which of course comes with a cost, just sometimes negligible cost. For example, two clients which are not coordinating (or not coordinating well enough, such as due to a bug in their code) write to the same key in a key-value store right after reading and modifying it, and there is a race condition if they do it at around the same time. A transaction prevents that, by forcing one client to wait or do something else until the other is done.