RDBMS và ACID properties. NoSQL categories: Key-Value (Redis, DynamoDB), Document (MongoDB, CouchDB), Wide Column (Cassandra, HBase), Graph (Neo4j). BASE vs ACID. Khi nào chọn SQL, khi nào chọn NoSQL. Polyglot Persistence. NewSQL (CockroachDB, TiDB).
System Architecture: From Zero to Hero
Phần 3: Database Architecture & Data Management
xdev.asia
Giới thiệu
Database là trái tim của mọi hệ thống. Chọn sai database có thể dẫn đến re-architecture toàn bộ hệ thống — tốn kém và đau đớn. Bài này giúp bạn hiểu rõ từng loại database và khi nào nên chọn cái nào.
1. SQL (Relational Database)
1.1 ACID Properties
Property
Ý nghĩa
Ví dụ
Atomicity
Transaction all-or-nothing
Chuyển tiền: trừ A + cộng B hoặc không gì cả
Consistency
Data luôn ở trạng thái hợp lệ
Balance không bao giờ âm
Isolation
Transactions không ảnh hưởng nhau
2 người cùng mua vé → không conflict
Durability
Commit xong = data an toàn
Server crash ≠ mất data
1.2 Khi nào chọn 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 Phổ biến: PostgreSQL vs MySQL
Feature
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)
Mỗi service chọn database phù hợp nhất cho use case của nó.
6. NewSQL
NewSQL kết hợp ưu điểm của cả hai: ACID + Horizontal Scaling.
Database
Mô tả
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)
Tổng kết
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
Bài tập
Database Selection: Cho hệ thống healthcare management (bệnh viện), chọn database cho: (a) Hồ sơ bệnh nhân, (b) Lịch sử khám bệnh, (c) Hình ảnh y tế, (d) Tìm kiếm triệu chứng, (e) Quan hệ bệnh nhân-bác sĩ.
Polyglot Design: Thiết kế data architecture cho Shopee-like platform. Xác định mỗi service cần database nào và tại sao.
Migration: Hệ thống hiện dùng MongoDB cho tất cả. Orders cần strong consistency. Lập kế hoạch migrate orders sang PostgreSQL mà không downtime.