Nâng CaoDatabase iconDatabase

CAP theorem là gì? Ý nghĩa trong database selection?

CAP: Partition Tolerance luôn bắt buộc trong distributed system — nên lựa chọn thực sự là CP (consistency khi partition) vs AP (availability khi partition). CP: PostgreSQL, MySQL, HBase — reject requests để đảm bảo data consistent; AP: Cassandra, CouchDB, DynamoDB — vẫn serve requests nhưng data có thể stale.

CAP theorem: trong distributed database, chỉ có thể đảm bảo tối đa 2 trong 3 properties: Consistency (mọi node thấy data giống nhau tại bất kỳ thời điểm nào), Availability (mọi request nhận response, không có error hay timeout), Partition Tolerance (hoạt động bình thường khi có network partition — network failure giữa các nodes). Trong thực tế network partition không thể tránh khỏi nên P luôn cần — lựa chọn thực sự là CP hay AP.

CP examples: PostgreSQL, MySQL, ZooKeeper — khi partition xảy ra, primary từ chối writes để tránh split-brain, một số requests bị rejected. AP examples: Cassandra, DynamoDB, CouchDB — khi partition, tất cả nodes vẫn serve requests nhưng có thể trả về stale data, resolve conflicts sau (eventual consistency).

MongoDB tùy cấu hình: default (writeConcern w:1, readConcern local) gần AP — writes confirm ngay khi primary nhận, reads có thể stale; với writeConcern majority + readConcern majority → CP nhưng latency cao hơn.

PAC theorem (2012) - refinement: trong thực tế không phải binary CP hay AP — có spectrum, và "consistency" có nhiều mức độ (strong, eventual, causal). PACELC model mở rộng thêm: khi không có partition (Else), tradeoff giữa Latency và Consistency — DynamoDB chọn low latency (AP + E→L), Spanner chọn consistency (CP + E→C).

Chọn CP khi: financial data, inventory, medical records — correctness quan trọng hơn availability. Chọn AP khi: social feeds, shopping carts, leaderboards — availability và performance quan trọng hơn strict consistency; eventual consistency thường đủ.

Xem toàn bộ Database cùng filter theo level & chủ đề con.

Mở danh sách Database