ACID là 4 đảm bảo giúp transaction đáng tin cậy. Lấy ví dụ chuyển tiền (trừ A, cộng B) để thấy rõ:
- Atomicity (nguyên tử): được ăn cả ngã về không — nếu bước cộng B lỗi thì bước trừ A cũng phải hoàn tác.
- Consistency (nhất quán): dữ liệu luôn thỏa ràng buộc — vd FK không cho tạo order với
userIdkhông tồn tại. - Isolation (cô lập): các transaction chạy song song không thấy trạng thái dở dang của nhau — có nhiều mức (READ COMMITTED, SERIALIZABLE).
- Durability (bền vững): đã commit thì không mất dù server crash — PostgreSQL ghi WAL (Write-Ahead Log) trước khi áp thay đổi, có thể replay để khôi phục.
Mức hỗ trợ khác nhau: PostgreSQL ACID đầy đủ; MySQL InnoDB có, MyISAM không; MongoDB 4.0+ có transaction ACID nhiều document nhưng chậm hơn thao tác một document.
Đối lập với ACID là BASE (Basically Available, Soft state, Eventual consistency) — triết lý của nhiều NoSQL như Cassandra: ưu tiên sẵn sàng + hiệu năng hơn nhất quán tuyệt đối, hợp khi cần scale cực lớn.
ACID is four guarantees that make transactions reliable. Use a money transfer (debit A, credit B) to see them clearly:
- Atomicity: all-or-nothing — if crediting B fails, debiting A must be undone too.
- Consistency: data always satisfies constraints — e.g. a FK prevents creating an order with a non-existent
userId. - Isolation: concurrent transactions don't see each other's in-progress state — with several levels (READ COMMITTED, SERIALIZABLE).
- Durability: once committed, data survives a server crash — PostgreSQL writes the WAL (Write-Ahead Log) before applying changes, and can replay it to recover.
Support varies: PostgreSQL is fully ACID; MySQL InnoDB is, MyISAM isn't; MongoDB 4.0+ has ACID multi-document transactions but they're slower than single-document operations.
The counterpart to ACID is BASE (Basically Available, Soft state, Eventual consistency) — the philosophy of many NoSQL stores like Cassandra: favor availability + performance over strict consistency, suitable for extreme scale.