
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:
| Aspect | Sql | NoSql |
|---|---|---|
| Data Structure | Tables with fixed rows and columns | Documents, Key-Value, Graphs, Wide-column |
| Schema | Rigid, predefined (strict) | Dynamic, flexible (schema-less) |
| Scalability | Vertical (scale up, bigger server) | Horizontal (scale out, more servers) |
| Data Integrity | High (ACID compliant) | Flexible (BASE: Basically Available, Soft state, Eventual consistency) |
| Querying | Structured Query Language (SQL) | Varies by database (e.g., JSON, API) |
| Best For | Structured data, complex joins | Unstructured 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.
| Aspect | Sql | NoSql |
|---|---|---|
| Speed | Slow | Fast |
| Scalability | Not Scale | Scale |
| Num Data | Small | Big |
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:
| Aspect | Backend Developer | Non Backend Developer |
|---|---|---|
| UI Design | No | Yes |
| Implement UI Design | No | Yes |
| Project Management | No | Yes |
| Deployment | No | Yes |
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
- UI Design -> UI/UX Designer
- Implemet UI Design -> Frontend Developer
- Project Management -> Project Manager
- Deployment -> DevOps Engineer
3. NoSql have broader category
If you compare query of sql database like Mysql and PostgresSql they will have same syntax:
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:
SET hello world
GET hellomongodb use syntax kinda like 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:
- MySql
- PostgreSql
- SQLite
- Microsoft SQL Server
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:
- Redis
- ValKey
- Memcached
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:
- MongoDB
- CouchDB
- Firebase Firestore
Imagine you have to store log from your aplication which is like this:
- json
{ "trace_id": "b7f9e8c2-4a1d-4e2e-9c3a-2f5d8e7b6c1a", "level": "info", "message": "User login successful", "userId": 12345 } - json
{ "trace_id": "a3d2c1b4-5e6f-7a8b-9c0d-1e2f3a4b5c6d", "level": "error", "error": "Database connection failed", "userId": 12345 } - json
{ "trace_id": "f1e2d3c4-b5a6-7890-1234-56789abcdef0", "level": "info", "message": "payment successful", "paymentInfo": { "amount": 100, "currency": "USD" }, "userId": 12345 }
Let's try to store using table like database like relational database
| trace_id | level | message | error | paymentInfo | userId |
|---|---|---|---|---|---|
| b7f9e8c2-4a1d-4e2e-9c3a-2f5d8e7b6c1a | info | User login successful | - | - | 12345 |
| a3d2c1b4-5e6f-7a8b-9c0d-1e2f3a4b5c6d | error | - | Database connection failed | - | 12345 |
| f1e2d3c4-b5a6-7890-1234-56789abcdef0 | info | payment 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
{
"name": "Alice",
"age": 30,
}Then you got typo name to username when insert new data
{
"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.