ACID Transactions

A transaction is a sequence of operations that must be atomic, consistent, isolated, and durable. Atomic means the transaction either completes entirely or has no effect at all. Consistent means it moves the database from one valid state to another. Isolated means concurrent transactions do not interfere with each other. Durable means once a transaction commits, its changes survive system failures.

Serializability

Serializability is the property that a system of simultaneous transactions is synchronized correctly if there exists a serial execution that produces the same database state. A set of transactions is serializable if the corresponding precedence graph contains no cycles — meaning you can order the transactions in a way that avoids conflicts and deadlocks.

Two-Phase Locking

Two-phase locking prevents a transaction from requesting an additional lock after it has released its first lock. Transactions under this protocol execute in two phases: during the expanding phase, all locks are requested and placed, either gradually or all at once; during the shrinking phase, locks are released one by one or in total at the end. This means locks can only be acquired during expansion and only released during shrinkage — no intermixing of locking and unlocking is allowed.

Pessimistic Concurrency Control

With two-phase locking, any set of concurrent transactions is guaranteed to be serializable. This approach is called pessimistic concurrency control because it assumes conflicts will happen and prevents them upfront by locking resources before access. The cost is reduced concurrency, since transactions must wait for locks to be released.

Optimistic Concurrency Control

Optimistic methods assume conflicts between concurrent transactions will be rare. Transactions proceed in three phases: a read phase, a validation phase, and a write phase. During the read phase, all required objects are read, saved to a separate transaction workspace, and processed there without placing any preventative locks. After processing, the validation phase checks whether the applied changes conflict with any other active transactions. If two transactions block each other, the one currently in validation is deferred. If validation succeeds, all changes from the workspace are written to the database during the write phase.

Recovery

Recovery is the restoration of a correct database state after an error. This involves rolling back incomplete transactions, replaying committed transactions from logs, and ensuring durability guarantees are met even when hardware or software fails mid-operation.

The CAP Theorem

The CAP theorem states that in any massive distributed data management system, only two of the three properties — consistency, availability, and partition tolerance — can be ensured simultaneously. Consistency means all nodes see the same data at the same time. Availability means every request receives a response, whether success or failure. Partition tolerance means the system continues to operate even when network splits occur. Distributed systems must choose which property to sacrifice when a partition happens, leading to different design tradeoffs in databases like Cassandra, MongoDB, and traditional SQL systems.