Lesson 9: SQL vs NoSQL - Choose the right Database
RDBMS and ACID properties. NoSQL categories: Key-Value (Redis, DynamoDB), Document (MongoDB, CouchDB), Wide Column (Cassandra, HBase), Graph (Neo4j). BASE vs ACID. When to choose SQL, when to choose NoSQL. Polyglot Persistence. NewSQL (CockroachDB, TiDB).
System Architecture: From Zero to Hero
Part 3: Database Architecture & Data Management
xdev.asia
Introduction
Database is the heart of every system. Choosing the wrong database can lead to a complete system re-architecture — expensive and painful. This article helps you understand each type of database and when to choose which one.
1. SQL (Relational Database)
1.1 ACID Properties
Property
Meaning
Example
Atomicity
Transaction all-or-nothing
Money transfer: minus A + plus B or nothing
Consistency
Data is always in a valid state
Balance is never negative
Isolation
Transactions do not affect each other
2 people buy tickets together → no conflict
Durability
Commit completed = data safe
Server crash ≠ data loss
1.2 When to choose SQL?
✓ Data có cấu trúc rõ ràng (schema cố định)
✓ Relationships phức tạp (foreign keys, joins)
✓ Cần ACID transactions
✓ Data integrity quan trọng
✓ Query patterns đa dạng (ad-hoc queries)
Ví dụ: Banking, ERP, E-commerce orders, CRM
1.3 Popular: PostgreSQL vs MySQL
Features
PostgreSQL
MySQL
ACID
Full
Full (InnoDB)
JSON support
Excellent (JSONB)
Good
Full-text search
Built-in
Built-in
Replication
Streaming, Logical
Binlog
Extensions
TimescaleDB, PostGIS
Limited
Best for
Complex queries, data integrity
Web apps, read-heavy
2. NoSQL Categories
2.1 Key-Value Store
SET user:123 → {"name": "John", "email": "[email protected]"}
GET user:123 → {"name": "John", "email": "[email protected]"}
Cực nhanh: O(1) read/write
Hạn chế: Không có query phức tạp, không có relationships
Row Key: user_123
Column Family "profile":
name: "John"
email: "[email protected]"
Column Family "activity":
last_login: "2026-03-30"
posts_count: 42
Mỗi row có thể có columns khác nhau
→ Linh hoạt cho IoT, time-series, analytics
Database
Best for
Cassandra
High write throughput, multi-datacenter
HBase
Hadoop ecosystem, analytics
ScyllaDB
Cassandra-compatible, higher performance
2.4 Graph Database
(John) ─[FRIENDS_WITH]─► (Jane)
(John) ─[WORKS_AT]────► (Google)
(Jane) ─[LIVES_IN]────► (Hanoi)
(John) ─[LIKES]───────► (Post_123)
Query: "Tìm bạn của bạn John sống ở Hà Nội"
→ Cực nhanh với Graph DB, cực chậm với SQL (multiple JOINs)
Each service chooses the most suitable database for its use case.
6. NewSQL
NewSQL combines the advantages of both: ACID + Horizontal Scaling.
Database
Description
CockroachDB
Distributed SQL, PostgreSQL-compatible
TiDB
MySQL-compatible, HTAP
Google Spanner
Global distributed, strong consistency
YugabyteDB
PostgreSQL-compatible, distributed
NewSQL = SQL Syntax + ACID Transactions + Horizontal Scaling
Trade-off: Latency cao hơn single-node SQL (distributed overhead)
Summary
Database Type
Strengths
Use Cases
SQL
ACID, relationships, complex queries
Banking, E-commerce, ERP
Key-Value
Speed, simplicity
Caching, sessions
Document
Flexible schema, rapid dev
CMS, catalogs
Wide Column
Write throughput, scale
IoT, time-series
Graph
Relationship queries
Social, recommendations
NewSQL
Best of both worlds
Distributed ACID
Exercises
Database Selection: For the healthcare management system (hospital), select the database for: (a) Patient records, (b) Medical examination history, (c) Medical images, (d) Symptom search, (e) Patient-doctor relationship.
Polyglot Design: Design data architecture for Shopee-like platform. Determine which database each service needs and why.
Migration: The system currently uses MongoDB for all. Orders need strong consistency. Plan to migrate orders to PostgreSQL without downtime.