DBMS · core
Transaction theory in DBMS: ACID, schedules and serializability
A transaction is one logical unit of work that takes the database from one valid state to another. DBMS theory explains how ACID is kept when many transactions run at once (schedules, serializability and locking) and after a crash (the log and recovery).
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- ACID: Atomicity (all or nothing), Consistency (rules such as keys and 'balance >= 0' hold before and after), Isolation (the result is as if the transactions ran one at a time), Durability (committed data survives a crash).
- Who keeps each letter: atomicity and durability come from the recovery system (the log); isolation comes from concurrency control (locks or versions); consistency comes from the database rules plus correct application code.
- Transaction states: active, partially committed (the last statement has run), committed; or failed, then aborted (rolled back). After an abort the DBMS restarts or kills the transaction.
- A schedule is the order in which the steps of concurrent transactions run. A serial schedule runs them one after another. A schedule is serializable when its result equals the result of some serial schedule.
- Conflict serializability: two steps conflict when they come from different transactions, use the same item, and at least one is a write. Draw the precedence graph (an edge Ti -> Tj when a step of Ti conflicts with a later step of Tj): the schedule is conflict serializable exactly when the graph has no cycle.
- Problems when isolation is weak: dirty read (reading uncommitted data), lost update (one write overwrites another), non-repeatable read (the same row reads differently), and phantom rows (a repeated query returns new rows).
- Two-phase locking (2PL): a transaction takes all its locks before it releases any (a growing phase, then a shrinking phase), which guarantees conflict serializability. Strict 2PL keeps write locks until commit, which avoids cascading aborts. 2PL can still deadlock.
- Recovery: every change is written to the log before the data pages (write-ahead logging). After a crash, committed work is redone and unfinished work is undone. Example: deduct a fee and insert the payment row in one transaction, so a crash never leaves money taken with no payment.
The red pen
Common traps: mistake → fix
Mistake: A try-catch makes the two updates safe
Fix: only a transaction undoes the first update.
Mistake: Serializable means the transactions really ran one at a time
Fix: their steps may interleave; only the result must equal some serial order.
Mistake: Consistency in ACID is the same as consistency in CAP
Fix: ACID consistency means the data rules hold; CAP consistency means every read sees the latest write.
Mistake: Two-phase locking prevents deadlock
Fix: it does not; it guarantees serializability, and deadlocks still need detection or timeouts.
Mistake: A crash after COMMIT can lose data
Fix: durability means it cannot.
Easy to hard
Questions interviewers ask, easy to hard
- What is ACID? Explain each letter with a fee payment example.
- A fee is deducted but not marked paid. What went wrong and how do you fix the code?
- What is a serializable schedule? How do you test a schedule for conflict serializability?
- What is two-phase locking, and why does strict 2PL avoid cascading aborts?
- How does a database recover committed work after a crash?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.