Couchbase SQL++でACIDデータベーストランザクション
Couchbaseは、パフォーマンスと高可用性を犠牲にすることなく、スケールで分散マルチドキュメントACIDデータベーストランザクションをサポートしています。リレーショナルデータベースアプリケーションをCouchbaseに移行し、豊富なSQLサポートを利用しながら、ACIDコンプライアンスを実現します。

ACIDとは何か?
データベースにおいてACIDとは、トランザクションを一貫性をもって、安全かつ確実に処理する方法を表す頭字語である:
A | 原子性 | オール・オア・ナッシング保証で複数のドキュメントを更新。 |
C | 一貫性 | レプリカ、インデックス、XDCRの一貫性を自動的に維持。 |
I | 孤立 | 並行読者のためのコミットされた分離を読む。 |
D | 耐久性 | 調整可能な耐久性レベルによる障害時のデータ保護。 |
トランザクション・コード・サンプル
一般的な取引シナリオのコードサンプルを3つ示す。1つ目は、両方の口座が変更されるか、あるいはどちらも変更されないことを保証する借方/貸方のトランザクションです。2つ目は、SQL++を使用した、ある部署の全従業員の一括更新で、全従業員が更新されるか、あるいは誰も更新されないかを確認します。
START TRANSACTION;
UPDATE customer SET balance = balance + 100 WHERE cid = 4872;
SELECT cid, name, balance from customer;
SAVEPOINT s1;
UPDATE customer SET balance = balance – 100 WHERE cid = 1924;
SELECT cid, name, balance from customer;
ROLLBACK WORK TO SAVEPOINT s1;
SELECT cid, name, balance from customer;
COMMIT ;
// Transfer money from Beth’s account to Andy’s account
transactions.run((txnctx) -> {
var andy = txnctx.get(collection, "Andy");
var andyContent = andy.contentAsObject();
int andyBalance = andyContent.getInt("account_balance");
var beth = txnctx.get(collection, "Beth");
var bethContent = beth.contentAsObject();
int bethBalance = bethContent.getInt("account_balance");
if (bethBalance > transferAmount) {
andyContent.put("account_balance", andyBalance + transferAmount);
txnctx.replace(andy, andyContent);
bethContent.put("account_balance", bethBalance - transferAmount);
txnctx.replace(beth, bethContent);
}
else throw new InsufficientFunds();
txnctx.commit();
});
// Bulk update salaries of all employees in a department
transactions.run((ctx) -> {
var auditLine = JsonObject.create().put("content", "Update on 4/20/2020");
ctx.insert(auditCollection, "Dept10", auditLine);
ctx.query("UPDATE employees
SET salary = salary * 1.1
WHERE dept = 10 AND salary < 50000");
ctx.query("UPDATE department
SET status = 'updated'
WHERE dept = 10");
txnctx.commit();
});