All-or-nothing changes that keep your data correct, even when things crash.
A transaction is a group of database steps that must be treated as one single action. ACID is the list of four promises that make a transaction safe.
Moving 100 rupees from account A to account B takes two steps: take 100 from A, add 100 to B. If the server crashes between them, the money has vanished. A transaction wraps both steps so that the database treats them as one unit.
BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 'A'; UPDATE accounts SET balance = balance + 100 WHERE id = 'B'; COMMIT; -- both changes become real at this moment -- ROLLBACK; -- or undo everything since BEGIN
| Property | Promise | How it is usually done |
|---|---|---|
| Atomicity | All steps happen, or none do | An undo log: old values are kept so a half-finished transaction can be rolled back |
| Consistency | Data moves from one valid state to another | Constraints (primary key, unique, foreign key, check) plus your application logic |
| Isolation | Concurrent transactions behave as if they ran one by one (to some chosen degree) | Locks, or MVCC (keeping several versions of a row so readers do not block writers) |
| Durability | Committed data survives power loss and crashes | A write-ahead log (WAL): the change is written to a log file on disk and flushed before COMMIT is confirmed |
Perfect isolation is slow, so databases let you choose a level. Higher levels stop more strange effects but cost speed. The names below come from the SQL standard. Real products differ in details, so read your database's docs.
| Level | Stops | Can still show |
|---|---|---|
| Read uncommitted | Almost nothing | Dirty reads: you see data another user has not committed (and may roll back) |
| Read committed | Dirty reads | Non-repeatable reads: the same row read twice gives two answers |
| Repeatable read | Dirty and non-repeatable reads (often by using a snapshot) | Phantoms (new rows appear) in some databases, and write skew in snapshot-based ones |
| Serializable | All of the above | Nothing, but it is the slowest, and transactions may wait or be told to retry |
A simplified model with a fixed timeline and no random numbers. Real databases differ in details (for example, PostgreSQL treats read uncommitted like read committed, and some engines make serializable transactions wait or retry). Consistency, the C in ACID, means your own rules (like total money stays 1,000) hold after each transaction.
stock = stock - 1), or to lock the row, or to check a version number.| Term | Meaning in easy words |
|---|---|
| Transaction | A group of operations treated as one unit |
| Commit | Make all the changes in the transaction permanent |
| Rollback | Undo all the changes since BEGIN |
| Write-ahead log (WAL) | A log written and flushed before data pages, used for recovery |
| Lock | A flag that stops others from touching a row while you use it |
| MVCC | Multi-version concurrency control: keep several versions of a row so readers and writers do not block each other |
| Dirty read | Reading data another transaction has not committed |
| Phantom read | A repeated query finds new rows that another transaction added |
| Deadlock | Two transactions each wait for the other, so neither can move. The database kills one |
| Two-phase commit (2PC) | A protocol to commit one transaction across several databases |
| Choice | We gain | We lose |
|---|---|---|
| Higher isolation | Fewer odd effects | More waiting and retries |
| Lower isolation | Speed and concurrency | Risk of anomalies you must handle |
| Flush log on every commit | No lost commits | Each commit waits for the disk |
| Group commits or async flush | Higher throughput | A crash may lose the last few commits |
| Distributed transaction | Atomic over many nodes | Slower, blocked if the coordinator fails |