Blog Hero

Sql vs NoSql is bad comparison

To decide which database to use, usually we start by grouping all database option. If you type on search engine or your favorite AI llm "how to choose the database?" they mostly will group it by "Sql vs NoSql". My Hot takes Sql vs NoSql is a bad comparison and I will explain to you guys why.

What is Sql vs NoSql?

Sql (structured query language) it's refer to database that implement sql standard (Wikipedia SQL) example database that categorize as sql mysql, postgresql, sqlite, microsoft sql etc. NoSql (is standfor not only sql) are database that don't implement sql standard for example redis, mongodb, dynamodb, etc.

this the example of comparison sql vs nosql:

AspectSqlNoSql
Data StructureTables with fixed rows and columnsDocuments, Key-Value, Graphs, Wide-column
SchemaRigid, predefined (strict)Dynamic, flexible (schema-less)
ScalabilityVertical (scale up, bigger server)Horizontal (scale out, more servers)
Data IntegrityHigh (ACID compliant)Flexible (BASE: Basically Available, Soft state, Eventual consistency)
QueryingStructured Query Language (SQL)Varies by database (e.g., JSON, API)
Best ForStructured data, complex joinsUnstructured or semi-structured data, rapid development, scalability

Problem with Sql vs NoSql?

1. Misinterpretation of NoSql

Some people misinterpret NoSql -> Not Sql. This sometimes cause bias comparison. Just add no on every aspect.

AspectSqlNoSql
SpeedSlowFast
ScalabilityNot ScaleScale
Num DataSmallBig

of cource comparison above it's non sense.

2. NoSql is better than Sql

The cause of using comparison above we conclude that NoSql is better than Sql. Using the comparison above it's like looking for IT project employee but we only categorize the job type into 2 Backend Developer and Non Backend Developer then make the comparison as follow:

AspectBackend DeveloperNon Backend Developer
UI DesignNoYes
Implement UI DesignNoYes
Project ManagementNoYes
DeploymentNoYes

Based on that comparison we must not recrut any backend developer because that role is bad. You will found that the app will not work. And you also realize that not all non backend developer can do design, not all non backend developer can do project management etc.

So what's the solution? Just make broader role by spliting non backend developer

3. NoSql have broader category

If you compare query of sql database like Mysql and PostgresSql they will have same syntax:

sql
SELECT * FROM users WHERE name = 'alice';

And how they store data is also similar they both store data on disk.

How about NoSQL database let's say mongodb and redis does query look's same? redis use key value syntax:

redis
SET hello world
GET hello

mongodb use syntax kinda like javascript:

javascript
db.users.find({ "name": "alice" })

Not only that the way mongodb store data is completely different with redis. mongodb store data on disk while redis on memory. So NoSql need broader category how to categorize even more? I will explain in next section.

Better way to categorize database

I got inspired from this website called DB-Engines that categorize database based on data model like:

1. Relational Database

Relational Database is database that implement sql standard. Example of relational database:

2. Key-Value Store

Key-Value Store is database that store data in key value format. Usually they store data primarily in memory (RAM). Example of key-value store database:

Advantages of storing data in memory is the access speed is very fast compared to storing on disk. That's why memory is used to store small and temporary data to speed up data access. But the drawback of memory is the size were small. Some key value store database also have TTL (time to live) feature which data will be deleted automatically after certain period.

3. Document Database

Document Database is database that store data in document format (like json). The big difference from other database is it can store diffrent shape of data in same table/document/index. Example of document database:

Imagine you have to store log from your aplication which is like this:

Let's try to store using table like database like relational database

trace_idlevelmessageerrorpaymentInfouserId
b7f9e8c2-4a1d-4e2e-9c3a-2f5d8e7b6c1ainfoUser login successful--12345
a3d2c1b4-5e6f-7a8b-9c0d-1e2f3a4b5c6derror-Database connection failed-12345
f1e2d3c4-b5a6-7890-1234-56789abcdef0infopayment successful-{"amount": 100, "currency": "USD"}12345

We can see that some column is blank (-) because not all data has that column. Imagine if you insert new data that have column/key that doesn't exist in this current table, You will lose data from that key. You have to add new column to that table.

If we use document database we don't have that problem, you can store the data as it is. document database will add key automatically.

But having dynamic key (dynamic schema) also have some drawback. Imagine you use document database to store user data like this

json
{
  "name": "Alice",
  "age": 30,
}

Then you got typo name to username when insert new data

json
{
  "username": "John",
  "age": 15,
}

If you use relational database. The database will reject this input and prevent data malform. In Document Store the data will be accepeted . The moment you query for name the data will not be found. you must fix the code and all malform data.