ACID is a set of four guarantees a database makes about a transaction, a group of changes treated as one unit of work. ACID stands for Atomicity, Consistency, Isolation and Durability. You meet it whenever you write BEGIN and COMMIT in SQL, and it is a common reason to choose a relational database for money, orders and inventory.
Each letter covers a different failure.
SQLite documents itself as atomic, consistent, isolated and durable even if a program crash, operating system crash or power failure interrupts a transaction. In its default rollback-journal mode, it copies the original content of each page it is about to change into a journal file, so it can restore the database after a failure.
The example below moves 150 from an account that holds 100. The CHECK constraint rejects the second update, and the rollback also undoes the first update.
import sqlite3
print(sqlite3.sqlite_version)
db = sqlite3.connect(":memory:", isolation_level=None)
db.execute("CREATE TABLE account (id INTEGER PRIMARY KEY, balance INTEGER NOT NULL CHECK (balance >= 0))")
db.execute("INSERT INTO account VALUES (1, 100), (2, 50)")
try:
db.execute("BEGIN")
db.execute("UPDATE account SET balance = balance + 150 WHERE id = 2")
db.execute("UPDATE account SET balance = balance - 150 WHERE id = 1")
db.execute("COMMIT")
except sqlite3.IntegrityError as e:
print("error:", e)
db.execute("ROLLBACK")
print(db.execute("SELECT id, balance FROM account ORDER BY id").fetchall())
Output from SQLite 3.45.1:
3.45.1
error: CHECK constraint failed: balance >= 0
[(1, 100), (2, 50)]
Account 2 still holds 50, even though its update ran before the failure. That is atomicity at work.
ACID stands for Atomicity, Consistency, Isolation and Durability. The four properties describe what a transaction promises, not how a database stores data. A system can be ACID for single-row writes and still give weaker guarantees across several rows or machines, so check what the vendor means by "ACID" before relying on it.
BASE stands for Basically Available, Soft state, Eventual consistency. Eric Brewer set it against ACID in his 2000 PODC keynote: BASE systems accept stale data and weaker consistency to stay available. ACID favors correctness at commit time. The line has blurred: MongoDB supports multi-document transactions on replica sets from version 4.0 and on sharded clusters from 4.2.