DEV Community

Oliver Mensah
Oliver Mensah

Posted on

Theory behind choosing either Relational or Non-Relational Database for your application

When it comes to choosing a database for a project, I usually go for NoSQL because using it with NodeJs is very simple in my opinion. So I have had this thought to research on what must influence my decision to choose either Relational or Non-Relational Database systems. Finally, I got to know there is a great theorem behind this.

Today, I would like to share with you the C.A.P theorem.
CAP stands for:
Consistency ( all nodes see the same data at the same time)
Availability (a guarantee that every request receives a response about whether it was successful or failed)
Partition tolerance (the system continues to operate despite arbitrary message loss or failure of part of the system)

The CAP theorem, also known as Brewer's theorem, states that it is impossible for a distributed computer system to provide all above-listed features at the same.
Though database systems cannot have all three but can have any two of them.

SQL databases employ CA while NoSQL databases use AP from C.A.P. So depending on the type of an application you are building, knowing this concept will help you a lot in choosing the right database system.

For instance, if you are working on an application where you want an update to be seen by users of the application without a second or minute delay among any of them then consistency matters In such an application, distributed system with consistency holds. Therefore SQL database system will be used in this case.

The main takeaway from this little piece is knowing whether your application needs CA or AP from CAP is very important and will help influence your decision to choose the right database system.

Top comments (7)

Collapse
 
alanmbarr profile image
Alan Barr

What would be real clear examples where I 100% need to use a SQL database versus NoSQL?

Collapse
 
dobesv profile image
Dobes Vandermeer

You should be asking why you nee a nosql database instead. Consistency, atomicity, and transactions are very helpful in writing reliable applications. With nosql you generally have to handle multi document transactions yourself. They don't support aggregations over multiple collections/tables so reporting is a huge pain compared to SQL. SQL should be the default for most applications, unless they have a great motivation to use a nosql database.

Collapse
 
olivermensahdev profile image
Oliver Mensah

Roughly I don't think there might be 100% use cases since the features make up the entire hundred 100%. Both are using 2/3 of that, which by the theory that is the maximum adoption.

Collapse
 
tojacob profile image
Jacob Samuel G.

Have you read about "ACID transactions"? The concepts are somewhat similar to the ones you are exposing. And MongoDB will implement ACID in its next version! :D

Collapse
 
olivermensahdev profile image
Oliver Mensah

Wow. yet another theory. I will read about it but it will be awesome if you can throw more light on it

Collapse
 
tojacob profile image
Jacob Samuel G.

ACID allows us to work with the data in a secure way.

A very simplistic summary:

A. Atomicity, each operation will execute its complete steps.
C. Consistency, ensures that each operation will be fully executed and, if not, will not be executed.
I. Isolation, ensures that one operation (execution in the database) will not affect others.
D. Durability, ensures that changes are persistent.

It's a bit more complex, I still do not understand it at all, but I know it's a good thing and soon Mongo will implement it. It seems to me that Arangodb already implements it.

Thread Thread
 
olivermensahdev profile image
Oliver Mensah

Okay. Thanks. I will do more research on this so we can have more discourse later