Database Internals Tradeoffs
Hai tầng trade-off
Database correctness và performance cùng nằm trong hai tầng quyết định:
- transaction/concurrency: đổi isolation và locking để lấy correctness hoặc throughput;
- storage engine: đổi read/write/space amplification để phù hợp access pattern.
Transaction layer
Database Transaction gom nhiều operation thành một đơn vị all-or-nothing. ACID mô tả guarantee mong muốn, nhưng Transaction Isolation luôn là trade-off: càng gần serializable càng an toàn, càng dễ contention. Concurrency Control chọn giữa blocking sớm bằng Pessimistic Locking hoặc validate muộn bằng Optimistic Locking.
MVCC và Snapshot Isolation là cách nhiều database giảm blocking giữa reader và writer, nhưng chúng không xóa trade-off correctness/performance. Transaction dài, lock order không nhất quán hoặc retry thiếu kiểm soát vẫn có thể tạo Deadlock và retry herd.
Query layer
Query Execution Plan cho biết bottleneck thật nằm ở Full Table Scan, index không được dùng, join order xấu, sort/aggregate nặng hay statistics sai. Query Planner chỉ chọn tốt khi schema, index và statistics phản ánh workload thật.
Storage layer
B-Tree tổ chức dữ liệu sorted trên disk, trả chi phí write để read/range query nhanh. LSM Tree defer organization: write vào memtable/SSTable nhanh, rồi trả chi phí qua Compaction, Read Amplification, Write Amplification và Space Amplification.
Read/write path
Read Path muốn câu trả lời đã precompute, nhiều copy, ít coordination. Write Path muốn một source of truth, invariant rõ và ordering an toàn. Vì vậy read optimization như Read Replica, Materialized View, cache, specialized read store hoặc fan-out đều phải trả bằng Staleness, sync pipeline hoặc write amplification.
Distributed consistency
Strong Consistency thường cần Consensus và Quorum, đổi lại read sau write không thấy state cũ. Eventual Consistency giảm coordination nhưng đẩy trách nhiệm sang conflict resolution, Read-Your-Writes Consistency, Saga Pattern, Change Data Capture hoặc Transactional Outbox.
Ghi nhớ
Không có storage engine hay isolation level “tốt nhất”. Câu hỏi đúng là workload đang muốn trả chi phí ở đâu: write latency, read latency, disk space, contention, retry hay operational complexity.